locked
Javascript for an onChange event to update the probability field RRS feed

  • Question

  • I would like to know what the proper script would be to have the probability field auto-update on an onChange event. This update would be based on the value that is chosen in the sales stage (which is in the form of a picklist) of an opportunity. 
     
    The following is what I currently have written although I tried to put it in the OnChange event for the probabiliy field and I kept getting an error.
    var stage = crmForm.all.salesstage;
    // Set the value of the Probability field.
    crForm.all.probability = ‘0’;
    If (stage = ‘3 – Qualified Interest’)
    {
      crmForm.all.probability = ‘5’;
    }
    If (stage = ‘4 – Long List’)
    {
      crmForm.all.probability = ‘10’;
    }
    If (stage = ‘5 – Short List’)
    {
     crmForm.all.probability = ‘30’;
    }
    If (stage = ‘6 – Short List Leader’)
    {
      crmForm.all.probability = ‘50’;
    }
    If (stage = ‘7 – Agreement’)
    {
      crmForm.all.probability = ‘75’;
    }
    If (stage = ‘8 – Won’)
    {
      crmForm.all.probability = ‘100’;
    }
    Thank you!
    Thursday, May 7, 2009 9:13 PM

Answers

  • AKMSS,

    In your current example you pass the value of the field, closeprobability into a variable.  Then you update that value of the variable with something else if a certain condition is met.  However, "probability" is a variable, NOT the actual field, so this code actually isn't updating the field, just the variable.  If you added:

    crmForm.all.closeprobability.DataValue = probability;

    Then you should be all set.  Just watch out for passing number inside of quotes into that closeprobability field, as it wants numbers NOT strings. 

    Fronk
    http://dmcrm.blogspot.com
    • Marked as answer by AKMSS Tuesday, May 12, 2009 3:58 PM
    • Unmarked as answer by AKMSS Tuesday, May 12, 2009 3:59 PM
    • Marked as answer by Donna EdwardsMVP Thursday, May 14, 2009 2:07 PM
    Monday, May 11, 2009 7:49 PM

