Header Ads Widget

Pick list value added dynamically from Controller || interview stuff


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.

Approval Process Code



Technical Issue – User want all new records for an object to be submitted to an Approval Process, however you do not want to the rely on users to manually click the ‘Submit for Approval’ button after creating all the records.



Solution – Create and test necessary Approval Process go through Setup / App Setup / Create / Workflow & Approvals / Approval Processes, implement this simple Apex Trigger (the following example is for the Account object). You will also need to create the test method to ensure code coverage if your current production Apex Classes do not provide adequate coverage also. You can also the add try / catch statements to ensure that the exceptions are caught and handled properly.



Code:

 trigger accountApprovalSubmit on Account (after insert) {

// trigger on Account object

for (Account a : trigger.new) {

// to extract all new values on Accounts

Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();

app.setObjectId(a.id);

//Add the id of account

Approval.ProcessResult result = Approval.process(app);

//pass Approval request in to approval process result set.

}



}



Other Example:

      if(Trigger.isAfter)

        {

          if(Criteria matched)

            {

                system.debug('condition passed');

                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();

                req1.setComments('Submitting request for approval.');

                req1.setObjectId(Trigger.old[0].Id);

                List<id> userId = new List<Id> {userInfo.getUserId()};

                req1.setNextApproverIds(userId);

                approval.ProcessResult result = Approval.process(req1);

            }


Conclusion:
  Using above code user can insert records without any operation of Submit for Approval Button.



Post a Comment

0 Comments