Before we begin, I want to make sure I correctly understand and articulate what I am trying to do, so want to confirm that my understanding of a Lookup is correct first (shown below) before moving into my actual question which is further below.
It is my understanding that a lookup field in CRM 2011 has three attributes, (1) entitytype, (2) id, and (3) name as shown below, and that the end user readable value of what is stored in the lookup field is contained in the name attribute.
Assuming that is true, and if an end user selects from the lookup the "John Smith" choice, and the field name is new_mylookup, and since it is the only lookup filed I am looking at programmatically I will simply use element [0], then am I correct in my understanding that the text "John Smith" would be contained in new_mylookup[0].name?
And if I wanted to get the GUID of the lookup field, that links back to the parent entity, I would look at new_mylookup[0].id correct?
new_mylookup[0].entitytype
new_mylookup[0].id
new_mylookup[0].name
Assuming the answer to each of those two quick pre-requisite questions' are "YES" it is now time for my actual question which asked in two parts below:
How can I edit the code below so that instead of getting the value of a standard text field, I am getting the value of the name attribute of a lookup filed in element [0] which is expected to be "John Smith"?
I am guessing that part of my problem rests in the fact that in addition to altering the syntax of how I look at the value inside the passed text field, I also have to change the data type of the field I am passing to the subroutine shown below so that I can properly access and grab the contents from the name attribute, but I am not entirely certain of what to do.
I am hoping that someone could edit the code below to set me on the right track so I know how to deal with this type of issue moving forward.
PART # 1
/**** THE FOLLOWING LINE OF CODE IS CALLED IN THE MAIN EXECUTUON CONTEXT OF MY PLUGIN ***/
ExtractNameStatusGUID(service, NameStatusResultData, "new_s_namestatusid");
/**** THE FOLLOWING LINE OF CODE IS CALLED IN THE MAIN EXECUTUON CONTEXT OF MY PLUGIN ***/
protected string ExtractNameStatusGUID(IOrganizationService ExtractGUIDService, EntityCollection PassedEntityCollection, string PassedFieldToExtract)
{
Entity e = PassedEntityCollection.Entities[0];
var Myname = e.GetAttributeValue<string>(PassedFieldToExtract);
var MyNameAsString = MyName.ToString();
return MyNameAsString;
}
I basically pass it the name of the text field and I grab the data inside of that text field.
The code works great if its a standard text field, but not if its a lookup field.
So I need to be able to pass it the name of the lookup field and get the same data from that, returning what I get as a string.
PART # 2
In the same manner, I would also like to extract the GUID value of the lookup field I pass in as PassedFieldToExtract, which I am assuming is contained inside the fieldname[0].id attribute of the PassedFieldToExtract.
The code shown below works great if I am dealing with a text field that contains a GUID, but it obviously does not work if I pass it the name of a lookup field.
/**** THE FOLLOWING LINE OF CODE IS CALLED IN THE MAIN EXECUTUON CONTEXT OF MY PLUGIN ***/
ExtractNameStatusGUID(service, NameStatusResultData, "new_s_namestatusid");
/**** THE FOLLOWING LINE OF CODE IS CALLED IN THE MAIN EXECUTUON CONTEXT OF MY PLUGIN ***/
protected string ExtractNameStatusGUID(IOrganizationService ExtractGUIDService, EntityCollection PassedEntityCollection, string PassedFieldToExtract)
{
Entity e = PassedEntityCollection.Entities[0];
var MyGUID = e.GetAttributeValue<Guid>(PassedFieldToExtract);
var MyGUIDAsString = MyGUID.ToString();
return MyGUIDAsString;
}
Any help on re-purposing this code so that it operates in accordance with my lookup value and id extraction needs would be greatly appreciated.