1
votes

I have two custom entities called "entity1" and "entity2".

In "entity1", there's a lookup field connecting it to "entity2". Now, I just want to use a plug in to fill in this lookup field when I create a new record of "entity1". To make things simpler, let's say I want to connect every new record of "entity1" with an already existing record in "entity2". And let's say the name of this record in "entity2" is "sampleName".

I'm not that experienced with plug ins, and I can't figure out how to make this work. I guess I need to use this line of code below somehow, but I have a problem with retrieving the correct GUID. How do I do this? And is there something else I have to include in my code?

entity1["new_lookup"] = new EntityReference("entity2", neededID);

The plugin itself does some other stuff too, and it's all working. I just can't seem to get past this lookup-field problem. I'm using Microsoft Dynamics CRM Online 2016 by the way.

Thanks in advance for your help!

1

1 Answers

2
votes

If you want to just get your neededId from its name, you'll need to do a query. Couple of options: CRM LINQ, QueryByAttribute, FetchXml or QueryExpressions.

You'll find many examples in the MSDN. Here's one using QueryByAttribute which might be the easiest one to ge started with. But better to use any of the others, as they are more generic.

Because that's firing from a plugin, you don't actually need to authenticate so just ignore the credentials in the OrganizationServiceProxy bit and use the IOrganizationService you already have in the plugin

You could also create EntityReferences from alternate keys without doing a query provided you have enabled those in entity2 and that column is part of the alternate key.