All replies

  • AKMSS,

    Your problem is a number of syntax errors.  To set the value of a field you need to use .DataValue and to get the text that has been selected from a picklist/dropdown you need to use .SelectedText.  Also, the if's need a double equal (==) in order to compare.  Here's your modified code:

    var stage = crmForm.all.salesstage.SelectedText;
    // Set the value of the Probability field.
    crmForm.all.probability.DataValue = "0";
    if (stage == "3 – Qualified Interest")
    {
      crmForm.all.probability.DataValue = 5;
    }
    if (stage == "4 – Long List")
    {
      crmForm.all.probability.DataValue = 10;
    }
    if (stage == "5 – Short List")
    {
     crmForm.all.probability.DataValue = 30;
    }
    if (stage == "6 – Short List Leader")
    {
      crmForm.all.probability.DataValue = 50;
    }
    if (stage == "7 – Agreement")
    {
      crmForm.all.probability.DataValue = 75;
    }
    if (stage == "8 – Won")
    {
      crmForm.all.probability.DataValue = 100;
    }

    Also, I typicially use double quotes and not single quotes, but as long as you're consistent JScript shouldn't care.  Give the code a try and see if that works for you.

    Fronk
    http://dmcrm.blogspot.com
    Friday, May 8, 2009 2:25 PM
  • Hi,

    Thank you. I think this is very close. Although when I put it in the onChange event, while I'm not getting an error message the probability field is still not populating with any of the values.

    WHen I open my test opportunity and save and close the field simply stays blank.

    I'm not sure what I'm missing. The following modification is what I have.

    var salesstage = crmForm.all.salesstagecode.SelectedText;
    // Set the value of the Probability field.
    crmForm.all.closeprobability.DataVaule = "null";
    if (salesstage == "1 - Suspect")
    {
    crmForm.all.closeprobabillity.DataValue = "0";
    }
    if (salesstage == "2 - Prospect")
    {
    crmForm.all.closeprobability.DataValue = "0";
    }
    if (salesstage == "3 – Qualified Interest")
    {
      crmForm.all.closeprobability.DataValue = "5";
    }
    if (salesstage == "4 – Long List")
    {
      crmForm.all.closeprobability.DataValue = "10";
    }
    if (salesstage == "5 – Short List")
    {
     crmForm.all.closeprobability.DataValue = "30";
    }
    if (salesstage == "6 – Short List Leader")
    {
      crmForm.all.closeprobability.DataValue = "50";
    }
    if (salesstage == "7 – Agreement")
    {
      crmForm.all.closeprobability.DataValue = "75";
    }
    if (salesstage == "8 – Won")
    {
      crmForm.all.closeprobability.DataValue = "100";
    }
    Friday, May 8, 2009 8:05 PM
  • AKMSS,

    You're putting quotes around the numbers.  The picklist data value needs to be an INT.  So, when you put quotes around the numbers you are passing a string to an INT, which isn't valid.  Also, make sure that your check on the text is exactly right, spaces, dashes, etc. 

    The best way to troubleshoot scripts is to use alerts:

    alert("salesstage = " + salesstage);

    Use alerts to see what your code is doing and where it is breaking.

    Try that and see if it works for you.

    Fronk
    http://dmcrm.blogspot.com
    Friday, May 8, 2009 10:59 PM
  • Will this work as an onChange event or should it be either an onSave or an onLoad? I currently have another script in the onSave event on the main Opportunity form so I'm not sure if the two scripts are conflicting.

    Also, where does the troubleshooting script go?

    Thanks,

    Monday, May 11, 2009 4:11 PM
  • This script should be in the onChange event.  If you have something in the onSave event effecting the fields in your onChange event, then yes, the could very well be conflicting.  Try turning the onSave script off and see if your onChange works.  If it does, then the problem is in your onSave.  But the onSave is triggered when you click the save button.  The onChange fires whenever you change the value of the given field.  So, you should be able to test them independently with them both being on. 

    One other thing you might want to consider is not setting the closeprobability to null.

    As for troubleshooting, just put the alert script anywhere in your script.  Put it inside an IF statement, just before an IF, at the beginning/end of the script, any where you want step through your code.

    Fronk
    http://dmcrm.blogspot.com
    Monday, May 11, 2009 4:20 PM
  • I have also tried the following script. I have tried putting it in both the onChange event for the sales stage, the picklist, as well as the probability field, and again I'm not getting an error but nothing is happening in the probability field when I save and close it.


    var salesstage = crmForm.all.salesstagecode.SelectedText;
    // Set the value of the Probability field.
    var probability = crmForm.all.closeprobability.DataVaule;

    probability = '0';

    if (salesstage == "1 - Suspect")
    {
    probability = '0';
    }
    if (salesstage == "2 - Prospect")
    {
    probability = '0';
    }
    if (salesstage == "3 – Qualified Interest")
    {
    probability = '5';
    }
    if (salesstage == "4 – Long List")
    {
      probability = '10';
    }
    if (salesstage == "5 – Short List")
    {
     probability = '30';
    }
    if (salesstage == "6 – Short List Leader")
    {
      probability = '50';
    }
    if (salesstage == "7 – Agreements")
    {
      probability = '75';
    }
    if (salesstage == "8 – Won")
    {
      probability = '100';
    }

    Monday, May 11, 2009 4:49 PM
  • AKMSS,

    In your current example you pass the value of the field, closeprobability into a variable.  Then you update that value of the variable with something else if a certain condition is met.  However, "probability" is a variable, NOT the actual field, so this code actually isn't updating the field, just the variable.  If you added:

    crmForm.all.closeprobability.DataValue = probability;

    Then you should be all set.  Just watch out for passing number inside of quotes into that closeprobability field, as it wants numbers NOT strings. 

    Fronk
    http://dmcrm.blogspot.com
    • Marked as answer by AKMSS Tuesday, May 12, 2009 3:58 PM
    • Unmarked as answer by AKMSS Tuesday, May 12, 2009 3:59 PM
    • Marked as answer by Donna EdwardsMVP Thursday, May 14, 2009 2:07 PM
    Monday, May 11, 2009 7:49 PM
  • DATE set by JAVASCRIPT on Form Load function. Is this possible in MS Dynamics CRM 4.0 On-Premise?

    I'm sorry guys if this post may not be applicable to the previous question but somehow it's relative to javascripting. All I really need is to set the Date value of a textbox with a javascript.

    under onLoad event in a form, I place the following code. This code may have some error but I just want to ask if the logic is correct?

    var textBox1 = document.getElementById('<%= crmForm.all.scheduledstart.ClientID %>');

     if ( textBox1.value.length <= 0 )
     {
     var dateArray = textBox1.value.split('/');

     var textBoxMonth = parseFloat(dateArray[0]);
     var textBoxDay = parseFloat(dateArray[1]);
     var textBoxYear = parseFloat(dateArray[2]);
     alert('textBox: ' + textBoxMonth + '/' + textBoxDay + '/' + textBoxYear);

     var currentDate = new Date(textBoxYear, (textBoxMonth - 1), textBoxDay);
     var currentMonth = currentDate.getMonth();
     var currentDay = currentDate.getDate();
     var currentYear = currentDate.getYear();
     alert('current: ' + currentMonth + '/' + currentDay + '/' + currentYear);

     var nextDate= new Date(currentYear, currentMonth, (currentDay + 1));

     var dateString = (nextDate.getMonth() + 1) + '/' + nextDate.getDate() + '/' + nextDate.getYear();

     document.getElementById('<%= crmForm.all.scheduledstart.ClientID %>').value = dateString;

    }

    Thanks!
    Friday, November 6, 2009 3:17 PM