简体   繁体   中英

Set Partylist value using array in JavaScript

I'm trying to set the value of the partylist from an array, which formatted from selected contacts. But I got exception indxIds is undefined, tried a lot to figure it out but couldn't do so. Here is in the following what I'm trying to do:

//arrIds is the array of guids of selected contacts

var partyList = new Array();

for (var indxIds = 0; indxIds < arrIds.length; indxIds++) {
    partyList[indxIds ] = new Object();
    partyList[indxIds].id = arrIds[indxids]; 
    partyList[indxIds].name = selectedname[indxids].Name; 
    partyList[indxIds].typename= 'contact';         
}

Xrm.Page.getAttribute("to").setValue(partyList);

I need your kind help where I'm doing wrong.

Javascript is case-sensitive, so indxIds and indxids are considered 2 different variables.

You are defining indxIds in your for loop, but using indxids (which is undefined ) to index your arrIds and selectedname arrays.

In addition Dynamics CRM lookups require entityType and not typename

Try:

for (var indxIds = 0; indxIds < arrIds.length; indxIds++) {
    partyList[indxIds] = new Object();
    partyList[indxIds].id = arrIds[indxIds]; 
    partyList[indxIds].name = selectedname[indxIds].Name; 
    partyList[indxIds].entityType = 'contact';         
}

or even better, you can use an object literal:

for (var i = 0; i < arrIds.length; i++) {
    partyList[i] = {
        id: arrIds[i], 
        name: selectedname[i].Name,
        entityType: 'contact'        
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM