1

I am using Javascript to get picklist values(dependent or normal picklist).Its working fine.

But the problem is i am getting all the Picklist values for that object. I have to get picklist values based on the record type for that object.

Here follows my Javascript:

    <!--Start: This Script is to load  dependent picklist values--> 

            //describe the object we are dealing with. You'll need to change this to whatever object you are working with. Use the API name of the object.'
            var describeResults = sforce.connection.describeSObject('NAT_Request__c'); 
            // the object we want picklists from 

            // function to get the picklist values of a field. Finds all potential options. Simple pass it in a field id. Will return object with data in the values key.
            function getPicklistValues(field) 
    {
        //create return object 
        var returnObj = new returnObject();

    try
    {
        var validField = false;
        for(var i = 0; i < describeResults.fields.length; i++) 
        {
            var fieldList = describeResults.fields[i];
            var fieldName = fieldList.name;

            if(fieldName.toLowerCase() == field.toLowerCase())
            {
                validField = true;
                for(var j = 0; j < fieldList.picklistValues.length; j++)
                {

                    var newValue = new Object();
                    newValue.label = fieldList.picklistValues[j].label;
                    newValue.value = fieldList.picklistValues[j].value;
                    newValue.default = fieldList.picklistValues[j].defaultValue;
                    if(fieldList.picklistValues[j].hasOwnProperty('validFor'))
                    {
                        newValue.validFor = fieldList.picklistValues[j].validFor;
                    }
                    returnObj.values.push(newValue);
                }
                break;
            }
        }
        if(!validField)
        {
            throw 'Invalid field '+field+' specified for object ' + params.object;
        }
    }
    catch(exception)
    {
        returnObj.message = exception;
        returnObj.success = false;
    }        

    return returnObj;
}


function getDependentValues(field, value) 
{
    var returnObj = new returnObject();
    try
    {     
        var dependencyCode = new Array();          
        var getValues = getPicklistValues(field);        
        if(!getValues.success)
        {
            throw getValues.message;
        }
        var picklistValues =  getValues.values;
        var getController = getControllerName(field);

        if(!getController.success)
        {
            throw getController.message;
        }              
        var controller = getController.values;

        // sub function to do the validFor test
        function isDependentValue(index, validFor)
        {
            var base64 = new sforce.Base64Binary("");
            var decoded = base64.decode(validFor);
            var bits = decoded.charCodeAt(index>>3);

            return ((bits & (0x80 >> (index%8))) != 0);
        }
        var controllerFields =getPicklistValues(controller);

        for(var item = 0; item < controllerFields.values.length; item++)
        {
            if(controllerFields.values[item].value.toLowerCase() == value.toLowerCase())
             {
                for(var i = 0; i < picklistValues.length; i++)
                {
                    if(isDependentValue(item, picklistValues[i].validFor))
                    {
                        var newValue = new Object();
                        newValue.label = picklistValues[i].label;
                        newValue.value = picklistValues[i].value;
                        newValue.default = picklistValues[i].defaultValue;
                        newValue.validFor = picklistValues[i].validFor;
                        newValue.validForName =controllerFields.values[item].value;
                        returnObj.values.push(newValue);                                                               
                    }
                }
            }
        }
    }

    catch(exception)
    {
        returnObj.success = false;
        returnObj.message = exception;
    }
    return returnObj;        
}


// get the controller field name
function getControllerName(field) 
{
    var returnObj = new returnObject();

    try
    {
        var isValid = false;

        for(var i = 0; i < describeResults.fields.length; i++)
        {
            var fieldList = describeResults.fields[i];
            var fieldName = fieldList.name;

            if(fieldName.toLowerCase() == field.toLowerCase())
            {
                if(fieldList.controllerName == undefined)
                {
                    throw 'Field has no controller';
                }
                else
                {
                    returnObj.values = fieldList.controllerName;
                    isValid = true;
                }
                break;
            }
        }

        if(!isValid)
        {
            throw 'Invalid field '+field+' specified';
        }
    }
    catch(exception)
    {
        returnObj.success = false;
        returnObj.message = exception;
    }
    return  returnObj;
}


function returnObject()
{
    this.success = true;
    this.message = 'Operation Ran Successfully';
    this.values = new Array();
}

function setPickVals(field,optionsArray)
{
        var options = '';
         for(var i = 0; i < optionsArray.length; i++)
         {
             options += '<option value="'+optionsArray[i].value+'">'+optionsArray[i].label+'</option>';
         }         
         $(field).html(options);
}

$(document).ready(function() {

    $('.select').each(function(i)
    {
        var thisFieldId = $(this).attr('id');
         var controller =  getControllerName(thisFieldId);

         //if this field is a controlled field (has a controller, then find an onchange handler to its parent)
         if(controller.success)
         {
              var currValues = getDependentValues(thisFieldId,  $('#'+controller.values).val()); 
              setPickVals('#'+thisFieldId,currValues.values);

             $('#'+controller.values).change(function(data){                 
                 var values = getDependentValues(thisFieldId, this.value); 
                setPickVals('#'+thisFieldId,values.values);
             });
         }
         else
         {
              values = getPicklistValues(thisFieldId);
              setPickVals('#'+thisFieldId,  values.values);
         }
    });

});
            <!--Ends: This Script is to load  dependent picklist values--> 
        </script>

Source: Source Javascript code taken for dependent picklist

Workaround: I have used describelayout instead of Sobject, But it is not working.

var describeResults = sforce.connection.describeLayout('NAT_Request__c');

Please help me to solve this problem, Thanks in advance.

4
  • 2
    Here is what I resorted to for an Angular client Picklist values by record type for AngularJS UI.
    – Keith C
    Commented Aug 2, 2016 at 12:29
  • As per salesforce.stackexchange.com/questions/32972/…, I would assume it's not possible with standard salesforce, even in JS. (i.e. without using something like what Keith has suggested)
    – Nick C
    Commented Aug 2, 2016 at 12:34
  • This has been asked several times on SFSE.
    – Adrian Larson
    Commented Aug 2, 2016 at 13:05
  • @KeithC You have given controller code, Can u please tell me how to implement in VF page to get dependent picklist values based on Rec type.
    – Sathish
    Commented Aug 3, 2016 at 4:19

2 Answers 2

0

In response to Satish's question in the comment, an example of the client-side of Picklist values by record type for AngularJS UI is:

<select
        id="Cause__c"
        name="Cause__c"
        ng-model="bc.Cause__c"
        ng-options="o.value as o.label for o in recordTypeFilteredPicklists.Cause__c"
        ng-required="required('cve__Cause__c')"
        class="form-control"
        ></select>

Here recordTypeFilteredPicklists.Cause__c is is an array to be iterated over to add the select options using whatever client-side JavaScript library you are using (Angular in my case). It is the part of the JSON keyed by the record type developer name. In this fragment of the JSON that is "TermLife":

{  
    "TermLife":{        // Record type developer name
        "Cause__c":[    // Picklist field API name
            "Accident", // Picklist entries for this record type
            "Homicide",
            "Illness"
        ],
        ...

(I had few record-type depending picklists so did not introduce a level in the JSON to distinguish by SObjectType name.)

0

We can use UI API to get picklist values based on record type id. Use the below javascript code in Visualforce page.

uiApi.getSObject("Contract").getPicklistValuesPerRecordType("PicklistfieldAPIName", "recordTypeId", function(err, res) {                                           
 if (err) return;
optionscontrats = JSON.parse(res).values;                                        });

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .