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.

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.