SCRIPT1003: Expected ‘:’ Error in IE 11


Just sharing a simple scenario where my code was working in Chrome and Edge but It was throwing error  (SCRIPT1003: Expected ‘:’ ) in IE. Basically I was calling an Actions using Web API which was returning two output parameters then I was returning these two values to calling function.

mc.Ribbon.GetSignature = function () {
var signature;
var referenceno;
var Id = Xrm.Page.data.entity.getId().substring(1, 37);
var data = {
“receiptid”: Id
};
var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
req.open(“POST”, clientURL + “/api/data/v8.2/<entity>(” + Id + “)/Microsoft.Dynamics.CRM.<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 /* complete */) {
req.onreadystatechange = null;
if (this.status == 204 || this.status == 200) {
var receipts = JSON.parse(this.response);
signature = receipts[“signature”];
referenceno = receipts[“referenceno”];
}
else {
var error = JSON.parse(this.response).error;
}
}
};
req.send(JSON.stringify(data));
return {signature,referenceno};
};
Notice the return statement where I was returning two variable as object without key/value pair which was not working in IE 11 but it was working perfectly fine with Chrome and Edge. Then I changed the code to return array after assigning the value. See the correct code below which was working perfectly fine in IE 11 as well.

mc.Ribbon.GetSignature = function () {
var signArray = new Array();
var Id = Xrm.Page.data.entity.getId().substring(1, 37);
var data = {
“receiptid”: Id
};
var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
req.open(“POST”, clientURL + “/api/data/v8.2/<entity>(” + Id + “)/Microsoft.Dynamics.CRM.<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 /* complete */) {
req.onreadystatechange = null;
if (this.status == 204 || this.status == 200) {
var receipts = JSON.parse(this.response);
signArray[“signature”] = receipts[“signature”];
signArray[“referenceno”] = receipts[“referenceno”];
}
else {
var error = JSON.parse(this.response).error;
}
}
};
req.send(JSON.stringify(data));
return signArray;
};

Hope this helps.

Call Actions using Dynamics CRM Web API with Output Parameter


Let me share one scenario where I was working on payment gateway integration with Dynamics CRM . We were redirecting to payment page once user click on ribbon button and redirect to CRM on successful payment. Since we need to pass secure hash as a part of payment URL which is generated using secret key and some security sequence string. Since secret key is secure information so we should not pass to JavaScript so we decided to create Actions which will take some input and generate the Secure Hash using algorithm(SHA-256 HMAC) and return to output parameters.

So mainly 3 steps involve in complete requirement.
1. Create Actions with Input and Output parameter
2. Create Web API to Call Actions to get Secure Hash which will be used to pass as parameter to payment gateway.
3. Call JavaScript function from ribbon button which internally call Web API.

Here is my Actions with input and output parameters

Below is my Plugin code which I registered on “mc_GetSecureHash” message for custom entity called “mc_receipts”.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace MC.CRM.PlugIns
{
public class GetSecureHash : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
string vendorName = string.Empty;
string merchantId = string.Empty;
string accessCode = string.Empty;
string secretKey = string.Empty;
string transactionType = string.Empty;
string sequenceStringForHash = string.Empty;
string secureHash = string.Empty;

// Obtain the execution context service from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

// Obtain the tracing service from the service provider.
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Obtain the Organization Service factory service from the service provider
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

// Use the factory to generate the Organization Service.
IOrganizationService service = factory.CreateOrganizationService(context.UserId);

try
{
if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is EntityReference)
{
EntityReference receipts = (EntityReference)context.InputParameters[“Target”];
sequenceStringForHash = (string)context.InputParameters[“sequencestring”];

vendorName = (string)context.InputParameters[“vendorname”];

}

EntityCollection paymentGatewaySettings = GetPaymentGatewayInformtion(service, vendorName);
if (paymentGatewaySettings.Entities.Count > 0)
{
Entity paymentSettings = paymentGatewaySettings.Entities[0];
if (paymentSettings.Attributes.Contains(“mc_merchantid”))
{
merchantId = paymentSettings.Attributes[“mc_merchantid”].ToString();
}
if (paymentSettings.Attributes.Contains(“mc_secretkey”))
{
secretKey = paymentSettings.Attributes[“mc_secretkey”].ToString();
}
if (paymentSettings.Attributes.Contains(“mc_accesscode”))
{
accessCode = paymentSettings.Attributes[“mc_accesscode”].ToString();
}

secureHash = GenerateSHA512String(secretKey, sequenceStringForHash);

context.OutputParameters[“securehash”] = secureHash;
}
}
catch (FaultException<OrganizationServiceFault> e)
{
throw new InvalidPluginExecutionException(e.Message);
}

}

