Friday, June 1, 2012

Pick list value added dynamically from Controller


Pick list value added dynamically from Controller:
Scenario: We faced issue related with pick list which having some values like ‘abc’,’def’,’ghi’,’jkl’etc. And we have to add another value ‘xyz’ in this pick list on certain condition.
For example we user select open page (visual force page) with Account. Some other user open same page with Opportunity button.
Proposed Solution: We can solve using two ways;
1. Solution: We can do using controller as following code.
Controller:
public List getPauseReason() {
List options = new List();
options.add(new SelectOption('','--None--'));
Schema.DescribeFieldResult fieldResult = Sales_Support_Request__c.Pause_Reason__c.getDescribe();
List ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry f : ple)
{
options.add(new SelectOption(f.getLabel(), f.getValue()));
}
if(Callfrom=='Account'){
options.add(new SelectOption(‘xyz','xyd'));
}
return options;
}
------------- End of controller-------------
Visual force page:
------------- End of controller-------------
2. Solution: We can do using JavaScript code in Visual force page code as given:
Step1. We have created hidden field which contains where call from like ‘Account’ or ‘Opportunity’.
Step2: We can call this field id in JavaScript code and write condition.

Conclusion: Using this solution we can add value in pick list dynamically in Salesforce.com.

No comments:

Post a Comment