0
votes

I am using Microsoft Dynamics CRM Online.

I have a working plug-in starting on the Create Message (post-operate) of an entity called (let's simplify) "entity1". One of the features of this plug-in is that it determines a certain value. Let's call this one "importantValue". The plug-in also creates a relation between "entity1" and another entity (let's simplify again) "entity2", and populates the corresponding lookup field in "entity1".

All of this is working just fine. However, I also want the plug-in to set a field of "entity2" (called "samplefield") to the value of "importantValue". I know how to retrieve the GUID of the related record of entity2, but I can't get the plug-in to update this (already existing) record.

This is the part of the code making problems. I already retrieved the GUID "entity2Guid" and filled importantValue (it's a string). My IOrganizationService is called "service".

 Entity entity2 = new Entity("new_entity2");
 entity2.Id = new Guid (entity2Guid);
 entity2["new_samplefield"] = importantValue;
 service.Update(entity2);

What am I doing wrong? Thanks in advance!

1
What exception do you get?Henk van Boeijen

1 Answers

-1
votes

I think if you are updating a custom field you will have to add the field name to the entities attribute collection. Try updating the entity this way:

if (entity2.Attributes.ContainsKey("new_samplefield"))
    entity2["new_samplefield"] = importantValue;
else
    entity2.Attributes.Add("new_samplefield", importantValue);

Maybe if you know this attribute will never be included in the attribute collection you can skip the if statement and always add it.