Sunday, February 21, 2010

checking duplicate detection using custom web service in ms crm 4.0

Here is the web method for duplicate detection web service:


[WebMethod]
public string CheckDuplicateAccount(string objectguid, string name)
{
CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
if(objectguid == null || objectguid == string.Empty )
{
//Code for checking account records at creation time
try
{
QueryExpression query = new QueryExpression();
query.EntityName = EntityName.account.ToString();// Create a set of columns to return.
ColumnSet cols = new ColumnSet();
cols.Attributes = new string [] {“name”};// Create the ConditionExpression.
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = “name”;
condition.Values = new string[] {name};// Builds the filter based on the condition
FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] {condition};
query.ColumnSet = cols;
query.Criteria = filter;// Retrieve the values from Microsoft CRM.
BusinessEntityCollection retrieved = service.RetrieveMultiple(query);
if(retrieved.BusinessEntities.Length > 0 )
{
return “Duplicate record exists for : ” + name;
}else
{
return “”;
}
}
catch(System.Web.Services.Protocols.SoapException se)
{
return “ERROR: ” + se.Detail.InnerText;
}
}
else {//Code for checking account records at updation time
try{
QueryExpression query = new QueryExpression();
query.EntityName = EntityName.account.ToString();// Create a set of columns to return.
ColumnSet cols = new ColumnSet();
cols.Attributes = new string [] {“name”};// Create the ConditionExpression.
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = “name”;
condition1.Values = new string[] {name};
condition1.Operator = ConditionOperator.Equal;
ConditionExpression condition2 = new ConditionExpression();
condition2.AttributeName = “accountid”;
condition2.Values = new string[] {objectguid};
condition2.Operator = ConditionOperator.NotEqual;// Builds the filter based on the condition
FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] {condition1, ondition2};
query.ColumnSet = cols;
query.Criteria = filter;// Retrieve the values from Microsoft CRM.
BusinessEntityCollection retrieved = service.RetrieveMultiple(query);
if(retrieved.BusinessEntities.Length > 0 )
{
return “Duplicate record exists for : ” + name;
}
else
{
return “”;
}
}catch(System.Web.Services.Protocols.SoapException se)
{
return “ERROR: ” + se.Detail.InnerText;
}
}


call this web method using java script .

Hiding a section using java script in MS CRM 4.0

Hidding a section which contains an IFrame :
var sec = document.getElementById( "tab5"  );
            sec.childNodes[0].rows[ 1 ].style.display = 'none';
           


=======================================================================
Hiding a section with attributes on it :


crmForm.all..parentElement.parentElement.parentElement.style.display='none';



getting the parameters from the current url using java script in MS CRM 4.0

getting the parameters  from the current url using java script:


var id= GetParam(‘id’);


 function GetParam(name) {            
          name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
         var regexS = "[\\?&]" + name + "=([^&#]*)";
         var regex = new RegExp(regexS); 
        var results = regex.exec(window.location.href); 
       if (results == null) return ""; 
      else return results[1]; 
  }

changing the background color of a Form & Tab in MS CRM 4.0

use the following Java script on page load event :


document.all.MyForm.style.backgroundColor = '#ccffcc';


document.all.tab0.style.backgroundColor = '#ccffcc';
crmForm.all.tab0Tab.style.backgroundColor='#ccffcc'
document.all.tab1.style.backgroundColor = '#ccffcc';
document.all.tab2.style.backgroundColor = '#ccffcc';
document.all.tab3.style.backgroundColor = '#ccffcc';


change new_label field label style


var field = crmForm.all.new_label_c;
  field.style.fontWeight = 'bold'; // change font to bold
  field.style.fontSize = '12px'; // change font size
  field.style.color = '#ff0000';  //change font color


Adding Custom Button on the Entity Entry part in MS CRM 4.0

Add the following code in the page load Event of Entity.

crmForm.all.sampleattribute.innerHTML = ''<button class='ms-crm-Button' onclick='ButtonClick();'>custom Button</button>'';


crmForm.all.sampleattribute_c.style.display = 'none';



ButtonClick= function() {
alert('alert from custom button click');
}