Posted in Actions, CRM, JavaScripts

Calling a Global_Action Through JavaScript with InputParameters

Calling a global_action through javascript with input parameters

Action  name= new_a

Input Parameters

first– string-input

//second-string-input

screenshot-14

When an action is triggered , it will creating the contact with input parameters –

firstname= first and lastname= second

screenshot-15

JavaScript Code-

It is added to the account form web-resources on form saved

Through code we are passing the data to the action input parameters as     first = recordid

//calling an globalaction and passing the recordguid to the inputparameter 
 

function OnReOpen() {
 recordId = Xrm.Page.data.entity.getId();
 recordId = recordId.replace("{", "");
 recordId = recordId.replace("}", "");
 entity = Xrm.Page.data.entity.getEntityName();
 var accountName = Xrm.Page.getAttribute("name").getValue();
 var accou = Xrm.Page.getAttribute("name").getValue();
 
 
 var result = InvokeAction("new_a", recordId);
 
}
function InvokeAction(actionName,recordId) {
 debugger;
 var xmlHTTPRequest = new XMLHttpRequest();
 var results = true;
 xmlHTTPRequest.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", false);
 xmlHTTPRequest.setRequestHeader("Accept", "application/xml, text/xml");
 xmlHTTPRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
 xmlHTTPRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");


 xmlHTTPRequest.onreadystatechange = function () {
 debugger;
 if (this.readyState == 4) {
 if (this.status == 200) {
 //if required write code here.
 alert("success");
 }
 else {
 results = false;
 }
 }
 };
 xmlHTTPRequest.send(BuildActionRequestXml(actionName,recordId));
 
}


function BuildActionRequestXml(actionName,recordId) {
 debugger;
 var requestXml = "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>" +
 "<s:Body>" +
 "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
 "<request xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>" +
 "<a:Parameters xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>" +
 "<a:KeyValuePairOfstringanyType>" +
 "<b:key>first</b:key>" +
 "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>" + recordId + "</b:value>" +
 "</a:KeyValuePairOfstringanyType>" +
 //"<a:KeyValuePairOfstringanyType>" +
 //"<b:key>second</b:key>" +
 //"<b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + + "</b:value>" +
 //"</a:KeyValuePairOfstringanyType>" +
 "</a:Parameters>" +
 "<a:RequestId i:nil='true' />" +
 "<a:RequestName>" + actionName + "</a:RequestName>" +
 "</request>" +
 "</Execute>" +
 "</s:Body>" +
 "</s:Envelope>";
 return requestXml;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s