Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to get the value of a lookup field inside another lookup field

(1) ShareShare
ReportReport
Posted on by 392

Hi Guys,

   I have been given this work,to create a plugin that will send email to a field value, that is a lookup field inside another lookup field, once a record is created of an entity is created. How to I get the field value?. Example :

 string staffRole = (Role)

Role = staff name

staffname = victor.

I want to send an email to the value of staffName, which in this case is Victor. I hope, I have explained this better. Thanks guys 

*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: How to get the value of a lookup field inside another lookup field

    One more option Is it possible to use three different formula fields to get the values from the Parent object.

    If not you can write your own apex trigger for this.

    trigger myTrigger on ObjectA__c(before insert, before update){

       for(Object A obj : Trigger.new){

           if(obj.Lookupfield__c !=null){

               obj.secondLookUp = obj.LookupField__c;

               obj.thirdLookup = obj.lookupfield__c;

               obj.fourthlookup = obj.lookupfield__c;

           }

       }

    }

    Hope this helped! If so please click the Best Answer button to close out the thread.

  • Victor Onyebuchi Profile Picture
    392 on at
    RE: How to get the value of a lookup field inside another lookup field

    This fixed, Thanks Arun. But can you take a look at this other post:

    community.dynamics.com/.../290596

    The plugin still given errors. I keep trying to fix it but to no avail.

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: How to get the value of a lookup field inside another lookup field

    Hi Victor,

    Error "Unable to cast object of type..." occurs if you use a wrong type when fetching the values. In the statement "if (travelDetail.GetAttributeValue<string>("gg_bookingcomplete") == "Yes")", we are using type string whereas the field "gg_bookingcomplete" is of type "boolean".

    To fix this error message, change the statement with this "if (travelDetail.GetAttributeValue<bool>("gg_bookingcomplete"))"

    Hope this helps.

  • Victor Onyebuchi Profile Picture
    392 on at
    RE: How to get the value of a lookup field inside another lookup field

    Please look at the result below.

  • Victor Onyebuchi Profile Picture
    392 on at
    RE: How to get the value of a lookup field inside another lookup field

    I was able to retrieve(so i believe), but it throws an exception error. my code is below:

    if (travelDetail.Attributes.Contains("gg_bookingcomplete"))
                        {
                            if (travelDetail.GetAttributeValue<string>("gg_bookingcomplete") == "Yes")
                            {
    
                                Guid userId = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;
    
                                Guid travelregardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                                string regardingobject = "gg_traveldetail";
                                string emailSubject = "A travel detail has just been completed";
                                string emailBody = "Plesae refer to the completed travel that has just been completed by either a consultant or a nurse";
    
                                if (sConRef != null)
                                {
    
                                    //Entity image = (Entity)context.PreEntityImages["ImageDist"];
    
                                    Entity conRef = service.Retrieve(sConRef.LogicalName, sConRef.Id, new ColumnSet(new string[] { "new_consultant" }));
    
                                    EntityReference con = conRef.GetAttributeValue<EntityReference>("new_consultant");
    
                                    Entity consultant = service.Retrieve(con.LogicalName, con.Id, new ColumnSet(new string[] { "new_name", "emailaddress" }));
    
                                    //Entity email = new Entity("email");
                                    SendEmail(service, travelDetail, consultant, consultant.Id, userId, travelregardingobjectid, emailBody, emailSubject, regardingobject);
                                }
    
                                if (sNurRef != null)
                                {
                                    Entity nurRef = service.Retrieve(sNurRef.LogicalName, sNurRef.Id, new ColumnSet(new string[] { "new_nurse" }));
                                    EntityReference nur = nurRef.GetAttributeValue<EntityReference>("new_nurse");
                                    Entity nurse = service.Retrieve(nur.LogicalName, nur.Id, new ColumnSet(new string[] { "new name", "emailaddress" }));
                                    SendEmail(service, travelDetail, nurse, nurse.Id, userId, travelregardingobjectid, emailBody, emailSubject, regardingobject);
                                }
                            }
                        }
                    }

    the error is at this line :

    if (travelDetail.GetAttributeValue<string>("gg_bookingcomplete") == "Yes")

    that field "gg_bookingcomplete" is a type TWO OPTIONs and the error is - "Unable to cast object of type 'System.Boolean' to type 'System.String'"

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: How to get the value of a lookup field inside another lookup field

    Hi,

    I hope Rawish's answer help you implement your requirement, if not please share screentshots of the entity, I hope you have Role and Staff realted as 1:N relationship if yes above code should help you, otherwise share some more details

  • Verified answer
    Rawish Kumar Profile Picture
    13,756 on at
    RE: How to get the value of a lookup field inside another lookup field

    Hi There,

    Here you go!

    var firstlookup = (EntityReference)(entity.Attributes["firstlookup"]);

    var retrievedRecord= service.Retrieve(firstlookup .LogicalName, firstlookup .Id, new ColumnSet(true));

    var fieldtoretrieve = (EntityReference)retrievedRecord ["second_lookup"]);

    fieldtoretrieve  will have the entity reference(lookup) that you wanted.

  • Victor Onyebuchi Profile Picture
    392 on at
    RE: How to get the value of a lookup field inside another lookup field

    Role is a lookup and staff name is also a lookup field. 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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,865 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans