locked
Auto Populate N/A value on slection of picklist in MSCRM 2011 RRS feed

  • Question

  • Hi,

    I have one requirement like I had one picklist value and 3 text fields
    Picklist  Name : Value Selected (new_valueselected)
    Values : yes (1) , No (2).

    Text Fields : Min ( new_min) , Max(new_max), Others(new_others).

    if I selected the value as NO from the picklist Min, Max and Others value should be N/A.

    If I selected the value as YES I should clear the text.

    I have written the script like this.

    function getValues() {
    
        var optionsetValueSelected = Xrm.Page.getAttribute("new_valueselected");
        if (optionsetValueSelected == 1) {
            if (Xrm.Page.getAttribute("new_min").getValue() == "N/A") {
                Xrm.Page.getAttribute("new_min").setValue(null);
            }
            else {
                var MinVal = Xrm.Page.getAttribute("new_min").getValue();
                Xrm.Page.getAttribute("new_min").setValue(MinVal);
            }
    
            if (Xrm.Page.getAttribute("new_max").getValue() == "N/A") {
                Xrm.Page.getAttribute("new_max").setValue(null);
            }
            else {
                var MaxVal = Xrm.Page.getAttribute("new_max").getValue();
                Xrm.Page.getAttribute("new_max").setValue(MaxVal);
            }
            if (Xrm.Page.getAttribute("new_others").getValue() == "N/A") {
                Xrm.Page.getAttribute("new_others").setValue(null);
            }
            else {
                var OthersVal = Xrm.Page.getAttribute("new_others").getValue();
                Xrm.Page.getAttribute("new_others").setValue(OthersVal);
            }
        }
    
        if (optionsetValueSelected == 2) {
            Xrm.Page.getAttribute("new_min").setValue("N/A");        
            Xrm.Page.getAttribute("new_max").setValue("N/A");      
            Xrm.Page.getAttribute("new_others").setValue("N/A");
            
        }
    }

    Code is working fine but, If I save the record which is having the some values in the text fields,  for example Min = 20 , Max 50, and Others = 15.
    Now When I was slecting the picklist value =  No then text field values are N/A and coming back to Yes values are clearing form the text fields I was unable to retaining the old values of (20, 50 and 15) of the record.

    Please let me know anyone have any idea how to reatin the old values ?

    Thanks,




    
    
    
    
    
    
    


    • Edited by Rahul-22 Monday, August 4, 2014 2:27 PM
    Monday, August 4, 2014 2:26 PM

All replies

  • Hi,

    setValue - will sets the data value for an attribute. - meaning - it will clear the value at browser level and sets the new value.

    In order to retain the old values, i would suggest you to assign the old values to a temp variable before setting new value, and get old values from temp variable if necessary. 


    Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, August 4, 2014 4:38 PM
  • You could query back to crm db to get old value because you have not saved so you will be able to access old values.

    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, August 5, 2014 9:09 AM
    Moderator

  • There is no field.getLastValue() for CRM fields.
    This is why you need to use a temporary variable as "Ravitheja J" suggested. The following script is probably close to what you require.

    var lastMin, 
        lastMax, 
        lastOthers, 
        xrmPage = Xrm.Page,
        ValuesSelected = xrmPage.getAttribute("new_valueselected"),
        Min = xrmPage.getAttribute("new_min"),
        Max = xrmPage.getAttribute("new_max"),
        Others = xrmPage.getAttribute("new_others");
         
    function getValues() {
        switch(ValuesSelected.getValue()) {
            case 1:
                Min.setValue(Min.getValue() === "N/A" ? lastMin : null);
                Max.setValue(Max.getValue() === "N/A" ? lastMin : null);
                Others.setValue(Others.getValue() === "N/A" ? lastOthers : null);
            break;
            case 2:
                lastMin = Min.getValue();
                lastMax = Max.getValue();
                lastOthers = Others.getValue();
            
                Min.setValue("N/A");        
                Max.setValue("N/A");      
                Others.setValue("N/A");
            break;
        }
    }
    cheers

    My New Blog


    • Edited by Adi Katz Tuesday, August 5, 2014 8:16 PM
    Tuesday, August 5, 2014 8:15 PM