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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Access custom action through web API in MS CRM 2016

(0) ShareShare
ReportReport
Posted on by 1,401

Hi

I am trying to access  my custom action , through web api in javascript  in crm 2016 . Tried many odata URI .  Can you help me with working odata URI 

Many Thanks in advance !

*This post is locked for comments

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
  • Verified answer
    nghieppham Profile Picture
    4,755 on at

    Hi Mclare,

    Please try this way

    1. You create 1 js libary and create the follow function:

     function executeUnboundFunction (functionName, parameters, successCallback, errorCallback, callerId) {

     /// <summary>Invoke an unbound function</summary>

     /// <param name="functionName" type="String">The name of the unbound function you want to invoke</param>

     /// <param name="parameters" type="Array">An array of strings representing the parameters to pass to the unbound function</param>

     /// <param name="successCallback" type="Function">The function to call when the function is invoked. The results of the unbound function will be passed to this function.</param>

     /// <param name="errorCallback" type="Function">The function to call when there is an error. The error will be passed to this function.</param>

     /// <param name="callerId" type="String" optional="true">The systemuserid value of the user to impersonate</param>

     var UriPath = "//Your Web API Url" + functionName;

     var parameterNames = [];

     var parameterAliasValues = [];

     var parameterNumber = 1;

     if (parameters) {

      parameters.forEach(function (param) {

       var keyValue = param.split("=");

       var name = keyValue[0];

       var value = keyValue[1];

       parameterNames.push(name + "=" + "@p" + parameterNumber.toString());

       parameterAliasValues.push("@p" + parameterNumber.toString() + "=" + value)

       parameterNumber++;

      });

      UriPath = UriPath + "(" + parameterNames.join(",") + ")?" + parameterAliasValues.join("&");

     }

     else {

      UriPath = UriPath + "()"; }

     var req = new XMLHttpRequest();

     req.open("GET", encodeURI(UriPath), true);

     req.setRequestHeader("Accept", "application/json");

     if (callerId) {

      req.setRequestHeader("MSCRMCallerID", callerId);

     }

     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 /* complete */) {

       req.onreadystatechange = null;

       if (this.status == 200) {

        if (successCallback)

         successCallback(JSON.parse(this.response, dateReviver));

       }

       else {

        if (errorCallback)

         errorCallback(Sdk.WebApiOperation.errorHandler(this));

       }

      }

     };

     req.send();

    }

    1. Add this library to your form or HTML web resource:

    and call this function like:

    "Your Library".executeUnboundFunction("", //functionName

     null, //your parameters

     function(){},  //successCallback

      function(){},); //errorCallback

    }

    Please remember that there are different from BoundFunction and Unboundfunction, refer this link for more information msdn.microsoft.com/.../gg309638.aspx

    Regards,

  • Suggested answer
    Javed Iqbal Profile Picture
    85 on at

    Below is the working example of calling custom action using JS. I have 2 output variables, one gets set by custom action and the other one "ptext" by plugin which i have registered on this Action. Hope it will help solve your problem.

    function CallCAction(context) {       
     var Credit_Limit = Xrm.Page.getAttribute("creditlimit").getValue();
        var Credit_hold = Xrm.Page.getAttribute("creditonhold").getValue();
        if(Credit_hold !=null && Credit_Limit!=null){
            var actionName = "new_Preferred_Check";
            var inputParam = {
                "Credit_Limit": Credit_Limit,
                "Credit_hold": Credit_hold
            };
            Xrm.Page.ui.setFormNotification("Processing...","INFO","processingFId");
            var actionResponse = callPreferredCust(actionName, inputParam);
    
            if (actionResponse != null) {
                Xrm.Page.getAttribute("new_preferredcust").setValue(actionResponse.Preferred_Cust);
                alert(actionResponse.ptext);
                Xrm.Page.ui.clearFormNotification("processingFId");
            }        
        }    
    }
    function callPreferredCust(actionName, inputParam) {
        var result = null;
        var req = new XMLHttpRequest();
        var uri = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/";
        try {
            req.open("POST",encodeURI(uri+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);
                    }else{
                        var err = JSON.parse(this.response).error;
                        alert(err.message);
                    }
                }
            };
            req.send(JSON.stringify(inputParam));
            return result;
    
        }catch(err){
            alert(err.message);
        }
    }


  • CRMQueries Profile Picture
    62 on at

    When you are calling an action isn't it necessary to pass the guid of the record and entity name.

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    For global actions there is no need to pass record id and type.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans