Muhammad Ahmed AbuTalib
Muhammad Ahmed AbuTalib

Reputation: 4302

Setting up a lookup attribute in an entity

I have an entity called Samples. Inside that I have many fields, one of them is a ProjectLocation drop down list.

Now I use this code to insert a new instance of type Sample in the CRM through WCF.

Entity sample = new Entity("new_sample");
sample.Attributes["name"]= "Ahmed";

This works but when I want to enter the ProjectLocation I don't have any idea how should it be accomplished.

This doesn't work.

Entity projectLoc = service.Retrieve("projectlocation", (new guid here), columnset)
sample.Attributes["new_projectlocation1"] = projectLoc

What can be done?

Upvotes: 1

Views: 2796

Answers (3)

Nicknow
Nicknow

Reputation: 7224

You need to change your code to return an EntityReference, here is the updated code:

Entity projectLoc=service.Retrieve("projectlocation",(new guid here),columnset) //retrieves a correct projectloc. 
sample.Attributes["new_projectlocation1"]=projectLoc.ToEntityReference(); //Now it'll work

Upvotes: 1

James Wood
James Wood

Reputation: 17562

You need to set an EntityReference.

sample.Attributes["new_projectlocation1"] 
  = new EntityReference("projectlocation", new guid here);

Upvotes: 0

Konrad Viltersten
Konrad Viltersten

Reputation: 39108

Lookups are instances of EntityReference not Entity. I've always imagine a lookup as a pointer (via a GUID) to an entity, instead of the entity itself. But then again, my diploma work was in C++ so I'm supposed to be brain-washed regarding pointers. :)

Upvotes: 1

Related Questions