locked
Cant update parentcontactid on opportunity RRS feed

  • Question

  • I have a custom workflow which should update the parentcontactid on the opportunity to a new Contact, but it doesn't seem to work, it doesn't error or fail it just doesn't update the field.

    Is there something different with this field to others? What am I doing wrong?

    TIA

    Thanks

                          foreach (Entity ec2 in ec.Entities)
                            {
                                ec2["parentcontactid"] = new EntityReference("contact", ParentContactID);
                                service.Update(ec2);
    
                            }


    Pete

    Tuesday, November 18, 2014 3:35 PM

Answers

  • It looks like your problem is that you have both the customerid and parentcontactid attributes in the Entity instance that you pass to the Update method. As parentcontactid is derived from customerid, I expect CRM is overriding your parentcontactid value with the original value from customerid.

    There are two ways you could resolve this:

    1. Don't include customerid. It is included because you include it in the ColumnSet. I don't see any reason for you to have it in the ColumnSet
    2. Update the customerid instead of the parentcontactid

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by Pete.CRM Wednesday, November 19, 2014 7:52 PM
    Wednesday, November 19, 2014 7:35 PM
    Moderator

All replies

  • Hi Pete,
    I can't test your code right now, but it looks ok 

    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, November 18, 2014 4:03 PM
  • This code is fine. Please put entire code.

    Regards Faisal

    Tuesday, November 18, 2014 4:07 PM
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Client;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using System.Activities;
    using Microsoft.Xrm.Sdk.Query;
    
    
    namespace XXX.CRM.CustomWorkflows
    {
        public class AccountReassignContactsOpps : CodeActivity
        {
            // Inputs
            [Input("Enter Contact")]
            [ReferenceTarget("contact")]
            public InArgument<EntityReference> InputContact { get; set; }
    
            [Input("Enter Contact of the Parent Account")]
            [ReferenceTarget("contact")]
            public InArgument<EntityReference> InputParentAccountContact { get; set; }
    
            protected override void Execute(CodeActivityContext executionContext)
            {
                // Context
                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                //Create the tracing service
                ITracingService tracingService = executionContext.GetExtension<ITracingService>();
    
                // Logic
                try
                {
                    // Source Contact
                    var ContactID = InputContact.Get<EntityReference>(executionContext);
                    // New Contact
                    Guid ParentContactID = InputParentAccountContact.Get(executionContext).Id;
    
                    if (ParentContactID != null)
                    {
    
                        QueryExpression qe = new QueryExpression
                        {
                            EntityName = "opportunity",
                            ColumnSet = new ColumnSet("customerid", "opportunityid"),
                            Criteria = new FilterExpression()
                        };
                        //filters
                        qe.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0); // Active Opps Only
                        qe.Criteria.AddCondition("customerid", ConditionOperator.Equal, ContactID.Id); // Where CustomerID equals the old Contact
    
                        EntityCollection ec = service.RetrieveMultiple(qe);
                        tracingService.Trace("QE has run with Opportunity results = " + ec.Entities.Count);
                        if (ec.Entities.Count != 0)
                        {
                            foreach (Entity ec2 in ec.Entities)
                            {
                                ec2["parentcontactid"] = new EntityReference("contact", ParentContactID);
                                ec2["name"] = "plugin has run," + ParentContactID;
                                service.Update(ec2);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new InvalidPluginExecutionException("Plugin - Update Opps - " + e.Message);
                }
                finally
                {
                    //   throw new InvalidOperationException("Want to see trace");
                }
            }
        }
    }
    


    Pete

    Tuesday, November 18, 2014 4:14 PM
  • anyone ? :)

    Pete

    Wednesday, November 19, 2014 8:07 AM
  • did you check if your query actually returns some entities?

    My blog: www.crmanswers.net - Rockstar 365 Profile

    Wednesday, November 19, 2014 8:26 AM
  • I did, I also put a trace message in there to return how many were returned, and then also added another update for a different field to make sure it would write to that record and that updated fine. It just seems to be this field for some reason.

    Thanks


    Pete

    Wednesday, November 19, 2014 8:27 AM
  • You are retrieving opportunities therefore you can update them but there is no field of parentcontactid in opportunity. I think you need to update customerid or another entity.

    Regards Faisal

    Wednesday, November 19, 2014 10:06 AM
  • Hi Faisal,

    there is on the more recent CRM's Org's. Agreed it used to be customerid in the old days :)

    See below


    Pete

    Wednesday, November 19, 2014 10:19 AM
  • You are retrieving opportunities therefore you can update them but there is no field of parentcontactid in opportunity. I think you need to update customerid or another entity.

    Regards Faisal

    Depends the CRM version. CRM 2013 has parentcontactid inside opportunity, and CRM 2011 I believe from ur12+

    My blog: www.crmanswers.net - Rockstar 365 Profile

    Wednesday, November 19, 2014 10:21 AM
  • Fine. The code looks fine. Have you registered you assembly? on which event you are trigerring workflow?

    Regards Faisal

    Wednesday, November 19, 2014 11:39 AM
  • Its registered as a custom workflow, the workflow is definitely passing in the 2 values. CRM Online too.

    I'm at a loss...


    Pete

    Wednesday, November 19, 2014 11:40 AM
  • Sorry another question is have you signed you assembly?

    Regards Faisal

    Wednesday, November 19, 2014 1:21 PM
  • Yup, couldn't add it to CRM Online without that.

    Pete

    Wednesday, November 19, 2014 2:23 PM
  • It looks like your problem is that you have both the customerid and parentcontactid attributes in the Entity instance that you pass to the Update method. As parentcontactid is derived from customerid, I expect CRM is overriding your parentcontactid value with the original value from customerid.

    There are two ways you could resolve this:

    1. Don't include customerid. It is included because you include it in the ColumnSet. I don't see any reason for you to have it in the ColumnSet
    2. Update the customerid instead of the parentcontactid

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by Pete.CRM Wednesday, November 19, 2014 7:52 PM
    Wednesday, November 19, 2014 7:35 PM
    Moderator
  • Thanks David, option 1 didn't make a difference but 2 did.

    Wasn't aware CustomerID Overwrite PArentContactID, will keep that in mind for future.


    Pete

    Wednesday, November 19, 2014 7:52 PM