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.

Wednesday, May 16, 2012

Formatting postal codes for USA for Dynamics CRM 2011

To Standardize & GUI Enhancement, I have implemented the following script to re-format the user input of postal codes for USA. Here I am collecting user’s input, and re-arranging it to follow a standard for zip code which is #####-####..
By default, Zip/Postal code field in Dynamics CRM is a free-form text field.  Requirement is that when a user types in a Zip Code like 432201234, it should automatically format the field to 43220-1234".
Dynamics CRM uses JavaScript for client side scripting and we can use Regular Expression to collect the data and match with the expression and if the format does not match then enforce to format. I am using (^\d{5}$)|(^\d{9}$)|(^\d{5}-\d{4}$) to match, whether user has typed in 5 characters or 9 characters or it is already in a US format Zip code like 43220-1234. If 5 then do not do anything, if 9, format it and if it is already in a US format Zip code like 43220-1234 do not do anything.

Now, follow the steps and screen shots.

1 - Create a web resource in CRM



2. Use the text editor to put the following JavaScript:
// Function to format Zip Code in USA Format XXXXX-XXXX
// Created by : Sudip Chakrovorty Dated : 5/15/2012
function FormatPostalCode(context)
{
var oField = context.getEventSource().getValue();
var tmp = oField;
    if(typeof(oField) != "undefined" && oField != null)
  {
    // check for US ZIP code format
    if(oField.match(/(^\d{5}$)|(^\d{9}$)|(^\d{5}-\d{4}$)/))
    {
        tmp = oField;
               
if(tmp.match(/^\d{9}$/))
    {
      tmp = tmp.substr(0,5) + "-" + tmp.substr(5,9);
      context.getEventSource().setValue(tmp);
      return true;
    }
}
else

    alert("Zip Code must contain 5 or 9 numbers.");
    //context.getEventSource().setValue("");
         }
}

3. Add your JavaScript into Form.



4 – Add a Reference of the Web resource in the field where you want to implement. Go to the properties of the field; Set the "form libraries" to above code. Then add an "event handler" for the Zip Code field's OnChange() event.   In the "function" field, use "FormatPostalCode".    Also, click on the "pass execution context as first parameter" check box.



And you are done.
Enjoy!

Thursday, July 7, 2011

Multiselect option set in CRM 2011

Multiselect option set in CRM 2011


Please refer below Code for implementation
new_subject :  is option set type field
 new_subject_value: is Text field which holds the selected values
 
new_subject  and new_subject_value will be hidden by JavaScript.
onload () we will call   “function Onload_Entity()”
and  onSave() we will call “ function OnSave_Entity()”.
//--------------------------------------------Code ---------------------------------------------------------------------//
var var_new_picklistacc = 'new_subject';
var var_new_picklistvalueacc = 'new_subjectvalue';

//Method to convert picklist to multi select picklist OnLoad of entity
function Onload_Entity()
{
var PL = document.all.new_subject;
var PLV = document.all.new_subjectvalue;
Xrm.Page.getControl('new_subjectvalue').setVisible(false);
ConvertToMultiSelectext(PL,PLV,"80");
}

// Save the selected text, this field can also be used in Advanced Find
function OnSave_Entity()
{
    //Update >> Provide picklist schema name
    var PL = document.all.new_subject;  OnSaveext(PL,var_new_picklistvalueacc);
}

///////Supported functions
function ConvertToMultiSelectext(p_PL,p_PLV,ht)
{
  // Create a DIV container
  var addDiv = document.createElement("<div style='overflow-y:auto; height:"+ht+"px; border:1px #6699cc solid; background-color:#ffffff;'
/>");
  p_PL.parentNode.appendChild(addDiv);





 // Initialise checkbox controls
  for( var i = 0; i < p_PL.,"length; i++ )
  {
    var pOption = p_PL.options[i];
    if(pOption.text!="") {
    if( !IsChecked( pOption.text , p_PL, p_PLV)  )
      var addInput = document.createElement("<input type='checkbox' style='border:none; width:25px; align:left;' />" );
    else
    var addInput = document.createElement("<input type='checkbox' checked='checked' style='border:none; width:25px; align:left;' />" );

    var addLabel = document.createElement( "<label />");
    addLabel.innerText = pOption.text;

    var addBr = document.createElement( "<br/>"); //it's a 'br' flag

    p_PL.nextSibling.appendChild(addInput);
    p_PL.nextSibling.appendChild(addLabel);
    p_PL.nextSibling.appendChild(addBr);
       }
  }
}
  // Check if it is selected
  function IsChecked( pText , PL, PLV)
  {
     var PLV1 = PLV.innerText;
    if(PLV1 != "")
    {
      var PLVT = PLV1.split(";");
      for( var i = 0; i < PLVT.length; i++ )
      {
        if( PLVT[i] == pText )
          return true;
      }
    }
        return false;
  }

function OnSaveep_var_new_picklistvaluext()
{
    var PL = document.all.new_subject;
    var getInput = PL.nextSibling.getElementsByTagName("input");
    var result = '';
    for( var i = 0; i < getInput.length; i++ )
    {
      if( getInput[i].checked)
      {
        result += getInput[i].nextSibling.innerText + ";";
      }
    }

    //save value
    control = Xrm.Page.getControl(var_new_picklistvalueacc);
    attribute = control.getAttribute();
    attribute.setValue(result);
}

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...