Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
CRM 2011: Plugin PostContactCreate IOrganizationService.Update does not work

Answered CRM 2011: Plugin PostContactCreate IOrganizationService.Update does not work

  • segunda-feira, 20 de agosto de 2012 08:59
     
      Contém Código

    Hello,

    on PostContactCreate I change the contact-entity and add an attribute:

    // service is IOrganizationService
    entity.Attributes.Add("new_attribute", "StringValue");
    service.Update(entity);

    It works fine, but when the duplicate detection pops up and I say "Save Record" it does not save the record, and it gives me no error. Debugging tells me, the error occours at service.Update. But why ony at duplicate detection?

    I run through the same code for the account, there it works perfect.

    Any idea?

Todas as Respostas

  • segunda-feira, 20 de agosto de 2012 09:05
     
     Respondido Contém Código

    Hi Mycapi,

    There might be having problem as you are updating entity in context by adding new attribute might create some duplicity.

    Instead do like following.

    // service is IOrganizationService
    Entity entity = new Entity("contact"); // This will only update a single attribute rather then a whole entity object.
    entity.attribute("contactid",SOME GUID);
    entity.Attributes.Add("new_attribute", "StringValue");
    service.Update(entity);

    Hope it helps.


    Thanks and regards,
    Hiren Solanki.
    Please vote or mark as a answer if it helped you.

    • Marcado como Resposta mycapi segunda-feira, 20 de agosto de 2012 09:33
    •  
  • segunda-feira, 20 de agosto de 2012 09:34
     
     

    Thx! That works. But i cannot understand why account works without this new Entity.

  • segunda-feira, 20 de agosto de 2012 15:45
     
      Contém Código

    Hey.

    You could add a condition to check if the attribute already exist

    if(entity.Attributes.Contains("new_attribute"))
    {
       entity.Attributes["new_attribute"] = "StringValue";
    }
    else
    {
       entity.Attributes.Add("new_attribute", "StringValue");
    }

    Jaimie
  • terça-feira, 21 de agosto de 2012 06:56
     
      Contém Código

    Thanks for answer. But that does not work. The attribute does not exist, at the moment I want to add it. I debugged it and didn´t see it and I tested this code after your answer:

    if (entity.Attributes.Contains("new_attribute"))
    {
      String str = "new_attribute exists"; 
      Exception ex = new Exception(str); 
      throw new InvalidPluginExecutionException(str, ex);
    }                    
    It does not throw the exeption. But the solution of Solanki Hiren works.