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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Calling an action through JavaScript and not allowing the record to be saved based on conditions in action

(0) ShareShare
ReportReport
Posted on by

Hello,

My requirement is to call a custom action through a javascript, the action executes certain conditions and sends back an output parameter to the javascript

Now based on the output parameter i have to save the record or not allow the record to be saved

any suggestions?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Radu Chiribelea Profile Picture
    6,667 on at

    Hi rahulvgerald,

    Not sure if what you are trying to achieve is possible via JS. However, you could consider another approach. Have a plug-in triggered when the action is called that updates the record. If certain criteria match, throw an InvalidPluginExecution Exception with a custom message. This will prevent the record from being saved server side.

    More details: msdn.microsoft.com/.../microsoft.xrm.sdk.invalidpluginexecutionexception.aspx

    Hope this helps.

    Regards,

    Radu

  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Rahul you can do this ,

    use web api to execute the action and pass on the parameter like below :

    i am doing something like his below  where in am passig the leadid to the action.

    Leadconversion: function () {

    var regardingObject = Xrm.Page.getAttribute("regardingobjectid");

    if (regardingObject != null && regardingObject.getValue() != null) {
    var leadlookup = regardingObject.getValue()[0];
    var leadId = leadlookup.id;
    leadId = leadId.replace(/[{}]/g, "");
    callAction("leads", "Microsoft.Dynamics.CRM.ActionName", leadId);

    }
    },
    callAction: function (entityName, actionName, targetid) {
    var result = null;

    var oDataEndPoint = Xrm.Page.context.getClientUrl() + "/api/data/v8.0/";

    var req = new XMLHttpRequest();
    req.open("POST", oDataEndPoint + entityName + "(" + targetid + ")/" + actionName, false);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function () {
    if (this.readyState == 4) {
    req.onreadystatechange = null;

    if (this.status == 200) {
    result = JSON.parse(this.response);

    //your logic goes to prevent save.

    }
    else {
    var error = JSON.parse(this.response).error;
    alert(error.message);
    }
    }
    };
    req.send();
    return result;

    }

    its very tricky prevent save in javascript but you can try below :

    if(a != b)

    {

    Xrm.Utility.alertDialog("a and b are not equal",function(){
    Xrm.Page.data.entity.addOnSave(function (execContext) {
    execContext.getEventArgs().preventDefault();//will prevent CRM form save
    }); 
    });

    }

    however i would suggest you to use a plugin and throw invalidpluginexception.

    mark my answer as verified if helpful

  • Verified answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Rahul,

    below is the full code for calling a custom action from java script and also you can check the condition too,

    function CallActionFromJavaScript() {

       var accountId = Xrm.Page.data.entity.getId();

       var entityName = "account";

       var requestName = "new_customaction";

    var appnumber = Xrm.Page.data.entity.attributes.get('new_leapcasenumber').getValue();

    var new_crmcustomerid = Xrm.Page.data.entity.attributes.get('new_crmcustomerid').getValue();

    if(new_crmcustomerid == null){

    alert("Save record before sending to Portal?");

    } else if(appnumber != null){

    if(appnumber.trim().length == 4)

    alert("This account has already been sent to portal.");

    }

    else

    ExecuteAction(accountId, entityName, requestName);

    }

    function ExecuteAction(entityId, entityName, requestName) {

       // Creating the request XML for calling the Action

       var requestXML = ""

       requestXML += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

       requestXML += "  <s:Body>";

       requestXML += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

       requestXML += "      <request xmlns:a=\"schemas.microsoft.com/.../Contracts\">";

       requestXML += "        <a:Parameters xmlns:b=\"schemas.datacontract.org/.../System.Collections.Generic\">";

       requestXML += "          <a:KeyValuePairOfstringanyType>";

       requestXML += "            <b:key>Target</b:key>";

       requestXML += "            <b:value i:type=\"a:EntityReference\">";

       requestXML += "              <a:Id>" + entityId + "</a:Id>";

       requestXML += "              <a:LogicalName>" + entityName + "</a:LogicalName>";

       requestXML += "              <a:Name i:nil=\"true\" />";

       requestXML += "            </b:value>";

       requestXML += "          </a:KeyValuePairOfstringanyType>";

       requestXML += "        </a:Parameters>";

       requestXML += "        <a:RequestId i:nil=\"true\" />";

       requestXML += "        <a:RequestName>" + requestName + "</a:RequestName>";

       requestXML += "      </request>";

       requestXML += "    </Execute>";

       requestXML += "  </s:Body>";

       requestXML += "</s:Envelope>";

       var req = new XMLHttpRequest();

       req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", false);

       req.setRequestHeader("Accept", "application/xml, text/xml, */*");

       req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

       req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

       req.send(requestXML);    

    //Get the Resonse from the CRM Execute method

       var response = req.responseXML;

       alert("Customer has been sent to your portal. Please login to portal and complete the form.");

    }

    Please mark my answer as verified if it resolves your issue.

    Thanks,

    Shahbaaz

  • Community Member Profile Picture
    on at

    HI Rawish,

    In the following piece of code from where are you getting "execContext" ?

    Xrm.Utility.alertDialog("a and b are not equal",function(){
    Xrm.Page.data.entity.addOnSave(function (execContext) {
    execContext.getEventArgs().preventDefault();//will prevent CRM form save

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Rahul,

    you can pass "executionContext" in this starting function as first parameter and use it below instead execContext or simple use below code in a difference function and pass it on same context and use it.

  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at

    to learn how to use execution context - i will suggest you to go through below post :

    practical-crm.blogspot.in/.../passing-execution-context-to-onchange.html

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Refer to this how to use Javascript to prevent save. but change into below code

    Place it under onSave Event, remember to click the pass execution context as first parameter

    function preventSave(econtext) 
    {
    // your logic go here, if you want prevent save just call the below two lines code. var eventArgs = econtext.getEventArgs(); eventArgs.preventDefault(); }

    alexmscrm.blogspot.my/.../dynamics-365-auto-save.html

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans