locked
CRM 2011 Get Lookup Text Value RRS feed

  • Question

  • Dear All,

    I wrote a plugin. I am trying to create account category record after creating account where I need one lookup Guid and the Text Value. I am getting Guid value. But lookup text value also i need to retrieve. How to get this text value ? I wrote the following code getting error on the bold line. Kindly help me.

                Entity account = context.InputParameters["Target"] as Entity;
                Guid accountid = (Guid)context.OutputParameters["id"];
                EntityReference PrimCategory = account.Attributes["new_primarycategoryid"] as EntityReference;
                if (account.Attributes.Contains("new_primarycategoryid") && (accountid != null))
                {
                    Entity AccCategory = new Entity("new_accountcategory");
                     AccCategory.Attributes = new AttributeCollection();

                    AccCategory.Attributes.Add("new_name", ((EntityReference)PrimCategory).Name);

                    AccCategory.Attributes.Add("new_accountid", new EntityReference("account", accountid));
                    AccCategory.Attributes.Add("new_accountareaofbusinessid", new EntityReference("new_primarycategoryid", PrimCategory.Id));
                    service.Create(AccCategory);

           }

     
    • Edited by crmpnk1 Saturday, April 6, 2013 4:52 AM
    Saturday, April 6, 2013 4:51 AM

Answers

  • Hi,
    normally the Name attribute contains the string value of the primary attribute, but happened also to me that sometimes the string returns null (specially working with activityparty).
    In this case you need to manually retrieve the value, take this code for example:

    Entity account = context.InputParameters["Target"] as Entity;
    Guid accountid = (Guid)context.OutputParameters["id"];
    
    // need to cast the entity reference after you check the attribute value
    if (account.Attributes.Contains("new_primarycategoryid") && account.Attributes["new_primarycategoryid"] != null && accountid != null) {
       EntityReference PrimCategory = account.Attributes["new_primarycategoryid"] as EntityReference;
       Entity AccCategory = new Entity("new_accountcategory");
       AccCategory.Attributes = new AttributeCollection();
       
       // retrieve the primary attribute from new_primarycategoryid entity
       Entity primaryCategory = service.Retrieve("new_primarycategory", PrimCategory.Id, new ColumnSet(true));
       string name = primaryCategory.Attributes["new_name"];
     
       AccCategory.Attributes.Add("new_name", name);
       AccCategory.Attributes.Add("new_accountid", new EntityReference("account", accountid));
       AccCategory.Attributes.Add("new_accountareaofbusinessid", new EntityReference("new_primarycategoryid", PrimCategory.Id));
       
       service.Create(AccCategory);
    }


    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Sunday, April 7, 2013 12:01 PM
    • Marked as answer by crmpnk1 Tuesday, April 9, 2013 5:03 AM
    Sunday, April 7, 2013 9:14 AM
  • I believe it has something to do with the way the platform passes the entity state into the Execute method of the plug-in. To simplify the call, you can retrieve the record from CRM again and only retrieve the EntityReference attribute you need. Once you have the Entity object, you can merge it with the target so you have everything you need in one variable.

    I hope that helps,

    Roshan


    Roshan http://www.magnetismsolutions.com

    • Marked as answer by crmpnk1 Tuesday, April 9, 2013 5:03 AM
    Monday, April 8, 2013 8:45 AM

All replies

  • Hi,

    Use the following code and try.

    if(PrimCategory != null)
    {
    AccCategory.Attributes.Add("new_name", PrimCategory.Name);
    }

    --

    Thanks and Regards,

    Gopinath.

    Osmosys

    • Proposed as answer by Makarand Keer Saturday, April 6, 2013 8:38 PM
    Saturday, April 6, 2013 5:34 AM
  • Hi,

    Thanks for the reply. it's not working. It gives always null though the value is there. I have tried in this way.If you observe third line its giving Id but name is giving always null.

    AccCategory.Attributes.Add("new_name", ((EntityReference)PrimCategory).Name);

    AccCategory.Attributes.Add("new_accountid", new EntityReference("account", accountid));
    AccCategory.Attributes.Add("new_accountareaofbusinessid", new EntityReference("new_primarycategoryid", PrimCategory.Id));

    Regards

    Sunday, April 7, 2013 6:36 AM
  • Hi,
    normally the Name attribute contains the string value of the primary attribute, but happened also to me that sometimes the string returns null (specially working with activityparty).
    In this case you need to manually retrieve the value, take this code for example:

    Entity account = context.InputParameters["Target"] as Entity;
    Guid accountid = (Guid)context.OutputParameters["id"];
    
    // need to cast the entity reference after you check the attribute value
    if (account.Attributes.Contains("new_primarycategoryid") && account.Attributes["new_primarycategoryid"] != null && accountid != null) {
       EntityReference PrimCategory = account.Attributes["new_primarycategoryid"] as EntityReference;
       Entity AccCategory = new Entity("new_accountcategory");
       AccCategory.Attributes = new AttributeCollection();
       
       // retrieve the primary attribute from new_primarycategoryid entity
       Entity primaryCategory = service.Retrieve("new_primarycategory", PrimCategory.Id, new ColumnSet(true));
       string name = primaryCategory.Attributes["new_name"];
     
       AccCategory.Attributes.Add("new_name", name);
       AccCategory.Attributes.Add("new_accountid", new EntityReference("account", accountid));
       AccCategory.Attributes.Add("new_accountareaofbusinessid", new EntityReference("new_primarycategoryid", PrimCategory.Id));
       
       service.Create(AccCategory);
    }


    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Sunday, April 7, 2013 12:01 PM
    • Marked as answer by crmpnk1 Tuesday, April 9, 2013 5:03 AM
    Sunday, April 7, 2013 9:14 AM
  • Hi,

    Thanks for the reply. So no way we can't get the lookup text value other than explicit call to org service again.

    Regards,

    Monday, April 8, 2013 7:57 AM
  • yes,

    its the safest way


    Hope this helps. ----------------------------------------------------------------------- Santosh Bhagat If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Monday, April 8, 2013 8:09 AM
  • hi,

    I have changed your code a little bit...try this....

     Entity account = context.InputParameters["Target"] as Entity;
                Guid accountid = (Guid)context.OutputParameters["id"];

                if (account.Attributes.Contains("new_primarycategoryid") && (accountid != null))
                {

    string PrimCategory = ((EntityReference)account["new_primarycategoryid"]).Name;

                    Entity AccCategory = new Entity("new_accountcategory");
                     AccCategory.Attributes = new AttributeCollection();
                    AccCategory["new_name"]=PrimCategory;
                     AccCategory.Attributes.Add("new_accountid", new EntityReference("account", accountid));
                    AccCategory.Attributes.Add("new_accountareaofbusinessid", new EntityReference("new_primarycategoryid", PrimCategory.Id));
                    service.Create(AccCategory);

           }

    if helpful, vote as helpful and mark as answer...

    Monday, April 8, 2013 8:34 AM
  • I believe it has something to do with the way the platform passes the entity state into the Execute method of the plug-in. To simplify the call, you can retrieve the record from CRM again and only retrieve the EntityReference attribute you need. Once you have the Entity object, you can merge it with the target so you have everything you need in one variable.

    I hope that helps,

    Roshan


    Roshan http://www.magnetismsolutions.com

    • Marked as answer by crmpnk1 Tuesday, April 9, 2013 5:03 AM
    Monday, April 8, 2013 8:45 AM
  • Hello Sir,
    can you please tell how to get the lookup field text value using an explicit call to org service?

    Regards



    • Edited by Yogi Kardak Thursday, November 21, 2013 6:54 PM
    Thursday, November 21, 2013 6:52 PM