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 :
Customer experience | Sales, Customer Insights,...
Answered

How do I populate lookup fields from staging fields via automation?

(0) ShareShare
ReportReport
Posted on by 25

So, I have a custom entity, called 'Issues'. Each issue can have a Client (contactType1) with a unique ID in its own field on the Contact table (ID1), and a Vendor (contactType2) with another unique ID in a different field on the Contact table (ID2).

I have some automations that create issues based on an import from SQL each night. the ID1 and ID2 are brought in as text fields.

On my Issues main form, I have a Client name, which is a lookup to the Contact table, and a Vendor name, which is also a lookup to the Contact table.

When the new issue is created, I want to look at ID1 and attach the record to contactType1 in the contact table on the client field. Likewise, to take ID2, find the corresponding vendor in the Contact entity, and pull that name in. It seems like it should be do-able, but I am new to Dynamics, and have been struggling with this for a while that I think I have thought myself in circles, as it were.

If this is possible without JS, that would be great, as I am not as familiar with Javascript

Any help appreciated!

I have the same question (0)
  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    I have written the code for you.

    To replicate SQL Staging, I have created below class and added few dummy data -

    public class Issue

    {

          public string IssueName { get; set; }

          public string ID1 { get; set; }

          public string ID2 { get; set; }

    }

    // Create Issue Records Coming from SQL

    List<Issue> issues = new List<Issue>();

    issues.Add(new Issue { IssueName = "Test Issue 1", ID1 = "1010", ID2 = "1011" });

    issues.Add(new Issue { IssueName = "Test Issue 2", ID1 = "1012", ID2 = "1013" });

    Now I am loping through each record and creating issue entity record in dynamics with ClientName and Vendor Name lookup. See below code -

    foreach (var item in issues)
                    {
    
                        // Create Issue Record in CRM
                        Entity issueEntity = new Entity("new_issue");
                        issueEntity["new_name"] = item.IssueName;
    
                        if (!string.IsNullOrEmpty(item.ID1))
                        {
                            Guid clientContactId = GetContactByUniqueID(item.ID1, crmSvc);
                            if (clientContactId != Guid.Empty)
                            {
                                issueEntity["new_clientnamee"] = new EntityReference("contact", clientContactId);
                            }
                        }
    
                        if (!string.IsNullOrEmpty(item.ID2))
                        {
                            Guid vendorContactId = GetContactByUniqueID(item.ID2, crmSvc);
                            if (vendorContactId != Guid.Empty)
                            {
                                issueEntity["new_vendornamee"] = new EntityReference("contact", vendorContactId);
                            }
                        }
    
                        crmSvc.Create(issueEntity);
                    }

    GetContactByUniqueID Method code is below which will get the contact record based on ID from sql

    private static Guid GetContactByUniqueID(string uniqueID, IOrganizationService service)
            {
                Guid contactId = Guid.Empty;
    
                string queryContact = $@"
                                          
                                            
                                            
                                            
                                            
                                            
                                              
                                              
                                            
                                          
                                        ";
    
                EntityCollection contacts = service.RetrieveMultiple(new FetchExpression(queryContact));
                if (contacts != null && contacts.Entities.Count > 0)
                {
                    contactId = contacts.Entities[0].Id;
                }
                return contactId;
            }

    Good Luck!

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Suggested answer
    shollis2814 Profile Picture
    25 on at

    Thanks. I did manage to do it from Power Automate, but I believe this solution would work as well. Thank you!

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans