locked
Problem updating relation between account and contact RRS feed

  • Question

  • Hi!

    I've extended the Lead entity with a relation to Contacts, this since we need to handle more than one contact on the Lead.

    So when I convert a Lead into Contact, Opportunity and Account, I've created a plugin by reusing some code published elsewhere here on the forum. This code is working fine for most convertions, but if the account already exists and this account that already exists also has a relation to the contacts, it doesn't work. This is of course not normal, but it can happen.

    The solution is that the new account will take the relation to the contacts instead. And the old account will then loose these contacts.

    Below is the code, which I tried to use to, but it doesn't work, I get an exception, that server is unable to process it.

                    ICrmService crmservice = context.CreateCrmService(true);               
                   
                    //Query               
                    QueryByAttribute query = new QueryByAttribute();
                    query.ColumnSet = new AllColumns();               
                    query.EntityName = EntityName.contact.ToString();               
                    query.Attributes = new string[] { "excanto_leadid" };               
                    query.Values = new object[] { leadid };               
                   
                    RetrieveMultipleRequest request = new RetrieveMultipleRequest();               
                    request.Query = query;               
                    request.ReturnDynamicEntities = true;               
                   
                    RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmservice.Execute(request);               
                   
                    foreach (DynamicEntity person in response.BusinessEntityCollection.BusinessEntities)               
                    {
                        Lookup lookup = new Lookup("account", accountid);
    //                    lookup.IsNull = true;
    //                    lookup.IsNullSpecified = true;
                        person["accountid"] = lookup;   //new Lookup("account");
                        person["parentcustomerid"] = lookup;
                        crmservice.Update(person);
                    }

    I've tried only using person["accountid"] and I also tried setting the value to null, or the lookup to null, but I get the same error.

    What is it that I am doing wrong? What I could do is ofcourse to not move the contacts from the already existing account, and instead handle the error, but that was not my first solution, and now I want to know what I am doing wrong.
    Sunday, November 15, 2009 6:31 PM

Answers

  • I think you have to use the Customer datatype, not Lookup, and only set the parentcustomerid attribute.

                    Customer cust = new Customer("account", accountid);
    //                    lookup.IsNull = true;
    //                    lookup.IsNullSpecified = true;
                        person["parentcustomerid"] = cust;
                        crmservice.Update(person);
    Sunday, November 15, 2009 7:35 PM