Lead Qualify by java script in dynamics crm 365






function qualifyLead(formContext) {

    formContext.ui.setFormNotification("Qualifying lead...", "INFO", "qualifyNotification");


    var leadRef = formContext.data.entity.getEntityReference();


    var actionRequest = {

        entity: leadRef,

        CreateAccount: true,

        CreateContact: true,

        CreateOpportunity: true, // Set this to true to create an opportunity

        OpportunityCurrencyId: leadRef.transactioncurrencyid, // Use the currency from the lead

        OpportunityCustomerId: null,

        SourceCampaignId: null,

        Status: 3,

        ProcessInstanceId: null

    };


    Xrm.WebApi.online.executeAction("QualifyLead", actionRequest).then(

        function (response) {

            if (response.ok) {

                var accountId = "";

                var opportunityId = "";


                response.json().value.forEach(function (record) {

                    if ("accountid" in record) {

                        accountId = record.accountid;

                    }

                    if ("opportunityid" in record) {

                        opportunityId = record.opportunityid;

                    }

                });


                if (accountId !== "") {

                    formContext.ui.clearFormNotification("qualifyNotification");

                    formContext.getNavigation().openForm({ entityName: "account", entityId: accountId });

                }


                if (opportunityId !== "") {

                    formContext.getNavigation().openForm({ entityName: "opportunity", entityId: opportunityId });

                }

            }

        },

        function (error) {

            formContext.ui.setFormNotification(error.message, "ERROR", "qualifyNotification");

        }

    );


// call this function on specific condition .

Comments

Popular posts from this blog

Power Automate - Update Child record when parent record is updated by using power automate in MS Dynamics CRM

set the active stage in Dynamics 365 when the option set value is "yes"