Module in JavaScript

Example fo creating a module in Javascript

Class definition in Javascript

defining a class in Jscript

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!";

JavaScript StringBuilder

a simple StringBuilder class that pushes individual strings into an array and then uses the join method to produce the concatenated output string. The code is simple and straightforward that it should be self-explanatory. Now here's an example of how to use it:
// create a StringBuilder
var sb = new StringBuilder();

// append some text
sb.append("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, ");
sb.append("sed diem nonummy nibh euismod tincidunt ut lacreet dolore ");
sb.append("magna aliguam erat volutpat.");

// get the full string value
var s = sb.toString();

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;

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.