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

(2) ShareShare
ReportReport
Posted on by 5,388
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:
  • Verified answer
    Jeevarajan Kumar Profile Picture
    769 Most Valuable Professional on at
    Concat 2 lookup field values
    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
  • Verified answer
    Holly Huffman Profile Picture
    5,917 on at
    Concat 2 lookup field values
    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 :)

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March 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... 294,261 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,017 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans