locked
CRM 2011-Set text value based on a couple of picklist values. RRS feed

  • Question

  • I'm having the hardest time getting this to work.  I've rewritten it multiple times.  I have multiple picklist values.  Based on those values, I want to write a value into a text field.  Below is an example of what I have written.

    function setvalue(){

    if (Xrm.Page.getAttribute("new_pvalue").getSelectedOption() != null && Xrm.Page.getAttribute("new_ncvalue").getSelectedOption() != null){
     var pset = Xrm.Page.getAttribute("new_pvalue").getSelectedOption().text
     var ncc = Xrm.Page.getAttribute("new_ncvalue").getSelectedOption().text
     if (pset == "A" && ncc == "1PC") {
      Xrm.Page.getAttribute(“new_bo”).setValue(‘1248986’);}
     else if (pset == "A" && ncc == "2PC") {
      Xrm.Page.getAttribute(“new_bo”).setValue(‘1249004’);}
     else if (pset == "A" && niccard == "4SPF") {
      Xrm.Page.getAttribute(“new_bo”).setValue(‘1248985’);}

    }

    }

    Thursday, November 1, 2012 5:30 PM

All replies

  • Hi,

    try this code (although I don't think that your code has any issues besides the weird quotation and a few semicolons missing):

    function setValue() {
        if (Xrm.Page.getAttribute("new_pvalue") == null
            || Xrm.Page.getAttribute("new_ncvalue") == null) {
            return;
        }
    
        if (Xrm.Page.getAttribute("new_pvalue").getSelectedOption() != null
            && Xrm.Page.getAttribute("new_ncvalue").getSelectedOption() != null) {
            var pset = Xrm.Page.getAttribute("new_pvalue").getSelectedOption().text;
            var ncc = Xrm.Page.getAttribute("new_ncvalue").getSelectedOption().text;
            if (pset == "A" && ncc == "1PC") {
                Xrm.Page.getAttribute("new_bo").setValue("1248986");
            }
            else if (pset == "A" && ncc == "2PC") {
                Xrm.Page.getAttribute("new_bo").setValue("1249004");
            }
            else if (pset == "A" && niccard == "4SPF") {
                Xrm.Page.getAttribute("new_bo").setValue("1248985");
            }
        }
    }

    Please also share the error you are getting, or what you mean by saying it's not working? Is it throwing an error or is it not working as it is supposed to?

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.


    Thursday, November 1, 2012 7:21 PM
  • I'm having the hardest time getting this to work.  I've rewritten it multiple times.  I have multiple picklist values.  Based on those values, I want to write a value into a text field.  Below is an example of what I have written.

    function setvalue(){

    if (Xrm.Page.getAttribute("new_pvalue").getSelectedOption() != null && Xrm.Page.getAttribute("new_ncvalue").getSelectedOption() != null){
     var pset = Xrm.Page.getAttribute("new_pvalue").getSelectedOption().text
     var ncc = Xrm.Page.getAttribute("new_ncvalue").getSelectedOption().text
     if (pset == "A" && ncc == "1PC") {
      Xrm.Page.getAttribute(“new_bo”).setValue(‘1248986’);}
     else if (pset == "A" && ncc == "2PC") {
      Xrm.Page.getAttribute(“new_bo”).setValue(‘1249004’);}
     else if (pset == "A" && niccard == "4SPF") {
      Xrm.Page.getAttribute(“new_bo”).setValue(‘1248985’);}

    }

    }

    One obvious error you have is the single quotation mark "‘"

    You might copied it from else where and somehow got the unicoded character instead of ASCII code "'" character

    There are other parts of the code that can be improved, but I think this is the cause of your problem.

    • Proposed as answer by Adam Vero Thursday, November 1, 2012 8:37 PM
    Thursday, November 1, 2012 7:25 PM
  • ...and likewise the 'right handed' single quote ’ character. Find/replace both left and right variations and things ought to work better.

    Personally I would use Xrm.Page.getAttribute("new_value").getText();

    rather than .getSelectedOption().text just for readability.

    Also I would be tempted to simplify the logic to:

    ...

    if (pset=="A")

        { if(ncc=="1PC") {foo}

           else if(ncc=="2PC") {etcetera}

           ...

        }

    or even use a SWITCH / CASE for the inner condition set.


    Hope this helps. Adam Vero, Microsoft Certified Trainer | Microsoft Community Contributor 2011

    Thursday, November 1, 2012 8:37 PM