Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to call ExecuteAction using API in MS Dynamics CRM?

(0) ShareShare
ReportReport
Posted on by

Hi,

I have following code which is working properly for ExecuteAction. I want to call following call using API. 

Can anybody please suggest me with return parameter?

function ExecuteAction(entityId, entityName, requestName) {
	try {
		var response = "";
		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>";

		$.ajax({
			type: "POST",
			contentType: "text/xml; charset=utf-8",
			datatype: "xml",
			url: serverUrl + "/XRMServices/2011/Organization.svc/web",
			data: requestXML,
			async: false,
			beforeSend: function (XMLHttpRequest) {
				XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
				XMLHttpRequest.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute");
			},
			success: function (data, textStatus, XmlHttpRequest) {
				if (XmlHttpRequest.status === 200) {
					response = $(XmlHttpRequest.responseText).find('b\\:value').text();
				}
				return response;
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert(errorThrown);
				return XMLHttpRequest.ResponseText;
			}
		});
	}
	catch (e) {
		return "";
	}
}

API call should return when it gets response.

Please suggest.

*This post is locked for comments

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: How to call ExecuteAction using API in MS Dynamics CRM?

    you can try below ; i am retrieving the lead id first from the current record and then passing that leadid to my call action function as an input parameter.

       abcfunction: 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);

                       Xrm.Utility.openEntityForm("opportunity", result.OpportunityID);

                   }

                   else {

                       var error = JSON.parse(this.response).error;

                       alert(error.message);

                   }

               }

           };

           req.send();

           return result;

       },

  • Verified answer
    sardar ahmed Profile Picture
    sardar ahmed 520 on at
    RE: How to call ExecuteAction using API in MS Dynamics CRM?

    Hi,

    Please go through the below link.

    https://community.dynamics.com/crm/b/nishantranaweblog/archive/2017/05/27/sample-code-to-call-action-using-web-api-in-crm 

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans