Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Get PartyList Type using JavaScipt

Posted on by 215

I'm trying to add some JavaScript to the "to" field's onChange event of the Phone Call activity.

I would like to grab the first entry in the "to" field and determine if it is an Account or Contact for the purpose of getting some data from the respective record.

It appears I cannot get the "type".  In line 7 below my alert shows "undefined".  If I change the alert to "members[0].id" then the GUID displays.  So I know it is getting there.  But "type" doesn't work.  And the subsequent IF statements never work.  I am choosing an account or contact while testing.

Is there something I am missing here?  I've researched online and other people have posted that "members[i].type" is the correct way to get the entity type.

Thank you in advance for any help you may have.

John.

function getparty(executionContext) {
    var formContext = executionContext.getFormContext();

    var party = formContext.getAttribute("to");
    var members = party.getValue();
  
      alert(members[0].type);

      // Contact
      if (members[0].type == 2) {
        alert("Contact Id-" + members[0].id + " Name-" + members[0].name);
        //Entity name
        var sEntityName = "contact";
     
        // Select field names:
        var sColumns = "telephone1";
        
        // Where clause filter
        var sFilter = "contactid eq " + members[0].id;
        
        var sOrderBy = "";
 
        //Call the retrieve function        
        var requestResults  = retrieveWebAPIData(sEntityNamesColumnssFiltersOrderBy);

        //Loop through the results (there should only be one in this case): 
        if (requestResults != null) {
            if (requestResults.value != null && requestResults.value.length > 0) {
                for (var indRecordLoop = 0indRecordLoop < requestResults.value.lengthindRecordLoop++) {
                    var oContact = requestResults.value[indRecordLoop];
                    //Check returned data field for a value:
                    
                    if(oContact.telephone1)
                    {
                        Xrm.Page.getAttribute("phonenumber").setValue(oContact.telephone1);
                    }
                }
            }
        }
      }
      // Account
      if (members[0].type == 1) {

           alert("Account Id-" + members[0].id + " Name-" + members[0].name);

        //Entity name
        var sEntityName = "account";
     
        // Select field names:
        var sColumns = "telephone1";
        
        // Where clause filter
        var sFilter = "contactid eq " + members[0].id;
        
        var sOrderBy = "";
 
        //Call the retrieve function        
        var requestResults  = retrieveWebAPIData(sEntityNamesColumnssFiltersOrderBy);

        //Loop through the results (there should only be one in this case): 
        if (requestResults != null) {
            if (requestResults.value != null && requestResults.value.length > 0) {
                for (var indRecordLoop = 0indRecordLoop < requestResults.value.lengthindRecordLoop++) {
                    var oContact = requestResults.value[indRecordLoop];
                    //Check returned data field for a value:
                    
                    if(oContact.telephone1)
                    {
                        Xrm.Page.getAttribute("phonenumber").setValue(oContact.telephone1);
                    }
                }
            }
        }
    }
}

  function retrieveWebAPIData(sEntityNamesSelectsFiltersOrderBy) {
 
    var serverUrl = Xrm.Page.context.getClientUrl();
    var ODATA_ENDPOINT = "/api/data/v9.0";
    var odataUri = serverUrl + ODATA_ENDPOINT + "/" + sEntityName + "?";
    if (sSelect) {
        odataUri += "$select=" + sSelect;
    }
     
    if (sFilter) {
        odataUri += "&$filter=" + sFilter;
    }
     
    if (sOrderBy){
        odataUri += "&$orderby=" + sOrderBy;
    }
    
    var data;
    
    try
    { 
        
        var req = new XMLHttpRequest();
        //alert(odataUri);
        
        req.open("GET"encodeURI(odataUri), false);
        
        req.setRequestHeader("Accept""application/json");
        req.setRequestHeader("Content-Type""application/json; charset=utf-8");        
        req.send(null);
        
        data = JSON.parse(req.responseTextdateReviver);
        
        return data;
    }
    catch (ex){
        alert("Error: retrieveWebAPIData - Data Set =" + odataSetName + "; filter = " + sFilter + "; select = " + sSelect + "; Error = " + ex.message);
    }
}
 
  • John Cogovan Profile Picture
    John Cogovan 215 on at
    RE: Get PartyList Type using JavaScipt

    Thank you Ankit.  I never considered the UI vs. Classic UI were different.  Your solution paved my way to success.

  • meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: Get PartyList Type using JavaScipt

    Sorry, this isn't my post. I just tested your suggested solution.

    Have a good one

  • RE: Get PartyList Type using JavaScipt

    Glad to hear that!!! It would be great if you mark above POST as verified!!!

  • meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: Get PartyList Type using JavaScipt

    it works yes !

  • Verified answer
    RE: Get PartyList Type using JavaScipt

    Hy Partner,

    Hope this finds you well. I have double checked the SDK for classic UI and Unified Interface.

    Below is my code snippet :-

    function getparty(executionContext) {
    debugger;
    var formContext = executionContext.getFormContext();

    var party = formContext.getAttribute("to");
    var members = party.getValue();
    alert (members[0].type);
    }

    In classic UI it is working fine as expected. 

    In Unified Classic Interface SDk has been changed and when retrieve the members, object contains field as entityType. So it might be the case that in classic interface your code is working fine, but in order to get it worked in UCI, intead of member[0].type you need to write members[0].entityType which returns the entity name as follows :-

    pastedimage1579856446724v1.png

    Hope this helps.

    Best regards,

    Ankit

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans