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 365 | Integration, Dataverse...
Answered

Concat 2 lookup field values

(4) ShareShare
ReportReport
Posted on by 5,514
How to CONCAT or join 2 lookup field values 
 
I have a ab field in bbb entity which is multi select lookup.
on click of ribbon button in bbb entity A xxx entity page is opening and  the value of ab is copying in "cd" field in xxx entity[Working as Expected].
 
I have Another lookup which is List field in xx entity and
merge

 
Any Suggestions will be marked as verified soon
 
Thanks
Sandeep
Categories:
I have the same question (0)
  • Verified answer
    Holly Huffman Profile Picture
    6,538 Super User 2025 Season 2 on at
    Good morning, afternoon, or evening depending on your location!
     
    Some suggestions, if you haven't tried already -
     
    Fixes & Improvements:
    1. Ensure List is properly parsed
      • You are using JSON.parse(List), but List might not always be a valid JSON string. Consider checking its type before parsing.
    2. Fix variable name typo
      • You are using dParsed[i].id, but dParsed is not defined. It should be dListParsed[i].id.
    3. Ensure To field is correctly updated
      • Instead of overwriting the To field, append new values while preserving existing ones.
     
    Updated Code:
    async function setDistributionList(executionContext) {
        var formContext = executionContext.getFormContext();
        var List = formContext.getAttribute("c30_list").getValue();
        var To = formContext.getAttribute("c30_to").getValue();
       
        const oContacts = [];
        const dContacts = [];

    // Process existing "To" field values
        if (To !== null) {
            const ToArray = To.split(',');
            for (var i = 0; i < ToArray.length; i++) {
                var contactId = ToArray[i].trim();
                var fetchXML = `<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                    <entity name='contact'>
                        <attribute name='name' />
                        <attribute name='contactid' />
                        <filter type='and'>
                            <condition attribute='contactid' operator='eq' value='${contactId}' />
                        </filter>
                    </entity>
                </fetch>`;
               
                await Xrm.WebApi.retrieveMultipleRecords("contact", "?fetchXml=" + fetchXML).then(
                    function success(result) {
                        result.entities.forEach(entity => {
                            oContacts.push({ id: entity.contactid, name: entity.name, entityType: "contact" });
                        });
                    }
                );
            }
        }

    // Process List field values
        if (List !== null) {
            try {
                const dListParsed = JSON.parse(List);
                for (var i = 0; i < dListParsed.length; i++) {
                    var dListName = dListParsed[i].id;
                    var fetchXML = `<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
                        <entity name='contact'>
                            <attribute name='name' />
                            <attribute name='contactid' />
                            <filter type='and'>
                                <condition attribute='c30_to' operator='eq' value='1' />
                            </filter>
                            <link-entity name='c30_c30_dlist_contact' from='contactid' to='contactid' intersect='true'>
                                <link-entity name='c30_dlist' from='c30_dlistid' to='c30_dlistid' alias='ag'>
                                    <filter type='and'>
                                        <condition attribute='c360_dlistid' operator='eq' value='${dListName}' />
                                    </filter>
                                </link-entity>
                            </link-entity>
                        </entity>
                    </fetch>`;

    await Xrm.WebApi.retrieveMultipleRecords("contact", "?fetchXml=" + fetchXML).then(
                        function success(result) {
                            result.entities.forEach(entity => {
                                dContacts.push({ id: entity.contactid, name: entity.name, entityType: "contact" });
                            });
                        }
                    );
                }
            } catch (error) {
                console.error("Error parsing List field:", error);
            }
        }

    // Concatenate values without overwriting
        formContext.getAttribute("c30_to").setValue([...oContacts, ...dContacts]);
    }

    Key Fixes:
    • Preserves existing values in the To field instead of overwriting.
    • Fixes variable name typo (dParsed → dListParsed).
    • Handles JSON parsing errors gracefully.
    • Ensures proper trimming of values before processing.
     
    Hope this is helpful :)
  • Verified answer
    Jeevarajan Kumar Profile Picture
    781 Most Valuable Professional on at
    Hi Sandeep,

    This is similar to the other one, but we have a few common issues. 1. Dont do JSON parse, use the object directly. 2. Don't use Split, its not a string as its an object. 3. Key issue here is the concat, try something like this rather
     
        var List = formContext.getAttribute("c30_list").getValue();
        var dListParsed = Array.isArray(List) ? List : [];
    
        var To = formContext.getAttribute("to").getValue();
        var oContacts = Array.isArray(To) ? To : [];
    
       //code to retrieve and populate dContacts Array
    
       // do the merge like the other one
    
       var mergedContacts = [...oContacts];
        dContacts.forEach(function (contact) {
            if (!mergedContacts.find(c => c.id === contact.id)) {
                mergedContacts.push(contact);
            }
        });
    
      // Set the final merged TO value
        formContext.getAttribute("to").setValue(mergedContacts);

    Kindly mark my answer as Verified if this works

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 365 | Integration, Dataverse, and general topics

#1
iampranjal Profile Picture

iampranjal 41

#2
Martin Dráb Profile Picture

Martin Dráb 36 Most Valuable Professional

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans