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

CRM 4.0 IFrame: Show Entity’s Associated View


It’s a common requirement to show entity’s associated view (1:N, N:N) in IFrame, the below code works for both 1:N and N:N relationship, it also works on both On-Premise and IFD deployment. All you need to do is find out (IE Developer Toolbar) the ID of the associated link.

The 1:N relationship needs these parameters in the request URL: oId, oType, security, tabSet
The N:N relationship needs an extra parameter: roleOrd in the request URL, which has been involved in the code.

var navId = “nav_new_new_myentity_account”;

if(document.getElementById(navId) != null)
{
var tmp = document.getElementById(navId).onclick.toString();
tmp = tmp.substring(tmp.indexOf(“‘”)+1, tmp.indexOf(“;”));
var loadArea = tmp.substring(0, tmp.indexOf(“‘”));
var roleOrd =  (tmp.indexOf(“roleOrd”) == -1) ? -1 : tmp.substring( tmp.indexOf(“roleOrd”), tmp.lastIndexOf(“‘”)).replace(“\\x3d”, “=”);
crmForm.all.IFRAME_view.src = (roleOrd == -1) ? GetFrameSrc(loadArea) : GetFrameSrc(loadArea) + “&” + roleOrd;

}

function GetFrameSrc(tabSet)
{
if (crmForm.ObjectId != null)
{
var id = crmForm.ObjectId;
var type = crmForm.ObjectTypeCode;
var security = crmFormSubmit.crmFormSubmitSecurity.value;
var path = document.location.pathname.substring(0, document.location.pathname.indexOf(“edit.aspx”)) + “areas.aspx?”;

return (path + “oId=” + id + “&oType=” + type + “&security=” + security + “&tabSet=” + tabSet);
}
else
{
return “about:blank”;
}
}

%d bloggers like this: