Showing posts with label Apex Controllers. Show all posts
Showing posts with label Apex Controllers. Show all posts

Friday, July 24, 2015

Automatically Submit Multiple Records to Approval through Apex

Hi Friends,

Today I am sharing one code snippet with you to automatically submit multiple records to approval process through apex.

Firstly you need to setup the approval process on which object you want to submit the records for Approval.

Than through your Apex Controller or Trigger you could process the records and send that for Approval.

//Create a List of Approval.ProcessSubmitRequest
List requests = new List();

// Process your List on which you need to send the records for Approval, It could be direct list or you could process the list from you inner query or whatever list you want to process, In my example I have used the list of Opportunity from a trigger:

for(Opportunity opp: Trigger.New){
// Create a instance of Approval.ProcessSubmitRequest to add that to the list initialized above
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();

// Set the comment you want to add
req.setComments('Submitting request for approval');

// Set the object-id in the Approval.ProcessSubmitRequest instance
req.setObjectId(opp.Id);

//Add the instance to the list initialized above, before the for loop
requests.add(req);
}

// We need to capture the result of the approval, so initialize a list of Approval.ProcessResult
Approval.ProcessResult[] processResults = null;

// Apply Exception Handliing
try {
// The command Approval.process will submit all your record for Approval
processResults = Approval.process(requests);
}catch (System.DmlException e) {
System.debug('Exception Is ' + e.getMessage());
}

Point to Notice:
Sometimes our trigger doesn't submit our records for approval, so that time we need to check, whatever records we are submitting for Approval through our code, are they meeting the entry-level criteria successfully which we have defined in our approval process, so we have to apply the conditions and have to test it carefully.

You could ask me for any topic or any help if you need something specific, I will be happy to help you.

Thanks,

Amit Goyal

Wednesday, December 7, 2011

How to build a scheduled Apex Class:

Topic in Detail: As we have discussed how to schedule a apex class and we need to assign a apex class in the schedule apex setup. The apex class is not same as our general apex class. We have to implement some syntactical changes in our apex class if we want to schedule that.

Note: If we don’t follow this syntax for schedulable apex class then we cannot find our class in the schedulable class options. SalesForce give options to choose apex class only for the classes which follows this syntax.

The syntax is:

global class ScheduledApexController implements Schedulable{

/* Optional Variable Declarations */

global void execute(SchedulableContext ctx){

/* Optional Variable Initialization */

/* Complete Logic */
}
}

We have to make our class as Global by global keyword and implement a schedulable interface. implements is a keyword to apply interfaces, as we know apex is based on java so you get the idea what it is.

Then we have to write a function execute which is also has its access as global and pass the SchedulableContext object as a parameter.

so this way u can change write a schedulable apex class.

For more guidance, reach me at:

http://www.amitgoyal09.blogspot.com
http://www.salesoncloud.blogspot.com
nicsplacement@gmail.com
linkedIn, Twitter, IM by name amitgoyal09

Thanks,
Amit Goyal
Online Trainer and SalesForce Developer
(SalesForce, MCA, BCA,RHCE)

Tuesday, December 6, 2011

Use of Batch / Schedule Apex in SalesForce

Topic In Detail: When you want to schedule some apex code at some particular time within a period of time not on some event occurance.
Batch apex is like schedule apex, if you want to run a specific code to achieve some task related to maintenance of your Organization data for some specific purpose so, you can schedule that code to run on weekly or monthly basis, you can choose the days on which you want to schedule you apex code. you can specify the period of time Start-End Date and the time too. To schedule the apex, we have the option in Setup -> Apex Classes -> Schedule Apex




Clicking on this button will redirect to you on a screen




You have to enter your job name then select the apex code you want to schedule and the time event on which you want to schedule this code. The code is scheduled.

So this way, you can schedule any apex code in your organization for your application.

For more guidance, reach me at:
http://www.amitgoyal09.blogspot.com
http://www.salesoncloud.blogspot.com
nicsplacement@gmail.com
linkedIn, Twitter, IM by name amitgoyal09

Thanks,
Amit Goyal
Online Trainer and SalesForce Developer
(SalesForce, MCA, BCA,RHCE)

Monday, November 28, 2011

Exception : Can't use Setup and Non-Setup DML Operation in same Context

Topic In Detail: We face this situation sometime when we want to use DML opertaion in SetUp Object(like user) and Contact (like Contact or any custom Object) as well. but that we got this error.

Solution:

Use Future class to resolve this Exception.

What is Future Class:

This is a class with added syntax(@future) like we do in testclass (@isTest), and in this future class u can use DML exception on any one of them.

So it will add the DML in your batch process / process heap and will execute this DML after some time.

so this way u can change the context of both of them and resolve this issue.

For more guidance, reach me at:
http://www.amitgoyal09.blogspot.com
http://www.salesoncloud.blogspot.com
nicsplacement@gmail.com
linkedIn, Twitter, IM by name amitgoyal09

Thanks,
Amit Goyal
Online Trainer and SalesForce Developer
(SalesForce, MCA, BCA,RHCE)