Set Partylist value using array in JavaScript

300 views Asked by At

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.

1

There are 1 answers

0
Alex McMillan On BEST ANSWER

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'        
    };
}