Skip to main content

Notifications

Service | Customer Service, Contact Center, Fie...
Answered

Update Contact on Click of Button

Posted on by 235

Hello CS Experts,

I have a requirement to update automatically  certain fields in Contact record using Button Click on Contact form.

Bellow are the fields that will be updated

pastedimage1659944240011v1.png

My question is how to do the automatic update using javascript.

Regards,

Jorge

  • CRMJetty Profile Picture
    CRMJetty 3,508 on at
    RE: Update Contact on Click of Button

    Hi Jolas365 ,


    You may update record on click of button using JS. For that create JS web resource in CRM and write update record logic in it and call function on click of ribbon button.

    To update record using JS we resource, refer below link:
    https://docs.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/xrm-webapi/updaterecord


    To call JS function on click of ribbon button, refer below link:
    https://www.inogic.com/blog/2018/08/java-script-execution-context-provides-form-values-on-web-as-well-as-uci-in-dynamics-365-v9-0/
    https://d365demystified.com/2018/12/21/create-a-new-record-button-for-activity-type-entity-using-ribbon-workbench-d365/

    I hope it helps,

    Thanks.

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

  • Verified answer
    Ray Profile Picture
    Ray 1,505 on at
    RE: Update Contact on Click of Button
    Hi Jorge,

    There are 2 ways to do.
    1. Send an update request via API. You can generate the code with Dataverse REST Builder(created by Guido Preite)
    2. Set attribute value and then call form save method.
    Sample code:
    function updateContactByAPIRequest(formContext) {
        var contactId = formContext.data.entity.getId().slice(1, -1);
        var record = {};
        record.firstname = "XXXXX";
        record.lastname = "XXXXX";
        record.emailaddress1 = "abc@xyz.com";
        record.telephone1 = null;

        var req = new XMLHttpRequest();
        req.open("PATCH", `${Xrm.Utility.getGlobalContext().getClientUrl()}/api/data/v9.0/contacts(${contactId})`, true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Prefer", "odata.include-annotations=*");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 204) {
                    // Success callback, you can do other logic here if needed.
                } else {
                    console.log(this.responseText);
                }
            }
        };
        req.send(JSON.stringify(record));
    }

    function updateContactByFormSave(formContext) {
        // Set attribute value as you want
        formContext.getAttribute("firstname").setValue("XXXXX");
        formContext.getAttribute("lastname").setValue("XXXXX");
        formContext.getAttribute("emailaddress1").setValue("abc@xyz.com");
        formContext.getAttribute("telephone1").setValue(null);

        // Call save method
        // More ref: docs.microsoft.com/.../save
        formContext.data.entity.save().then(
            function () {
                // Success callback, you can do other logic here if needed.
            },
            function (error) {
                // Error callback
                Xrm.Utility.alertDialog(error);
            }
        )
    }

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans