web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Name Attribute for lookup field null

(0) ShareShare
ReportReport
Posted on by

i am working on a plugin where i need to retrieve both the GUID and name attribute of a lookup field. i am able to retrieve the GUID with no issue, but when i try and get the name it is returning null, beloe is my code, any help would be greatly appreciated 

string NewAccType = string.Empty; //creat + set zipcode var
if (account.Contains("ptus_companytypeid")) //check if populated
{
tracingservice.Trace(account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id.ToString());
NewAccType = account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id.ToString();
tracingservice.Trace(account.GetAttributeValue<EntityReference>("ptus_companytypeid").Name.ToString());
String NewAccTypeName = account.GetAttributeValue<EntityReference>("ptus_companytypeid").Name.ToString();

I did some reading and someone mention that i need to request the name attribute, but i am unsure how to do that?

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I'm not 100% but you can try to register post-image for your step and try to get name from image instead trying to that from target.

  • Brekh Profile Picture
    192 on at

    Hi,

    Typecast the field to EntityReference and get the Name. This should work.

    String NewAccTypeName = ((EntityReference)account.Attributes["ptus_companytypeid"]).Name;

  • Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    Can you share your full code here for inquiry it will help to provide you best possible solution. thanks

  • Community Member Profile Picture
    on at

    ofcourse, full code is below

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace SET_FSM_Final

    {

       public class Set_FSM : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               //************************************stuff needed for plugin to work**********************************

               var context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;

               if (context.Stage != 20)

                   throw new InvalidPluginExecutionException("must run as pre-operation stage 20");

               if (context.MessageName != "Create")

                   throw new InvalidPluginExecutionException("must run on create");

               if (context.PrimaryEntityName != "account")

                   throw new InvalidPluginExecutionException("must run agianst account entity");

               var tracingservice = serviceProvider.GetService(typeof(ITracingService)) as ITracingService;

               IOrganizationServiceFactory serviceFactory = serviceProvider.GetService(typeof(IOrganizationServiceFactory)) as IOrganizationServiceFactory;

               IOrganizationService service = serviceFactory.CreateOrganizationService(null);

               //************************************Start the Main Section**********************************

               tracingservice.Trace("Hello from the plugin dude");

               tracingservice.Trace("lets get our guid, shall we?");

               Entity account = context.InputParameters["Target"] as Entity;

               string NewAccType = Get_Account_Type(tracingservice, account);

               tracingservice.Trace("select the account entity");

               if (NewAccType == "55cdb627-9703-e211-8be2-78e3b510fddb")

               {

                   Set_Supplier_FSM(tracingservice, service, account);

               }

               else if (NewAccType == "add5b921-9703-e211-8be2-78e3b510fddb")

               {

                   Set_Repairer_FSM(tracingservice, service, account);

               }

               else

                   return;

           }

           private static string Get_Account_Type(ITracingService tracingservice, Entity account)

           {

               string NewAccType = string.Empty; //creat + set zipcode var

               if (account.Contains("ptus_companytypeid")) //check if populated

               {

                   tracingservice.Trace(account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id.ToString());

                   NewAccType = account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id.ToString();                

                   string NewAccTypeName = ((EntityReference)account.Attributes["ptus_companytypeid"]).Name;

                   tracingservice.Trace("this is the account type"  + NewAccTypeName);

               }

               return NewAccType;

           }

           private static void Set_Repairer_FSM(ITracingService tracingservice, IOrganizationService service, Entity account)

           {

               string ZipCode = string.Empty; //creat + set zipcode var

               if (account.Contains("address1_postalcode")) //check if populated

                   ZipCode = account.GetAttributeValue<string>("address1_postalcode");

               tracingservice.Trace("query FSMUpdate entity "); //**trace ask for the stuff you want from the place you need**

               tracingservice.Trace("start of query expression"); //**start of query**

               var query2 = new QueryExpression("ptus_fsmipdate"); //set the entity to query

               tracingservice.Trace("select entity ptus_fsmipdate");

               query2.ColumnSet.AddColumn("ptus_fsm"); //select the user field

               tracingservice.Trace("select the ptus_fsm field");

               query2.Criteria.AddCondition("ptus_name", ConditionOperator.Equal, ZipCode); //only select when zipcode in account = zipcode in update entity

               tracingservice.Trace("select when zipcodes match");

               var result2 = service.RetrieveMultiple(query2); //set the result

               tracingservice.Trace("set result");

               if (result2.Entities.Count > 0) //check to see if results > 0

               {

                   var r2 = result2.Entities[0]; //select the guid

                   account["ptus_fieldrelationshipmanagerid"] = ((EntityReference)r2.Attributes["ptus_fsm"]); //set the fsm field on account from fsm on update

               }

           }

           private static void Set_Supplier_FSM(ITracingService tracingservice, IOrganizationService service, Entity account)

           {

               string ZipCode = string.Empty; //creat + set zipcode var

               if (account.Contains("address1_postalcode")) //check if populated

                   ZipCode = account.GetAttributeValue<string>("address1_postalcode");

               tracingservice.Trace("query FSMUpdate entity "); //**trace ask for the stuff you want from the place you need**

               tracingservice.Trace("start of query expression"); //**start of query**

               var query2 = new QueryExpression("new_supplierfsmupdate"); //set the entity to query

               tracingservice.Trace("select entity ptus_fsmipdate");

               query2.ColumnSet.AddColumn("new_fsm"); //select the user field

               tracingservice.Trace("select the ptus_fsm field");

               query2.Criteria.AddCondition("new_name", ConditionOperator.Equal, ZipCode); //only select when zipcode in account = zipcode in update entity

               tracingservice.Trace("select when zipcodes match");

               var result2 = service.RetrieveMultiple(query2); //set the result

               tracingservice.Trace("set result");

               if (result2.Entities.Count > 0) //check to see if results > 0

               {

                   var r2 = result2.Entities[0]; //select the guid

                   account["ptus_fieldrelationshipmanagerid"] = ((EntityReference)r2.Attributes["new_fsm"]); //set the fsm field on account from fsm on update

               };

           }

       }

    }

  • Community Member Profile Picture
    on at

    just tried this and the name is still coming back as null.

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    You can make an additional call to retrieve name for the lookup field entity like following

    Entity entitydetails = service.Retrieve("NAMEOFENTITY", account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id, new ColumnSet(true));

    string NewAccTypeName=entitydetails.GetAttributeValue<String>("NAMEOFPrimaryField");

    Replace highlighted fields with entity name and primary attribute name, you can replace below line with above two lines

    string NewAccTypeName = ((EntityReference)account.Attributes["ptus_companytypeid"]).Name;

    Let me know if it's not working.

    Thanks

  • Community Member Profile Picture
    on at

    getting the following error

    <Message>account With Id = add5b921-9703-e211-8be2-78e3b510fddb Does Not Exist</Message>

  • Mahendar Pal Profile Picture
    45,095 on at

    It seems you are writing wrong entity name, make sure you need to write entity name of the lookup field, for example the lookup is from prefix_demo entity then you need to mention this entity under

    Entity entitydetails = service.Retrieve("NAMEOFENTITY", account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id, new ColumnSet(true));

    Thanks

  • Community Member Profile Picture
    on at

    when i update my code with your suggestion i get the error i mentioned above

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace SET_FSM_Final

    {

       public class Set_FSM : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               //************************************stuff needed for plugin to work**********************************

               var context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;

               if (context.Stage != 20)

                   throw new InvalidPluginExecutionException("must run as pre-operation stage 20");

               if (context.MessageName != "Create")

                   throw new InvalidPluginExecutionException("must run on create");

               if (context.PrimaryEntityName != "account")

                   throw new InvalidPluginExecutionException("must run agianst account entity");

               var tracingservice = serviceProvider.GetService(typeof(ITracingService)) as ITracingService;

               IOrganizationServiceFactory serviceFactory = serviceProvider.GetService(typeof(IOrganizationServiceFactory)) as IOrganizationServiceFactory;

               IOrganizationService service = serviceFactory.CreateOrganizationService(null);

               //************************************Start the Main Section**********************************

               tracingservice.Trace("Hello from the plugin dude");

               tracingservice.Trace("lets get our guid, shall we?");

               Entity account = context.InputParameters["Target"] as Entity;

               string NewAccType = Get_Account_Type(tracingservice, account, service);

               tracingservice.Trace("select the account entity");

               if (NewAccType == "55cdb627-9703-e211-8be2-78e3b510fddb")

               {

                   Set_Supplier_FSM(tracingservice, service, account);

               }

               else if (NewAccType == "add5b921-9703-e211-8be2-78e3b510fddb")

               {

                   Set_Repairer_FSM(tracingservice, service, account);

               }

               else

                   return;

           }

           private static string Get_Account_Type(ITracingService tracingservice, Entity account, IOrganizationService service)

           {

               string NewAccType = string.Empty; //creat + set zipcode var

               if (account.Contains("ptus_companytypeid")) //check if populated

               {

                   tracingservice.Trace(account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id.ToString());

                   NewAccType = account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id.ToString();

                   Entity entitydetails = service.Retrieve("account", account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id, new ColumnSet(true));

                   string NewAccTypeName = entitydetails.GetAttributeValue<String>("ptus_companytypeid");

                   tracingservice.Trace(NewAccTypeName);

                   //tracingservice.Trace(account.GetAttributeValue<EntityReference>("ptus_companytypeid").Name.ToString());

                   //String NewAccTypeName = account.GetAttributeValue<EntityReference>("ptus_companytypeid").Name.ToString();

               }

               return NewAccType;

           }

           private static void Set_Repairer_FSM(ITracingService tracingservice, IOrganizationService service, Entity account)

           {

               string ZipCode = string.Empty; //creat + set zipcode var

               if (account.Contains("address1_postalcode")) //check if populated

                   ZipCode = account.GetAttributeValue<string>("address1_postalcode");

               tracingservice.Trace("query FSMUpdate entity "); //**trace ask for the stuff you want from the place you need**

               tracingservice.Trace("start of query expression"); //**start of query**

               var query2 = new QueryExpression("ptus_fsmipdate"); //set the entity to query

               tracingservice.Trace("select entity ptus_fsmipdate");

               query2.ColumnSet.AddColumn("ptus_fsm"); //select the user field

               tracingservice.Trace("select the ptus_fsm field");

               query2.Criteria.AddCondition("ptus_name", ConditionOperator.Equal, ZipCode); //only select when zipcode in account = zipcode in update entity

               tracingservice.Trace("select when zipcodes match");

               var result2 = service.RetrieveMultiple(query2); //set the result

               tracingservice.Trace("set result");

               if (result2.Entities.Count > 0) //check to see if results > 0

               {

                   var r2 = result2.Entities[0]; //select the guid

                   account["ptus_fieldrelationshipmanagerid"] = ((EntityReference)r2.Attributes["ptus_fsm"]); //set the fsm field on account from fsm on update

               }

           }

           private static void Set_Supplier_FSM(ITracingService tracingservice, IOrganizationService service, Entity account)

           {

               string ZipCode = string.Empty; //creat + set zipcode var

               if (account.Contains("address1_postalcode")) //check if populated

                   ZipCode = account.GetAttributeValue<string>("address1_postalcode");

               tracingservice.Trace("query FSMUpdate entity "); //**trace ask for the stuff you want from the place you need**

               tracingservice.Trace("start of query expression"); //**start of query**

               var query2 = new QueryExpression("new_supplierfsmupdate"); //set the entity to query

               tracingservice.Trace("select entity ptus_fsmipdate");

               query2.ColumnSet.AddColumn("new_fsm"); //select the user field

               tracingservice.Trace("select the ptus_fsm field");

               query2.Criteria.AddCondition("new_name", ConditionOperator.Equal, ZipCode); //only select when zipcode in account = zipcode in update entity

               tracingservice.Trace("select when zipcodes match");

               var result2 = service.RetrieveMultiple(query2); //set the result

               tracingservice.Trace("set result");

               if (result2.Entities.Count > 0) //check to see if results > 0

               {

                   var r2 = result2.Entities[0]; //select the guid

                   account["ptus_fieldrelationshipmanagerid"] = ((EntityReference)r2.Attributes["new_fsm"]); //set the fsm field on account from fsm on update

               };

           }

       }

    }

  • Mahendar Pal Profile Picture
    45,095 on at

    Hello,

    I am assuming in your account entity you have lookup of another entity, so you need to mention your lookup entity name here instead of account

    Entity entitydetails = service.Retrieve("account", account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id, new ColumnSet(true));  

    Let me know if my understanding is not correct.

    Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans