Javascript to Create A Contact In Microsoft CRM 2011
This is a Javascript customization that will create a Contact in Microsoft CRM. We didn't write this code (see author information below), but we have personally tested and implemented this code and we know it works. The code was tested using Microsoft CRM 2011 online and Microsoft CRM 2011 on-premise.
// Author: Cornel Croitoriu // Web: www.Biz-Forward.com / www.Croitoriu.net
// BEGIN CODE:
//Base function to create new Contact
//To add more fields, just change the line that starts with 'var fields='
function CreateContact()
{
var fields = [new CRMField('firstname', 'Lady'), new CRMField('lastname', 'Gaga')];
return CreateRecord('contact', fields);
}
//NOTE: the rest of this code is used/called by the above function.
//Don't edit anything below this.
// Create Record
function CreateRecord(entityName, fields)
{
try {
var resultArray = new Array();
var attributesList = '';
for( i = 0; i < fields.length; i++ ){
if (fields[i].Value != null)
attributesList += "<"+fields[i].SchemaName+">" + fields[i].Value + "</"+fields[i].SchemaName+">";
}
var xml = "<Create xmlns='http://schemas.microsoft.com/crm/2007/WebServices'><entity xsi:type='"+entityName+"'>" + attributesList + "</entity></Create>";
var resultXml = CallCrmService(xml, 'Create');
if (resultXml) {
var newid = resultXml.selectSingleNode('//CreateResult').nodeTypedValue;
return newid;
}
}
catch(err) {
}
return null;
}
// Call Crm Service
function CallCrmService(soapBody, method)
{
try {
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", '/mscrmservices/2007/CrmService.asmx', false); //synchronous
xmlHttpRequest.setRequestHeader("SOAPAction", 'http://schemas.microsoft.com/crm/2007/WebServices/' + method); //Fetch,Execute,Create
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
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>" + soapBody + "</soap:Body></soap:Envelope>";
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
return null;
}
return resultXml;
}
catch(err) {
}
return null;
}
// Make Struct
function MakeStruct(names) {
try {
var names = names.split(' ');
var count = names.length;
function constructor()
{
for (var i = 0; i < count; i++)
this[names[i]] = arguments[i];
}
return constructor;
}
catch(err)
{
}
}
// Global Structs
var FilterBy = MakeStruct("SchemaName Operator Value");
var ViewColumn = MakeStruct("SchemaName Width");
var CRMField = MakeStruct("SchemaName Value");
var MetadataObject = MakeStruct("SchemaName DisplayName");