Tuesday 11 February 2014

CrmService.Create Method Using JScript

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



// 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 = "" +
"" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+
authenticationHeader+
""+
""+
""+
""+address1_city+""+
""+address1_line1+""+
""+address1_postalcode+""+
""+address1_stateorprovince+""+
""+donotbulkemail+""+
""+firstname+""+
""+lastname+""+
"
"+
"
"+
"
"+
"
";
// 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:





368c8b1b-851c-dd11-ad3a-0003ff9ee217



Changing the title of a CRM form

This will cahange the title of the crm From
document.title = "Hello World!";

Sunday 9 February 2014

Adding additional values to duration fields

The following script adds the new option "42 Minutes" to the actualdurationminutes field in a task:

var duration = crmForm.all.actualdurationminutesSelect;

var tables = duration.getElementsByTagName("table");
var table = tables[1];

var row = table.insertRow();
var newOption = row.insertCell(-1);

var newValue = "42 Minutes";
newOption.setAttribute("val", newValue);
newOption.innerText = newValue;

Saturday 8 February 2014

Replacing the content of an IFRAME

If you really want to do some funny things in your CRM form, you can create an IFRAME serving as a placeholder for your real HTML code. Create an IFRAME in an entity and name it "IFRAME_TEST". In the Onload event put the following code:

crmForm.all.IFRAME_TEST_d.innerHTML ="Some HTML text";


Note the "_d" at the end of IFRAME_TEST. This single line replaces the whole IFRAME element with your own HTML.

Friday 7 February 2014

Standard XmlHttpRequest using JScript


// Standard XmlHttpRequest using JScript
sendXmlHttpRequest = function (xml){

//send the xml request and return the responseXML
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "http://service url", false);
xmlHttpRequest.setRequestHeader("SOAPAction",'http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple');
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
if (xmlHttpRequest.responseXML == null || xmlHttpRequest.responseXML.xml == null || xmlHttpRequest.responseXML.xml == "") {
if (xmlHttpRequest.responseText != null && xmlHttpRequest.responseText != "")
throw new Error(xmlHttpRequest.responseText);
else
throw new Error("Error returning response");
}
var responseXML = xmlHttpRequest.responseXML;
if (xmlHttpRequest.responseXML.documentElement.selectNodes("//error/description").length > 0) {
throw new Error(responseXML.xml);
}

return responseXML;
}