[SalesForce] get picklist values of custom field into customButton javascript

Is it possible to populate the custom field picklist values into a custom button javascript code? Any pointers on how to use Schema.DescribeFieldResult class in javascript?

I am actually working on a jQuery modal dialog to create a new contact. I want to bring the role picklist values of Contact into my jQuery code. The button is on related list of standard page layout.

Best Answer

Here is a quick example using the Opportunity and stage name to console log the output. Not sure where your button is but this would be a custom button on the opportunity.

Basically everything available in the SOAP API is available via the AJAX toolkit. You just have to get the javascript correct. Here is a link to the AJAX toolkit documentation...

https://resources.docs.salesforce.com/sfdc/pdf/apex_ajax.pdf

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}

var result = sforce.connection.describeSObject("Opportunity");


for (var i=0; i<result.fields.length; i++) {
  var field = result.fields[i];

  if(field.name == 'StageName'){
      var oPicklistVals = field.picklistValues;
       for (var b=0; b<oPicklistVals.length; b++) 
       { 
        console.log(oPicklistVals[b].value);
       }
  }
}

Where does this button reside? It makes a difference on how you go about it. Maybe a code sample for what you have?

To get a list of options in APEX via the controller you could use:

public List<SelectOption> getownershipOptions()    
    {    
        List<SelectOption> options =  new List<SelectOption>();    
        options.add(new selectOption('None','--- None ---'));    
        Schema.DescribeFieldResult fieldResult = Opportunity.StageName.getDescribe();    
        List<Schema.picklistEntry> ple = fieldResult.getPicklistValues();    
        for(Schema.picklistEntry f:ple)    
        {    
            options.add(new selectOption(f.getLabel(),f.getValue()));                    
        }    
        return Options;    
    }  

Then use that in your javascript. This can also be done via Javascript Remoting....

Related Topic