web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve fields from related contact via JScript

(0) ShareShare
ReportReport
Posted on by

Trying to retrieve some fields from the related contact record on a lead, but get the error "not defined at eval" can anyone tell me what i have wrong please?

function setContactFields() {

    //Get the contact lookup off of the record
    var contact = Xrm.Page.getAttribute('primarycontactid').getValue();

    //if contact exist, attempt to pull back the contact record
    if (contact != null && contact[0].entityType == "contact") {

        var contactId = contact[0].id;
        var serverUrl;

        if (Xrm.Page.context.getClientUrl !== undefined) {
            serverUrl = Xrm.Page.context.getClientUrl();
        }
        else {
            serverUrl = Xrm.Page.context.getServerUrl();
        }

        var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
        var contactRequest = new XMLHttpRequest();

        contactRequest.open("GET", ODataPath + "/ContactSet(guid'" + contactId + "')", false);
        contactRequest.setRequestHeader("Accept", "application/json");
        contactRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");

        contactRequest.send();

        //If request was successful, parse the associated contact account name

        if (contactRequest.status == 200) {

            var retrievedContact = JSON.parse(contactRequest.responseText).d;

            if (retrievedContact != null) {

                var retJobTitle = retrievedContact.jobtitle.getValue()

                Xrm.Page.getAttribute("jobtitle").setValue(retJobTitle);

            }
            else {
            }

        }
        else {
        }

    }
    else {
    }
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Ben Thompson Profile Picture
    6,350 on at

    At a quick first glance

    if (Xrm.Page.context.getClientUrl !== undefined) {

    should be

    if (Xrm.Page.context.getClientUrl != undefined) {

    with 1 equals sign not 2.

    The rest of it looks fine although you are using the old (and deprecated) soap interface rather than the more recent Webapi one.

  • Suggested answer
    Community Member Profile Picture
    on at

    Please perform the null checks the following way in some of the areas of your code:-

    if (contact && contact.length > 0 && contact[0].entityType && contact[0].id 
    && contact[0].entityType == "contact")

    if (serverUrl) {
    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

    your rest of the code goes here
    }

    And some more null checks in the following lines of code :-

    var retrievedContact = (contactRequest && contactRequest.response && contactRequest.response.value) ? 
    JSON.parse(contactRequest.response).value : null;

    You wouldn't really need a getValue() in this line, please try without the getValue()

    if (retrievedContact && retrievedContact.jobtitle) { var retJobTitle = retrievedContact.jobtitle }



    Please mark the answer as verified if it resolves your issue.

    Thanks,

    Sravya Patti

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    You don't need to use getValue here

    var retJobTitle = retrievedContact.jobtitle.getValue()

    //instead it should be simple

    var retJobTitle = retrievedContact.jobtitle

  • Community Member Profile Picture
    on at

    I have removed the extra equals and the getvalue but now get "Error: parameter is not passed or parameter is nNull or undefined"

    function setContactFields() {
    
        //Get the contact lookup off of the record
        var contact = Xrm.Page.getAttribute('parentcontactid').getValue();
    
        //if contact exist, attempt to pull back the contact record
        if (contact != null && contact[0].entityType == "contact") {
    
            var contactId = contact[0].id;
            var serverUrl;
    
            if (Xrm.Page.context.getClientUrl != undefined) {
                serverUrl = Xrm.Page.context.getClientUrl();
            }
            else {
                serverUrl = Xrm.Page.context.getServerUrl();
            }
    
            var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
            var contactRequest = new XMLHttpRequest();
    
            contactRequest.open("GET", ODataPath + "/ContactSet(guid'" + contactId + "')", false);
            contactRequest.setRequestHeader("Accept", "application/json");
            contactRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    
            contactRequest.send();
    
            //If request was successful, parse the associated contact account name
    
            if (contactRequest.status == 200) {
    
                var retrievedContact = JSON.parse(contactRequest.responseText).d;
    
                if (retrievedContact != null) {
    
                    var retJobTitle = retrievedContact.jobtitle
    
                    Xrm.Page.getAttribute("jobtitle").setValue(retJobTitle);
    
                }
                else {
                }
    
            }
            else {
            }
    
        }
        else {
        }
    }
    


  • Community Member Profile Picture
    on at

    Hey, what is about the object 'retrievedContact'. Can you discover it with the developer tools of your browser? you can log it in the console with console.log(retrievedContact). Here you have the chance to discover all properties given in the object. Thats how i debug my scripts. In every critical part of your script, put a console.log(object) (in development). So you can be sure, that you have the information you want in the current context.

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    I will suggest you to debug your code, it will help you to understand where things are not working correctly, put debugger keywork to in your code.

  • Verified answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi, 

    You need to select jobtitle in your query,

    function setContactFields() {
    
    //Get the contact lookup off of the record
    var contact = Xrm.Page.getAttribute('parentcontactid').getValue();
    
    //if contact exist, attempt to pull back the contact record
    if (contact != null && contact[0].entityType == "contact") {
    
    var contactId = contact[0].id;
    var serverUrl;
    
    if (Xrm.Page.context.getClientUrl != undefined) {
    serverUrl = Xrm.Page.context.getClientUrl();
    }
    else {
    serverUrl = Xrm.Page.context.getServerUrl();
    }
    
    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    var contactRequest = new XMLHttpRequest();
    
    contactRequest.open("GET", ODataPath + "/ContactSet(guid'" + contactId + "')?$select=jobtitle", true);
    contactRequest.setRequestHeader("Accept", "application/json");
    contactRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    
    contactRequest.send();
    
    //If request was successful, parse the associated contact account name
    
    if (contactRequest.status == 200) {
    
    var retrievedContact = JSON.parse(contactRequest.responseText).d;
    
    if (retrievedContact != null) {
    
    var retJobTitle = retrievedContact.jobtitle
    
    Xrm.Page.getAttribute("jobtitle").setValue(retJobTitle);
    
    }
    else {
    }
    
    }
    else {
    }
    
    }
    else {
    }
    }


    Thanks,

    Shahbaaz

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans