Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

while error retrieve field value from Contact to Customer entity using javascript

Posted on by Microsoft Employee

There are two entities 

1.  Contact

2. Customer

I will try to retrieve field value from contact(field name mobile) and update to Customer(field name sbl_contact_mobile) with below code but field value should be blank.So please check and correction of my code.

function GetDataOnChange()
{
var custid = Xrm.Page.getAttribute("parentaccountid").getValue()[0].id;
if(custid !=null) {
var cid =custid.replace("{","").replace("}","");
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts("+cid+")?$select=mobilephone", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var contmobile = result["mobilephone"]; // will return value example - 8
Xrm.Page.getAttribute("sbl_contact_mobile").setValue(contmobile);

} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
}

 

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: while error retrieve field value from Contact to Customer entity using javascript

    Hi,

    I have checked your code and it looks fine. You need to ensure that the custid you are passing is a contact record (and not account). Also the mobile number should exists in the record you are retrieving.

    Hope this helps.

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: while error retrieve field value from Contact to Customer entity using javascript

    Hi,

    By the way, I would suggest you to use rest builder to help you to generate the code.

    www.toplinestrategies.com/.../crm-rest-builder-useful-tool-working-crm-2016%E2%80%99s-web-api

  • Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: while error retrieve field value from Contact to Customer entity using javascript

    Hi,

    would you mind to debug the JS and let us know which line is causing the problem?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: while error retrieve field value from Contact to Customer entity using javascript

    Yes I have done with Workflow but i need to retrieve with script

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: while error retrieve field value from Contact to Customer entity using javascript

    Hi,

    do they have relationship ? would you consider to use workflow instead of webapi to achieve it?.

    I personally will suggest you to use workflow to achieve it, because JS only happens in client side.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans