Skip to main content

Notifications

Dynamics 365 general forum
Suggested answer

Code to reopen the closed leads using web api

Posted on by 2

Hi,

I need code to reopen or reactivate the closed leads using web api,similar to Reactivate lead button.

pastedimage1616074822362v1.png

  • Suggested answer
    Ali Raza Profile Picture
    Ali Raza 10 on at
    Code to reopen the closed leads using web api
    Please copy the same JavaScript code into your web resource without any changes.
     
    function reactivateLead() {
    debugger;
        var leadId = Xrm.Page.data.entity.getId(); // Current lead ka ID lete hain
        if (leadId) {
            var entityFormOptions = {};
            entityFormOptions["entityName"] = "lead";
            entityFormOptions["entityId"] = leadId;
            var parameters = {};
            parameters["statuscode"] = 1; // Status code for reactivation
            parameters["statecode"] = 0;   // State code for active
            Xrm.WebApi.updateRecord("lead", leadId, parameters).then(
                function success(result) {
                    console.log("Lead reactivated successfully.");
                    // Optionally, you can refresh the form or redirect
                    Xrm.Page.data.refresh();
                },
                function (error) {
                    console.error("Error reactivating lead: " + error.message);
                    alert("Error: " + error.message);
                }
            );
        } else {
            alert("Lead ID not found.");
        }
    }
  • Suggested answer
    meelamri Profile Picture
    meelamri 13,202 User Group Leader on at
    RE: Code to reopen the closed leads using web api

    Hi, 

    //set statecode to 0 aka open
    var data ={"statecode": 0}
    var leadId = "8F4FA496-3FF2-E311-9864-A45D36FC5F1C"; 
    
    // update the record
    Xrm.WebApi.updateRecord("lead", leadId, data).then(
        function success(result) {
            console.log("Lead updated");
            // perform operations on record update
        },
        function (error) {
            console.log(error.message);
            // handle error conditions
        }
    );

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... 290,888 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,247 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans