Dynamic Required Fields - Using JScript for CRM 3.0

During a customer, sales or service lifecycle, requirements for data entry can change as a customer goes from being a prospect to a customer, from a cold opportunity to a hot opportunity, from a inquiry case to a system down case.

 

Using Jscript and a few minutes, here is a sample script that will allow you to have the fields required on a form change depending on where they are in their rating.

 

So a Cold Opportunity has NO required fields

A Warm Opportunity has the required fields of Estimated Close Date and Price List

A Hot Opportunity has required fields of the above PLUS Probability and Estimated Revenue.

 

Again, this is for DEMO Use. It has been tested and is for use only with our demo data. If you decide to use this on your own system, you do so at your own risk.

 

So this script needs to be attached to the Opportunity Rating Field. Refer to the IG on how to do this. The four fields we required should be attached to the script as dependencies.

 

Start Script Here ---

 

switch (parseInt(event.srcElement.DataValue, 10))

{

 

    /* Opportunity Rating Picklist */

    case 1:

        /* Hot */

        crmForm.SetFieldReqLevel("pricelevelid", 1);

        crmForm.SetFieldReqLevel("estimatedvalue", 1);

        crmForm.SetFieldReqLevel("estimatedclosedate", 1);

        crmForm.SetFieldReqLevel("closeprobability", 1);

        break;      

    case 2:

        /* Warm */

        crmForm.SetFieldReqLevel("pricelevelid", 1);

        crmForm.SetFieldReqLevel("estimatedvalue", 0);

        crmForm.SetFieldReqLevel("estimatedclosedate", 1);

        crmForm.SetFieldReqLevel("closeprobability", 0);

        break;

    case 3:

    /* Cold */

       crmForm.SetFieldReqLevel("pricelevelid", 0);

       crmForm.SetFieldReqLevel("estimatedvalue", 0);

       crmForm.SetFieldReqLevel("estimatedclosedate", 0);

       crmForm.SetFieldReqLevel("closeprobability", 0);

       break;

 

    /* All other values */

    default:

       crmForm.SetFieldReqLevel("name", 0);

       crmForm.SetFieldReqLevel("customerid", 0);

       break;

 

}

Comments

  • Anonymous
    January 24, 2006
    I have tried to find documentation on the CRM DOM and I do not see the "SetFieldReqLevel" form method shown anywhere. I'm sure there are other useful form methods hidden as well. Can you please post a link to these "missing" methods? Thanks. -Keith

  • Anonymous
    January 24, 2006
    Keith,
    This script was a 1.2 script that a few changes where made for 3.0.... I will see if I can dig a few more out... :-)

    Thanks!
    Ben

  • Anonymous
    February 10, 2006
    The comment has been removed

  • Anonymous
    February 10, 2006
    The comment has been removed

  • Anonymous
    February 10, 2006
    I did find this out, my issue was that I was using a bit field and changing it to a pick list allowed me to go back and forth.  Thank you for your reponse.  One more question, how can you change the field to be business suggested instead of required?

  • Anonymous
    February 10, 2006
    Mike,
    I think you can... Try putting a 2 there... I did it for a client for 1.2, but have not played with it in 3.0....
    Thanks
    Ben

  • Anonymous
    February 13, 2006
    Mike,
    Here it is from 3.0.3 of the sdk.

    {Field}.RequiredLevel   Get property.
    Determines the level of requirement for the field. Valid values are:

    0 = No constraint (normal)

    1 = Business recommended

    2 = Business required

    Example

    var CRM_REQUIRED_LEVEL_NORMAL = 0;
    var CRM_REQUIRED_LEVEL_RECOMMENDED = 1;
    var CRM_REQUIRED_LEVEL_REQUIRED = 2;
    var oField = crmForm.all.SOME_FIELD_ID;

    switch (oField.RequiredLevel)
    {
      case CRM_REQUIRED_LEVEL_NORMAL:
         alert("This field is not required or recommended");
         break;

      case CRM_REQUIRED_LEVEL_RECOMMENDED:
         alert("This field is business recommended");
         break;

      case CRM_REQUIRED_LEVEL_REQUIRED:
         alert("This field is required");
         break;
    }

  • Anonymous
    August 21, 2006
    Hello,

    Im trying to chage the required level of a field and I dont find any script to change it. Ben, could you post the script to change it?

    Many thanks,

  • Anonymous
    August 21, 2006
    Luis,
    The script to change the required level is above.
    Ben

  • Anonymous
    August 21, 2006
    Im sorry, forgot say that y use CRM 3.0

  • Anonymous
    August 21, 2006
    The script above is for 3.0...

  • Anonymous
    August 21, 2006
    It´s true. I had a mistake. When I read the commentaries, think that the function "SetFieldReqLevel" dont run in CRM 1.2 and because of it i ask you for the function in CRM 3.0

    Many Thanks

  • Anonymous
    October 08, 2006
    // --- set required var oField = crmForm.all.yourfieldname; oField.req = 2; // --- change the label to be required var oFieldLabel = document.getElementById(oField.id + "_c"); if (oFieldLabel == null) return; // --- the following sets the correct class for the label if (oFieldLabel.className.indexOf("req") != -1) return; if (oFieldLabel.className.indexOf("rec") != -1) { oFieldLabel.className = oFieldLabel.className.replace("rec", "req"); } else { oFieldLabel.className = oFieldLabel.className + (oFieldLabel.className == "" ? "req" : " req"); }

  • Anonymous
    September 24, 2007
    So I am trying to use the code to change the type of field from business recommended to business required for certain picklist choices and then back to business recommended. After fiddling with it for an hour or so in different versions I can't get the business recommended fields to return to their original state - they only go to normal or required and won't go back to recommended. Any suggestions?

  • Anonymous
    December 05, 2007
    I had the some problem. Try this code: crmForm.all.your_field.setAttribute("req", 1); crmForm.all.your_field_c.className = "rec"; Replace "your_field" with "accountnumber" and "your_field_c" with "accountnumber_c".

  • Anonymous
    May 21, 2008
    Does the script change when moving to CRM 4.0?

  • Anonymous
    September 09, 2008
    CRM 4.0 supports the SetFieldReqLevel call.  It even changes the appearance of the field when toggled, without having to change the className manually. Example: crmForm.SetFieldReqLevel("custom_somefield",1); crmForm.SetFieldReqLevel("custom_somefield",0); ... will toggle "custom_somefield" between Required and No Constraint, each time updating the look of the field to match either the red *, or blue + without additional, fancy code.

  • Anonymous
    September 21, 2008
    I am working with crm 4.0 but crmForm.SetFieldReqLevel("custom_somefield",0); but dont change the look the field. could you helpme

  • Anonymous
    January 20, 2009
    PingBack from http://www.hilpers-esp.com/641683-campos-requeridos-en-crm-3-a

  • Anonymous
    March 15, 2009
    Hi, The code above works great and is nearly what i'm trying to achieve but I need it to.... If field1 = certain picklist values then make field2 a required field BUT only when trying to save+close the activity. Ive tried adding in a questions each time the activity is saved which works but if you save+complete then it simply closes the window if(crmForm.all.new_appointmentoutcome.DataValue==null && crmForm.all.new_appointmenttype.DataValue==(1)) { var sao=confirm("Do you know the Sales Outcome Yet?"); if (sao==true)  { crmForm.all.new_appointmentoutcome.setAttribute("req", 2); crmForm.all.new_appointmentoutcome_c.className = "req";  } else  {  } } any ideas what i can try?? Thanks