public string GenerateSHA512String(string secretKey, string sequenceString)
{
byte[] hashmessage;
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secretKey);
byte[] messageBytes = encoding.GetBytes(sequenceString);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
hashmessage = hmacsha256.ComputeHash(messageBytes);
}
return Convert.ToBase64String(hashmessage);
}

private EntityCollection GetPaymentGatewayInformtion(IOrganizationService service, string venderName)
{

QueryByAttribute paymentGateway = new QueryByAttribute(“mc_paymentgatewaysettings”);
paymentGateway.ColumnSet = new ColumnSet(“mc_name”, “mc_merchantid”, “mc_secretkey”, “mc_accesscode”);
paymentGateway.Attributes.AddRange(“mc_name”);
paymentGateway.Values.AddRange(venderName);
EntityCollection paymentgatewaysettings = service.RetrieveMultiple(paymentGateway);

return paymentgatewaysettings;

}
}
}

Then finally Web API which calling the above Plugins

function GetSecureHash(requestStringForHash) {
debugger;

try
{
var secureHash;
var vendorName = “<>”;
var Id = Xrm.Page.data.entity.getId().substring(1, 37);
console.log(“Receipt Id:” + Id);
var data = {
“receiptid”: Id,
“vendorname”: vendorName,
“sequencestring”: requestStringForHash
};
var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();

req.open(“POST”, clientURL + “/api/data/v8.2/mc_receiptses(” + Id + “)/Microsoft.Dynamics.CRM.mc_GetSecureHash”, 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 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
var receipts = JSON.parse(this.response);
secureHash = receipts.securehash;
}
else {
var error = JSON.parse(this.response).error;
console.log(error.message);
}
}
};
req.send(JSON.stringify(data));
console.log(“secureHash: ” + secureHash);

}
catch (e) {
console.error(e.message);
}
return secureHash;

}

Using this Web API, we got secure hash to build the complete payment URL and then redirect to payment gateway.

Some reference URL for Action and Web API for more details
https://community.dynamics.com/crm/b/crminogic/archive/2018/01/04/fixed-executing-action-with-complex-output-parameters-through-web-api-in-dynamics-365-v9-0
Invoke your Custom Action from Dynamics CRM Web API–Dynamics CRM 2016

Hope this help you.

CrmService.Create Method Using JScript


Create record using JScript

This sample shows how to use the CrmService.Create method using the same example provided in the Server Programming Guide: CrmService.Create method.

To test this sample:

  1. Paste the following code into any Event Detail Properties dialog box.
  2. Save the form and then click Create Form on the Preview menu.

When the event occurs, the code will run. This code creates and opens a new contact record.

// Prepare values for the new contact.

var firstname = “Jesper”;

var lastname = “Aaberg”;

var donotbulkemail = “true”;

var address1_stateorprovince = “MT”;

var address1_postalcode = “99999”;

var address1_line1 = “23 Market St.”;

var address1_city = “Sammamish”;

var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.

var xml = “<?xml version=’1.0′ encoding=’utf-8′?>” +

“<soap:Envelope xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/‘”+

” xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance‘”+

” xmlns:xsd=’http://www.w3.org/2001/XMLSchema‘>”+

authenticationHeader+

“<soap:Body>”+

“<Create xmlns=’http://schemas.microsoft.com/crm/2007/WebServices‘>”+

“<entity xsi:type=’contact’>”+

“<address1_city>”+address1_city+”</address1_city>”+

“<address1_line1>”+address1_line1+”</address1_line1>”+

“<address1_postalcode>”+address1_postalcode+”</address1_postalcode>”+

“<address1_stateorprovince>”+address1_stateorprovince+”</address1_stateorprovince>”+

“<donotbulkemail>”+donotbulkemail+”</donotbulkemail>”+

“<firstname>”+firstname+”</firstname>”+

“<lastname>”+lastname+”</lastname>”+

“</entity>”+

“</Create>”+

“</soap:Body>”+

“</soap:Envelope>”;

// Prepare the xmlHttpObject and send the request.

var xHReq = new ActiveXObject(“Msxml2.XMLHTTP”);

xHReq.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);

xHReq.setRequestHeader(“SOAPAction”,”http://schemas.microsoft.com/crm/2007/WebServices/Create”);

xHReq.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

xHReq.setRequestHeader(“Content-Length”, xml.length);

xHReq.send(xml);

// Capture the result

var resultXml = xHReq.responseXML;

// Check for errors.

var errorCount = resultXml.selectNodes(‘//error’).length;

if (errorCount != 0)

{

var msg = resultXml.selectSingleNode(‘//description’).nodeTypedValue;

alert(msg);

}

// Open new contact record if no errors.

else

{

var contactid = resultXml.selectSingleNode(“//CreateResult”);

window.open(“/sfa/conts/edit.aspx?id={“+contactid.nodeTypedValue+”}”);

}

A successful response includes XML with a CreateResponse element that returns the ID for the record created. The following is an example of a successful response:

<?xml version=”1.0″ encoding=”utf-8″?>
<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;
<soap:Body>
<CreateResponse xmlns=”http://schemas.microsoft.com/crm/2007/WebServices”&gt;
<CreateResult>368c8b1b-851c-dd11-ad3a-0003ff9ee217</CreateResult>
</CreateResponse>
</soap:Body>
</soap:Envelope>

CRM 4.0: Use JavaScript execute/call/launch CRM Workflow


Run a workflow through JavaScript

/* the function */

ExecuteWorkflow = function(entityId, workflowId)

{

var xml = “” +

“<?xml version=\”1.0\” encoding=\”utf-8\”?>” +

“<soap:Envelope xmlns:soap=\”http://schemas.xmlsoap.org/soap/envelope/\” xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\”>” +

GenerateAuthenticationHeader() +

” <soap:Body>” +

” <Execute xmlns=\”http://schemas.microsoft.com/crm/2007/WebServices\”>” +

” <Request xsi:type=\”ExecuteWorkflowRequest\”>” +

” <EntityId>” + entityId + “</EntityId>” +

” <WorkflowId>” + workflowId + “</WorkflowId>” +

” </Request>” +

” </Execute>” +

” </soap:Body>” +

“</soap:Envelope>” +

“”;

var xmlHttpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);

xmlHttpRequest.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);

xmlHttpRequest.setRequestHeader(“SOAPAction”,”http://schemas.microsoft.com/crm/2007/WebServices/Execute”);

xmlHttpRequest.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

xmlHttpRequest.setRequestHeader(“Content-Length”, xml.length);

xmlHttpRequest.send(xml);

var resultXml = xmlHttpRequest.responseXML;

return(resultXml.xml);

}

/* call */

var theWorkflowId = “3FD2DD58-4708-43D7-A21B-F0F90A0AA9F2”;
//change to your workflow Id

ExecuteWorkflow(crmForm.ObjectId, theWorkflowId);

%d bloggers like this: