Archive

Posts Tagged ‘CRM 4.0’

Email format validation in CRM

October 27, 2011 1 comment

Hi,

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)
emailTextBox.style.color = “#0000ff”;
emailTextBox.style.textDecoration = “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 (emailID.search(/^\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.”);
crmForm.all.emailaddress1.SetFocus();
event.returnValue = false;
}
}
}

Hope it helps 🙂

Execute the workflow from JScript In CRM

October 2, 2011 1 comment

Hi,

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 + “‘”;

$.ajax({

type:“GET”,

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

datatype:“json”,

url: odataSelect,

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

success:function(data, textStatus, XmlHttpRequest) {

RetrieveWorkflow(data.d);

},

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;

TriggerWorkflow(wfID);

}

}

//Trigger WF using WorkflowGUID

function TriggerWorkflow(workflowGuid) {

try{

var soapBody = “<soap:Body>”+” <Execute xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>&#8221;

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

+” <EntityId>” + Xrm.Page.data.entity.getId() + “</EntityId>”

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

+” </Request>”

+” </Execute>”

+“</soap:Body>”;

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

var soapXml = “<soap:Envelope “

+” xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/&#8217; “

+” xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance&#8217; “

+” xmlns:xsd=’http://www.w3.org/2001/XMLSchema’>&#8221;

+ GenerateAuthenticationHeader()

+soapBody +

“</soap:Envelope>”;

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

var xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”);

xmlhttp.open(“POST”, “/MSCRMservices/2007/crmservice.asmx”, false);

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

xmlhttp.setRequestHeader(“SOAPAction”, http://schemas.microsoft.com/crm/2007/WebServices/Execute);

/* Send the XMLHTTP object. */

xmlhttp.send(soapXml);

}

catch(e) {

}

}

🙂

How to change CRM form field’s label text and color using Jscript

September 15, 2011 1 comment

Hi,

Below is the sample JScript to change the color and text of  CRM form field’s  label; Place the code in JScript “onload” event

// To change color

if(crmForm.all.new_fieldname != null) {

var field = crmForm.all.new_fieldname_c; //”_c” is the caption (i.e.,Label)

if (field != null)

field.style.color = ‘gray’; //Specify your desired color

}

Same script work for both CRM 4.0 & CRM 2011

// To set the label

Xrm.Page.ui.controls.get(fieldname).setLabel(‘New label’);

🙂