Tuesday, July 16, 2013

Validting phone number to US Format in Dynamics CRM 2011

Requirement: The phone number field should accept only 10 digits and should format in US phone number format (ex: XXX-XXX-XXXX)
This requirement can be completed with some java script:
// Function To validate the Mobile Phone field
function validateMPhone()
{
var phone = Xrm.Page.getAttribute("new_mobilephone").getValue();
var sTmp = phone.replace(/[^0-9]/g, "");
phoneRegex = /^\d{10}$/;

if( !sTmp.match( phoneRegex ) )
{
event.returnValue = false;
alert("Phone must contain 10 numbers.") ;
Xrm.Page.getAttribute('new_mobilephone').setValue('');
}
else
{
var sTmpClean =  "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
Xrm.Page.getAttribute("new_mobilephone").setValue(sTmpClean);
}
}

Here is the step by step procedure:
1. Create a javascript web resource in the given solution .
2. Click the Text editor button and copy the above code in there.
3. Go to the Field properties of the field you want to validate.
4. Click on "Event" tab.
5. Under 'Form Library' Click on "Add" tab and  add the web resource.
6. In the Event Handelers, Select 'OnChange'  for the event and then click 'Add'.
7. Add the Function name validateMPhone and click on OK.
8. Publish the Solution.
When you enter a phone number which is not equal to 10 digits you will receive a message box and clicking 'Okay'.
The focus will be in the Phone field and the field would be cleaned up.

Display label of option set based on value

Display label of option set based on value


string GlobaloptionsetText = GetCRMOptionSetLabel(service, entity.LogicalName, "new_rating", irating);


   private string GetCRMOptionSetLabel(IOrganizationService service, string entityname, string optionsetname, int value)
        {
            Microsoft.Xrm.Sdk.Messages.RetrieveAttributeRequest reqOptionSet = new Microsoft.Xrm.Sdk.Messages.RetrieveAttributeRequest();
            reqOptionSet.EntityLogicalName = entityname;
            reqOptionSet.LogicalName = optionsetname;
            Microsoft.Xrm.Sdk.Messages.RetrieveAttributeResponse resp = (Microsoft.Xrm.Sdk.Messages.RetrieveAttributeResponse)service.Execute(reqOptionSet);
            Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata opdata = (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)resp.AttributeMetadata;
            var option = opdata.OptionSet.Options.FirstOrDefault(o => o.Value == value);

            return option.Label.LocalizedLabels.FirstOrDefault().Label;
        }

Copy Records from an entity to another entity through Plugin in Dynamics CRM 2011

Copy Records from an entity to another entity through Plugin in Dynamics CRM 2011
Requirement: Suppose you have to copy a record from one entity another, Lets say you have one custom entity called prospect with few attributes like Pipelinestatus, Rating, Firstname, Lastname. And you want to convert this Prospect Record to Lead means a new record in Lead entity copying over the all the attributes value from Prospect to Lead.
Following is the Plugin that can help you to create a copy of record:
Etity lead = new Entity("lead");

ColumnSet cols = new ColumnSet();
                                                                                 cols.AddColumns(new string[] { "new_pipelinestatus", "new_rating", "new_lastname", "new_firstname", "new_businessphone", "new_addressstreet1", "new_city", "new_stateprovince", "new_zippostalcode", "new_updatecounter" });

 entity = service.Retrieve("new_prospect", entity.Id, cols);

string status = "";
int irating = 0;
int count = 0;
if (entity.Attributes.Contains("new_pipelinestatus"))
 {
  //rating Please Select One=0,10%=1,20%=2,30%=3,40%=4,50%=5
irating = Convert.ToInt16(((OptionSetValue)entity.Attributes["new_rating"]).Value.ToString());
status = ((OptionSetValue)entity.Attributes["new_pipelinestatus"]).Value.ToString();
count = Convert.ToInt16(entity.Attributes["new_updatecounter"]);
count = count + 1;
//status P=1,L=2,C=3
   if (status == "1")
         {
  throw new InvalidPluginExecutionException("PipelineStatus Should be L or C to convert to Lead ");
           }
  if (irating == 0 || irating == 1)
         {
       throw new InvalidPluginExecutionException("Rating Should be greater than 10 To convert to Lead ");
          }
       }                         
  string GlobaloptionsetText = GetCRMOptionSetLabel(service, entity.LogicalName, "new_rating", irating);

                                            
if (entity.Attributes.Contains("new_lastname"))
lead["lastname"] = entity.Attributes["new_lastname"];

if (entity.Attributes.Contains("new_firstname"))
lead["firstname"] = entity.Attributes["new_firstname"];

if (entity.Attributes.Contains("new_businessphone"))
lead["telephone1"] = entity.Attributes["new_businessphone"];

if (entity.Attributes.Contains("new_addressstreet1"))
lead.Attributes["address1_line1"] = entity["new_addressstreet1"];

if (entity.Attributes.Contains("new_city"))
lead.Attributes["address1_city"] = entity["new_city"];

if (entity.Attributes.Contains("new_stateprovince"))
lead.Attributes["address1_stateorprovince"] = entity["new_stateprovince"];

if (entity.Attributes.Contains("new_zippostalcode"))
lead.Attributes["address1_postalcode"] = entity["new_zippostalcode"];

Guid Lid=  service.Create(lead);


Copy all activities associated with an entity to another entity through Plugin in Dynamics CRM 2011

Copy all activities associated with entity to other entity thru Plugin in MS Dyanmcis

Requirement: Suppose you are copying over a record from an entity to another entity and you wanted to copy over the realted activities as well. Suppose you a custom Prospect entity record which have 2 task activites associated with, if you have to copy both tasks to another entity lets says Lead entity along with data.
Following is the Plugin you can use on Update Event.

if (entity.LogicalName == "new_prospect")
           {

  try
      {
         Entity lead = new Entity("lead");

         ColumnSet cols = new ColumnSet();
         cols.AddColumns(new string[] { "new_pipelinestatus", "new_rating", "new_lastname", "new_firstname", "new_businessphone", "new_addressstreet1", "new_city", "new_stateprovince", "new_zippostalcode", "new_updatecounter" });

entity = service.Retrieve("new_prospect", entity.Id, cols);

Guid Lid=  service.Create(lead);

 // copy activities
 var query = new QueryExpression("task");
query.ColumnSet = new ColumnSet(true);

//regardingobjectid this will get all activities of Prospect entity.
query.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, entity.Id));
                                           
                                                                                        

var activities = service.RetrieveMultiple(query).Entities;

foreach (var activity in activities)
{
  activity.Attributes.Remove("activityid");
  activity.Id = Guid.NewGuid();
  string regardingaccountype = "lead";
  activity["regardingobjectid"] = new EntityReference(regardingaccountype, Lid);

  service.Create(activity);

}

 

Random Number Generation through plug-in in MS Dynamics 2011

The Requirement is to generate a Unique Random Auto-generated Number while creating a record in Custom Prospect entity.

public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service =  (IOrganizationService)factory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                int count = 0;
                if (entity.LogicalName == "new_prospect")
                {
                    // A Prospect number attribute should not already exist because
                    // it is system generated.
                 

                    if (entity.Attributes.Contains("new_prospectnumber") == false)
                    {
                        // Create a new Prospect number attribute, set its value, and add
                        // the attribute to the entity's attribute collection.
                        Random rndgen = new Random();
                        entity.Attributes.Add("new_prospectnumber", rndgen.Next().ToString());
                        entity.Attributes.Add("new_updatecounter", count.ToString());
                       
                    }
                    else
                    {
                        // Throw an error, because Prospect numbers must be system generated.
                        // Throwing an InvalidPluginExecutionException will cause the error message
                        // to be displayed in a dialog of the Web application.
                        throw new InvalidPluginExecutionException("The Prospect number can only be set by the system.");
                    }
                }
            }
        }

Thursday, June 20, 2013

Code to keep the count of a Record has been modified


Hello All,

An interesting request came from customer to find a way that how many times a record has been updated after creation and if Plug-in can be avoided.

I just implemented the request with small JavaScript.
Here is the code that you have write on the load of the form:

updatecounter is the function that determines if the Form is Create Mode or Update mode and then set the value to a custom field in the form called "Counter".




























Javascript Function:

function updatecounter()
{
var FormType = Xrm.Page.ui.getFormType();
var count = Xrm.Page.getAttribute("Replace_with_Your_Field_Name").getValue();

if (FormType == 1)  // Checks if the form is in create mode

{

Xrm.Page.getAttribute("Replace_with_Your_Field_Name").setValue('0'); // Sets the value of the field to 0
Xrm.Page.getAttribute("Replace_with_Your_Field_Name").setSubmitMode("always"); // Force submit as field is read only.

}

if (FormType == 2 && Xrm.Page.data.entity.getIsDirty()) // Checks if the form is in update mode and if anything changed in the form.

{
    count = parseInt(count)+1;
    count = count.toString();
    Xrm.Page.getAttribute("Replace_with_Your_Field_Name").setValue(count);
    Xrm.Page.getAttribute("Replace_with_Your_Field_Name").setSubmitMode("always");
}
}

Save the customization and Publish.

This is pretty much it is.

Result :



















Thank You at looking at my post.




Wednesday, July 18, 2012

Close Form using JavaScript in Dynamics CRM 2011

I came across with a requirement where i have to Hide the Item from Fly-Out Anchors/ Short cuts in CRM, Because You can not remove Items from Fly-Outs, the work around is to Close the form with a User-friendly message using JavaScript on load of the form. You can Security based code also. Closing the UI is not posiible through Plug-Ins, Only possible way to do this using Javascript.

The Close method is available for CRM UI : Xrm.Page.ui.close();

Using this method will a prompt with a dialog box (Find the image below)



To get rid of this Dialog box is to implement getSubmitMode method of attribut, Following the code below to implement it.

Create a Webresource : new_CloseForm

add the following Javascript

function Close()
{
 var attributes = Xrm.Page.data.entity.attributes.get();
  for (var x in attributes)
    {       attributes[x].setSubmitMode("never"); }
alert ("XXX Form is not available for Current logged-in User");
Xrm.Page.ui.close();
}

Add the event method as Close to the desired entity's form. Find the image



Hope this helps.

Enhancing Model-Driven App Views with Custom Column Icons

Enhancing Model-Driven App Views with Custom Column Icons A Hidden Feature That Can Make Your Views Much More User-Friendly I came acro...