Archive for October, 2011

Email format validation in CRM

October 27, 2011 1 comment


The out of the box CRM Email field type does not validate whether the given email is ending with “.com”,”.in”,etc… (Refer below)

  • rajeevpentyala@live  (Its a valid email format as per out of the box CRM Email Type validation)

We can validate all the metrics of Email format by using below steps.

Step 1:-

  • Create a new “Field” (i.e.,new_emailaddress1) of type nvarchar(100)
  • Since the field is normal field, we have to format the field to look like “Email”
  • Place below script in form ‘onload’ event

    var emailTextBox = crmForm.all.new_emailaddress1;

//Set the field style similar to ‘Email’ (i.e., Blue forecolor and Underline) = “#0000ff”; = “underline”;

//Attach ‘on double click’ event
emailTextBox.attachEvent(“ondblclick”, openEmail);
function openEmail() { if (emailTextBox.DataValue != null) document.location = “mailto:” + emailTextBox.DataValue; }

Step 2 (Email Validation) :-

  • We can validate email format either in form’s ‘onsave’ event (or) field’s ‘onchange’ event
  • Register below function as per your requirement (i.e.,Either in ‘onsave’ or ‘onchange’)

function ValidateEmail() {
var emailID = crmForm.all.emailaddress1.DataValue;

if ((emailID.indexOf(“@”) > 0 && (emailID.lastIndexOf(“@”) != (emailID.length – 1))) || (1 == 1)) {
if (^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
else {
alert(“You must enter a valid e-mail address.”);
event.returnValue = false;

Hope it helps 🙂

Hide or Show Tabs & Navigation Items of CRM form in CRM 2011

October 27, 2011 2 comments


Below is the JScript to Show or Hide Tabs and Navigation Items on CRM form

//Set “true” to Hide and “false” to show the Tab/Navigation item



Note :-

  • We can get the ‘Tab’ name, from the Form customization
  • We can get the “Navigation link” name using the IE developer tool (i.e., On the browser Press F12)

Hope it helps 🙂

Activate or Deactivate record in CRM 2011

October 18, 2011 4 comments


We can Activate\Deactivate record using “SetStateRequest“. Below is the code snippet.

private void SetEntityStatus(IOrganizationService service, Guid recordGUID, string entityName)        {

SetStateRequest setState = newSetStateRequest();

setState.EntityMoniker = newEntityReference();

//Pass GUID of the record to be activated or Deactivated

setState.EntityMoniker.Id = recordGUID;

setState.EntityMoniker.Name = entityName;

setState.EntityMoniker.LogicalName = entityName;

//Setting ‘State’ i.e., (0 – Active ; 1 – InActive)

setState.State = new OptionSetValue();

setState.State.Value = 0/1;

//Setting ‘Status’ i.e., (1 – Active ; 2 – InActive)

setState.Status = new OptionSetValue();

setState.Status.Value = 1/2;

SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);


Hope it helps 🙂

Set field values using query string parameters on Form load in CRM

October 11, 2011 Leave a comment


I have got below requirement,

  •  I need to open a CRM form from my custom ‘.aspx’ page.
  • On opening of the form I should populate “name” field with value

After referring MSDN article, I came to know that we can set the field values using “extraqs” query string parameter and it must meet below requirements.

  • You must encode the parameters passed in the extraqs parameter. Use encodeURIComponent to encode the parameters.
  • The names of the query string arguments must match or include the names of attributes for the entity.
  • The value cannot be a script (To prevent script injection)

Below is the sample JScript to open a form and populate “name” field on form load

function openform() {
var encodedString = encodeURIComponent(“new_name=rajeev pentyala”);“http://ServerName/OrgName/main.aspx?etc=10014&extraqs=” + encodedString + “&pagetype=entityrecord”);

  • Here “new_name” is the name of attribute

You get the CRM form as below, when you call the above function

extraqs query parameter

extraqs query parameter

Please refer below useful MSDN articles

Hope it helps 🙂

Execute the workflow from JScript In CRM

October 2, 2011 1 comment


We can run the CRM workflows from JScript, if we know the GUID of the workflow. Below are the execution steps.

Steps :-

  • Get the GUID of workflow from name using OData Service  (i.e., Using GetWorkflowIDByName(wfName) function )
  • Execute the Workflow using the GUID (i.e., Using TriggerWorkflow(workflowGuid) function)

Below are the JScript functions mentioned in above steps.

function GetWorkflowIDByName(wfName) {

var serverUrl = Xrm.Page.context.getServerUrl();

//Prepare ODATA query to fetch WF GUID by WF Name

var odataSelect = serverUrl + “/xrmservices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=ActiveWorkflowId/Id ne null and Name eq ‘” + wfName + “‘”;



contentType:“application/json; charset=utf-8”,


url: odataSelect,

beforeSend:function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader(“Accept”, “application/json”); },

success:function(data, textStatus, XmlHttpRequest) {



error:function (XmlHttpRequest, textStatus, errorThrown) { alert(‘OData Select Failed: ‘+ odataSelect); }



function RetrieveWorkflow(Entity) {

var objFilteredWF = null;

objFilteredWF = Entity;

wfID =null;

//Get the WF GUID from OData result set

if (objFilteredWF != null && objFilteredWF.results != null && objFilteredWF.results.length != 0 && objFilteredWF.results[0].WorkflowId != null) {

wfID = objFilteredWF.results[0].WorkflowId;




//Trigger WF using WorkflowGUID

function TriggerWorkflow(workflowGuid) {


var soapBody = “<soap:Body>”+” <Execute xmlns=’’>&#8221;

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

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

+” <WorkflowId>” + workflowGuid + “</WorkflowId>”

+” </Request>”

+” </Execute>”


/*Wrap the Soap Body in a soap:Envelope.*/

var soapXml = “<soap:Envelope “

+” xmlns:soap=’; “

+” xmlns:xsi=’; “

+” xmlns:xsd=’’>&#8221;

+ GenerateAuthenticationHeader()

+soapBody +


/* Create the XMLHTTP object for the execute method.*/

var xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”);“POST”, “/MSCRMservices/2007/crmservice.asmx”, false);

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


/* Send the XMLHTTP object. */



catch(e) {