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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Javascript to populate customerid field

(0) ShareShare
ReportReport
Posted on by 1,549

Hi there,

I'm trying to do something quite simple but not being great with Javascript, I need some help.

On the case form, I have 2 lookup fields fields: customerid and primarycontactid.

When a user fills the primarycontactid, I need to auto-populate the customerid field with the contact's parent account.

I tried using workflows, but customerid is system required and needs to be filled before saving :(.

Any help is appreciated.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    You will have to write JS to make it.

    You can use CRMRestBuilder to prepare query and generate code.

  • Suggested answer
    sandeepstw Profile Picture
    4,601 on at

    Hi,

    You can use something like this

    www.crmanswers.net/.../set-accounts-primary-contact-as.html

    with javascript event bind.

  • yleclerc Profile Picture
    1,549 on at

    Thanks Andrii,

    I installed CRMRestBuilder and played around with it.

    I'm no programmer and have a hard time figuring out which Action to chose (Retrieve Single, Update, etc.).

    I also found a similar thread, not sure if any of the suggested solutions make sense?

    social.microsoft.com/.../autopopulate-account-field-based-on-selected-contact

  • yleclerc Profile Picture
    1,549 on at

    Thank you Sandeep,

    But what I'm trying to accomplish here is the opposite.

    1. User selects a contact from lookup field

    2. Account name populates upon selection of contact (with the contact's parent account)

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    In your case it should be Retrieve. Choose contact entity and select parentaccountid (or something similar). Build query.

    All you will need is to include id of contact inside. To get it you can use something similar to

    var contactid = Xrm.Page.getAttribute("primarycontactid").getValue();

    if (contactid == null)

    return;

    contactid = contactid[0].id;

    Please continue posting your progress and I believe you with my help resolve your scenario.

  • yleclerc Profile Picture
    1,549 on at

    Great, thank you - I fell I'm getting closer!

    Here is what I have from what you provided + the code that was generated by CRMRestBuilder.

    I applied the function on the Contact field (onchange).

    function updateaccount()
    {

    var contactid = Xrm.Page.getAttribute("primarycontactid").getValue();
    if (contactid == null)
    return;
    contactid = contactid[0].id;

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts()?$select=_parentcustomerid_value", 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=\"OData.Community.Display.V1.FormattedValue\"");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var result = JSON.parse(this.response);
    var _parentcustomerid_value = result["_parentcustomerid_value"];
    var _parentcustomerid_value_formatted = result["_parentcustomerid_value@OData.Community.Display.V1.FormattedValue"];
    }
    else {
    alert(this.statusText);
    }
    }
    };
    req.send();
    }

    Nothing happens when I select a contact. Maybe I'm just missing something to post the value to the customerid field...

    Xrm.Page.getAttribute("customerid").setValue(_parentcustomerid_value_formatted ); ???

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    So first thing - you should point CRM to give you correct contact. Instead of using

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts()?$select=_parentcustomerid_value", true);

    try to use following:

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts(" + contactid + ")?$select=_parentcustomerid_value", true);

    Can you also add code

    alert(_parentcustomerid_value);

    after line

    var _parentcustomerid_value = result["_parentcustomerid_value"];

    It will notify that something was retrieved.

  • yleclerc Profile Picture
    1,549 on at

    Okay, now I do have an alert but it says "Bad Request".

    2016_2D00_08_2D00_19_5F00_09_2D00_28_2D00_58.jpg

    Nothing else happens when I update the Contact field.

    Your help is very much appreciated!

    Here is my updated code:

    function updateaccount()
    {

    var contactid = Xrm.Page.getAttribute("primarycontactid").getValue();
    if (contactid == null)
    return;
    contactid = contactid[0].id;

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts(" + contactid + ")?$select=_parentcustomerid_value", 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=\"OData.Community.Display.V1.FormattedValue\"");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var result = JSON.parse(this.response);
    var _parentcustomerid_value = result["_parentcustomerid_value"];
    alert(_parentcustomerid_value);
    var _parentcustomerid_value_formatted = result["_parentcustomerid_value@OData.Community.Display.V1.FormattedValue"];
    }
    else {
    alert(this.statusText);
    }
    }
    };
    req.send();
    }

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Try following:

    function updateaccount()

    {

    var contactid = Xrm.Page.getAttribute("primarycontactid").getValue();

    if (contactid == null)

    return;

    contactid = contactid[0].id.replace("}", "").replace("{", "");

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts(" + contactid + ")?$expand=parentcustomerid_account($select=accountid,name)", 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=\"OData.Community.Display.V1.FormattedValue\"");

    req.onreadystatechange = function () {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var result = JSON.parse(this.response);

               var contactid = result["contactid"];

               if(result.hasOwnProperty("parentcustomerid_account")) {

                   var parentcustomerid_account_accountid = result["parentcustomerid_account"]["accountid"];

                   var parentcustomerid_account_name = result["parentcustomerid_account"]["name"];

    Xrm.Page.getAttribute("customerid").setValue([{

    id: parentcustomerid_account_accountid,

    name: parentcustomerid_account_name,

    entityType: "account"

    }]);

               }

           }

           else {

               alert(this.statusText);

           }

       }

    };

    req.send();

    }

  • yleclerc Profile Picture
    1,549 on at

    Wow! It's working!!!

    Thank you so much. I never would have figured this out without your help!

    They should create a special badge for you ;)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans