Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Update contact field when account field is updated using javascript

(0) ShareShare
ReportReport
Posted on by 105

Update company name in contact when company name in account is updated .. I want this using Javascript. i have tried this but it is not working..Pls guide

function setContactCompanyName() {

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

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

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 (contactRequest.status == 200) {

var col1 = ["orc_companyname1"];
var retrievedCompanyName = XrmServiceToolkit.Soap.Retrieve
("orc_accountentity", contactId, col1);

if (retrievedCompanyName.attributes["orc_companyname1"] != null) {
Xrm.Page.getAttribute("orc_companyname1").setValue(retrievedCompanyName.attributes["orc_companyname1"].value);
}

}

}
}

  • Suggested answer
    cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: Update contact field when account field is updated using javascript

    Hi Rits,

    Please check following points:

    1. Did you use a custom entity "orc_contactentity" to represent the company of contact?

    If so, you should query orc_contactentitySet instead.

    If not, which means that orc_accountowners field of contact is still based on OOB contact entity, then contact[0.entityType == "orc_contactentity" should be changed to contact[0.entityType == "contact".

    2. For this part of code:

    if (contactRequest.status == 200) {
    
        var col1 = ["orc_companyname1"];
        var retrievedCompanyName = XrmServiceToolkit.Soap.Retrieve
            ("orc_accountentity", contactId, col1);
    
        if (retrievedCompanyName.attributes["orc_companyname1"] != null) {
            Xrm.Page.getAttribute("orc_companyname1").setValue(retrievedCompanyName.attributes["orc_companyname1"].value);
        }
    }

    Now that you have run XMLHttpRequest query request, why not extract attribute you want from the response text directly?

    if (contactRequest.status == 200) {
    
        var result = JSON.parse(contactRequest.responseText).d;
        var retrievedCompanyName = result["FullName"];
        console.log(retrievedCompanyName);
    
    }

    3. When extracting attributes from the response text, the attribute name that you want to fetch should be in its schema name.

    pastedimage1603157804396v1.png

    In summary, please confirm the actual entity type that you want to retrieve(contact or account or custom orc_contactentity).

    I modified your code, which is based on those prerequisites.

    1. The main entity is OOB Contact.

    2. Company name of the contact is still the OOB contact type, therefore, the lookup field I use is the existing Account Name/Company Name field likewise.

    3. I extract fullname of the parent contact from the response text and populate a custom single line text field with the value.

    function setContactCompanyName() {
    
        //Get the contact lookup off of the record
        var contact = Xrm.Page.getAttribute('parentcustomerid').getValue();
    
        //if contact exist, attempt to pull back the entire 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 (contactRequest.status == 200) {
    
                var result = JSON.parse(contactRequest.responseText).d;
                var retrievedCompanyName = result["FullName"];
                Xrm.Page.getAttribute("new_companyname").setValue(retrievedCompanyName);
            
            }
    
        }
    }

    Regards,

    Clofly

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February 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... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans