locked
Set the value of the option field that the user is on RRS feed

  • Question

  • I have an option set field that is that record status.  When the user changes the record status, I want to do a few checks and if there are issues with the record I want to warn the user and reset the option.  This code worked great until I added the java line to set the option field to the value I wanted.  Does anyone know how to make this work?

    function CheckBeforeClose() {
      debugger;
      var firstfail = 0
      var secondfail = 0
      alert (Xrm.Page.getAttribute("po_initialleadstatus").getSelectedOption().text);
      if (Xrm.Page.getAttribute("po_initialleadstatus").getSelectedOption().text == "Closed - Passed on/Qualified Lead") {
        alert("second check");
        alert(Xrm.Page.getAttribute("po_facevalueoflifeinsurancepolicy").getValue());
        if (Xrm.Page.getAttribute("po_facevalueoflifeinsurancepolicy").getValue() < 100000) {
          alert("first fail");
          firstfail = 1;
        }
        var now = new Date(); //Todays Date  
        var birthday =Xrm.Page.getAttribute("po_dateofbirth").getValue(); //Get the Date of Birth value 
         
        var diff = now.getMonth() - birthday.getMonth();  //Check to see if Birthday has already passed
    alert("***"+diff);
        if (diff > -1) //If Birthday has already occurred  
         {
             var bd1 = now.getFullYear() - birthday.getFullYear();
             //set the age attribute
             //Xrm.Page.getAttribute("arcrp_age").setValue(bd1.toString());
            var Age = bd1;
         }
         else //If Birthday has not already occurred 
         {
             var bd2 = now.getFullYear() - birthday.getFullYear() - 1;
             //Xrm.Page.getAttribute("arcrp_age").setValue(bd2.toString());
            var Age = bd2;
         }
        alert(Age);
        if (Age < 70) {
         alert("hi");
          secondfail = 1;
          }
             alert( firstfail);
            if (firstfail == 1 || secondfail == 1) {
    alert(firstfail);
    alert(secondfail);
             Xrm.Page.getAttribute(“po_initialleadstatus”).setValue('Closed - Does Not Quality');
              alert("Caller is not qualified to pass on!");
             
        }
      }
    }

    The line that is failing and broke the script is:   Xrm.Page.getAttribute(“po_initialleadstatus”).setValue('Closed - Does Not Qualify');

    I have copied the value (Closed - Does Not Qualify).  Straight from the field so I can't be wrong.


    Thanks, Dale

    Thursday, May 23, 2013 6:12 PM

Answers

  • for the date just use the function from this post:

    http://www.crmanswers.net/2013/03/calculate-age-in-microsoft-dynamics-crm.html

    the problem in that line can be the quotation marks, need to use " instead of 

    so your code will be this: (I also corrected other issues with the .text method because is not supported)

    function CalculateAge(birthday, ondate) {
       // if ondate is not specified consider today's date
       if (ondate == null) { ondate = new Date(); }
       // if the supplied date is before the birthday returns 0
       if (ondate < birthday) { return 0; }
       var age = ondate.getFullYear() - birthday.getFullYear();
       if (birthday.getMonth() > ondate.getMonth() || (birthday.getMonth() == ondate.getMonth() && birthday.getDate() > ondate.getDate())) { age--; }
       return age;
    }
    
    function CheckBeforeClose() {
        debugger;
        var firstfail = 0;
        var secondfail = 0;
        alert(Xrm.Page.getAttribute("po_initialleadstatus").getText());
        if (Xrm.Page.getAttribute("po_initialleadstatus").getText() == "Closed - Passed on/Qualified Lead") {
            alert("second check");
            alert(Xrm.Page.getAttribute("po_facevalueoflifeinsurancepolicy").getValue());
            if (Xrm.Page.getAttribute("po_facevalueoflifeinsurancepolicy").getValue() < 100000) {
                alert("first fail");
                firstfail = 1;
            }
            var birthday = Xrm.Page.getAttribute("po_dateofbirth").getValue(); //Get the Date of Birth value  
            var Age = CalculateAge(birthday);
            alert(Age);
            if (Age < 70) {
                alert("hi");
                secondfail = 1;
    	}
            alert(firstfail);
            if (firstfail == 1 || secondfail == 1) {
                alert(firstfail);
                alert(secondfail);
                Xrm.Page.getAttribute("po_initialleadstatus").setValue(100000001);
                alert("Caller is not qualified to pass on!");
            }
        }
    }


    My blog: www.crmanswers.net


    Thursday, May 23, 2013 7:17 PM

All replies

  • you need to set the numeric value of the optionset, not the label.

    and that function to calculate the birthday isn't accurate, it doesn't consider the day, to make an example today is 23 may but will give the same age for a person born on 1 may or 31 may.


    My blog: www.crmanswers.net

    Thursday, May 23, 2013 6:20 PM
  • OK, I have modified like this but it is still now working.  Also, how do I fix the date?

    function CheckBeforeClose() {
      debugger;
      var firstfail = 0
      var secondfail = 0
      alert (Xrm.Page.getAttribute("po_initialleadstatus").getSelectedOption().text);
      if (Xrm.Page.getAttribute("po_initialleadstatus").getSelectedOption().text == "Closed - Passed on/Qualified Lead") {
        alert("second check");
        alert(Xrm.Page.getAttribute("po_facevalueoflifeinsurancepolicy").getValue());
        if (Xrm.Page.getAttribute("po_facevalueoflifeinsurancepolicy").getValue() < 100000) {
          alert("first fail");
          firstfail = 1;
        }
        var now = new Date(); //Todays Date  
        var birthday =Xrm.Page.getAttribute("po_dateofbirth").getValue(); //Get the Date of Birth value 
         
        var diff = now.getMonth() - birthday.getMonth();  //Check to see if Birthday has already passed
    alert("***"+diff);
        if (diff > -1) //If Birthday has already occurred  
         {
             var bd1 = now.getFullYear() - birthday.getFullYear();
             //set the age attribute
             //Xrm.Page.getAttribute("arcrp_age").setValue(bd1.toString());
            var Age = bd1;
         }
         else //If Birthday has not already occurred 
         {
             var bd2 = now.getFullYear() - birthday.getFullYear() - 1;
             //Xrm.Page.getAttribute("arcrp_age").setValue(bd2.toString());
            var Age = bd2;
         }
        alert(Age);
        if (Age < 70) {
         alert("hi");
          secondfail = 1;
          }
             alert( firstfail);
            if (firstfail == 1 || secondfail == 1) {
    alert(firstfail);
    alert(secondfail);
             Xrm.Page.getAttribute(“po_initialleadstatus”).setValue(100000001);
              alert("Caller is not qualified to pass on!");
             
        }
      }
    }


    Thanks, Dale

    Thursday, May 23, 2013 6:50 PM
  • for the date just use the function from this post:

    http://www.crmanswers.net/2013/03/calculate-age-in-microsoft-dynamics-crm.html

    the problem in that line can be the quotation marks, need to use " instead of 

    so your code will be this: (I also corrected other issues with the .text method because is not supported)

    function CalculateAge(birthday, ondate) {
       // if ondate is not specified consider today's date
       if (ondate == null) { ondate = new Date(); }
       // if the supplied date is before the birthday returns 0
       if (ondate < birthday) { return 0; }
       var age = ondate.getFullYear() - birthday.getFullYear();
       if (birthday.getMonth() > ondate.getMonth() || (birthday.getMonth() == ondate.getMonth() && birthday.getDate() > ondate.getDate())) { age--; }
       return age;
    }
    
    function CheckBeforeClose() {
        debugger;
        var firstfail = 0;
        var secondfail = 0;
        alert(Xrm.Page.getAttribute("po_initialleadstatus").getText());
        if (Xrm.Page.getAttribute("po_initialleadstatus").getText() == "Closed - Passed on/Qualified Lead") {
            alert("second check");
            alert(Xrm.Page.getAttribute("po_facevalueoflifeinsurancepolicy").getValue());
            if (Xrm.Page.getAttribute("po_facevalueoflifeinsurancepolicy").getValue() < 100000) {
                alert("first fail");
                firstfail = 1;
            }
            var birthday = Xrm.Page.getAttribute("po_dateofbirth").getValue(); //Get the Date of Birth value  
            var Age = CalculateAge(birthday);
            alert(Age);
            if (Age < 70) {
                alert("hi");
                secondfail = 1;
    	}
            alert(firstfail);
            if (firstfail == 1 || secondfail == 1) {
                alert(firstfail);
                alert(secondfail);
                Xrm.Page.getAttribute("po_initialleadstatus").setValue(100000001);
                alert("Caller is not qualified to pass on!");
            }
        }
    }


    My blog: www.crmanswers.net


    Thursday, May 23, 2013 7:17 PM