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

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Answered

Update Contact on Click of Button

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Verified answer
    Ray Profile Picture
    1,537 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);
            }
        )
    }
  • CRMJetty Profile Picture
    3,512 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.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 64

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 23 Super User 2025 Season 2

#3
mk1329 Profile Picture

mk1329 16

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans