Update Multiple Contacts

Unanswered Update Multiple Contacts

  • 07 Mei 2012 19:20
     
      Memiliki Kode

    Well I'm developing a Plugin for CRM 2011 online, and I'm stuck here, I need that when an Account record its updated not created, the field of Fax and all of Address 1, will be updated too on all the children Contacts here the code I have:

     
    public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                tracing.Trace("Creating an instance of the web service for user {0}", context.UserId);
                IOrganizationServiceFactory crmFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = crmFactory.CreateOrganizationService(context.UserId);
                tracing.Trace("OK");
                Account account;
                if (context.Depth > 1)
                    return;
                if (context.PrimaryEntityName.ToLower().Equals("account"))
                {
                    if (context.MessageName.ToLower().Equals("update"))
                    {
                        if (context.InputParameters != null && context.InputParameters.Contains("Target"))
                        {
                            if (context.InputParameters["Target"] != null && context.InputParameters["Target"].GetType().Equals(typeof(Entity)))
                            {
                                account = ((Entity)context.InputParameters["Target"]).ToEntity<Account>();
                                tracing.Trace("Query definition started.");
                                QueryExpression query = new QueryExpression(Contact.EntityLogicalName);
                                query.ColumnSet = new ColumnSet(true);
                                LinkEntity link = new LinkEntity
                                {
                                    LinkFromEntityName=Contact.EntityLogicalName,
                                    LinkToEntityName=Account.EntityLogicalName,
                                    LinkFromAttributeName="new_parentaccount",
                                    LinkToAttributeName="accountid"
                                };
                                query.LinkEntities.Add(link);
                               query.Criteria.AddCondition("new_parentaccount", ConditionOperator.Equal, account.AccountId);
                                tracing.Trace("Query definition ended. Starting execution.");
                                EntityCollection Contacts = service.RetrieveMultiple(query);
                                tracing.Trace("Query Execution ended.");
                                tracing.Trace("Storing results in temp.");
                                foreach (Contact c in Contacts.Entities)
                                {
                                    
                                        try
                                        {
                                            c.Fax = account.Fax;
                                            //same for all the rest of the desires fields
                                            service.Update(c);
                                            
                                        }
                                        catch (Exception ex)
                                        {
                                            tracing.Trace("Error : " + ex.Message);
                                        
                                        }
                                        break;
                                    
                                }
                                tracing.Trace("All Clear");
                            }
                        }
                    }
                }
                return;
            }

    Actually when i tested this, when i Update the account, only one child was updated, i'm not sure if i'm have a mistake on the code or when i register the plugin, any way, if you can some ideas, it will be useful.



Semua Balasan

  • 07 Mei 2012 19:47
     
     

    Hi Xavier,

    Have you tried adding a trace statement which shows what Contacts (and how many) are being returned?  That will help you work out if the issue is with the query or with the update logic (I'm guessing the former).

    Thanks,

    David

  • 07 Mei 2012 20:38
     
     

    i already do some part of what you say, on my test environment i have a few contacts (5 for test) who are children from one particular account, and at the point of Contacts.Entities the count are 5, but i didn't check what records of contacts are those. 

    Thanks.

  • 08 Mei 2012 2:06
    Moderator
     
      Memiliki Kode

    Hello Xavier,

    Answer is obvious:

    foreach (Contact c in Contacts.Entities)
    {
    	try
    	{
    		c.Fax = account.Fax;
    		//same for all the rest of the desires fields
    		service.Update(c);
    	}
    	catch (Exception ex)
    	{
    		tracing.Trace("Error : " + ex.Message);
    	}
    	break;
    }
    You have break inside your foreach loop. Once first contact is updated your code leaves loop.

    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

  • 08 Mei 2012 3:33
     
      Memiliki Kode

    OMG... I really havent seen that break there, not sure why i put there, anyway, i will errase it, and see how this work then

    Edit: i remove that break, and did all the registration stuff again, and now when i update the account a error appears 

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred. Contact a system administrator or refer to the Microsoft Dynamics CRM SDK troubleshooting guide.Detail: 
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220891</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
        <KeyValuePairOfstringanyType>
          <d2p1:key>OperationStatus</d2p1:key>
          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
        </KeyValuePairOfstringanyType>
      </ErrorDetails>
      <Message>An error occurred. Contact a system administrator or refer to the Microsoft Dynamics CRM SDK troubleshooting guide.</Message>
      <Timestamp>2012-05-08T16:04:52.6975703Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText>
    
    [ActivityFeeds.Plugins: ActivityFeeds.Plugins.ActivityClose]
    [8e52e7e6-1e99-e111-ada2-1cc1de798391: ActivityFeeds.Plugins.ActivityClose: Update of account]
    
    
    </TraceText>
    </OrganizationServiceFault>

    Here the info that how i register the plugin.

    Isolation Mode: Sandbox

    Assembly Stored: DataBase

    Message: Update

    Primary Entity: account

    Stage of execution: Post-Operation

    Execution Mode: Asynchronous 

    And by the way, I don't know, but the tracing service doesn't work for CRM Online ?