Showing posts with label @future(callout=true). Show all posts
Showing posts with label @future(callout=true). Show all posts

Tuesday, November 20, 2018

Asynchronous Apex: Scheduled Apex in Salesforce.com

Scheduled Apex 

Scheduled Apex lets you run Apex classes at a specified time; for example, This is ideal for daily or weekly maintenance tasks using Batch Apex. 

To invoke Apex classes at specific times, first implement the Schedulable interface for the class. Then, schedule an instance of the class to run at a specific time using the System.schedule method.

Note: Governor limits in case of asynchronous are higher as compared to synchronous. 

Schedule Apex Syntax:

global class SomeClass implements Schedulable {
    global void execute(SchedulableContext cntx) {
        // awesome code here
    }
}

Schedulable Apex

global class RemindOpptyOwners implements Schedulable {

    global void execute(SchedulableContext cntx) {
        List<Opportunity> opptys = [Select Id, Name, OwnerId, CloseDate  From Opportunity  Where IsClosed = False AND CloseDate < TODAY];
        
// Call util class and method here to create a task for each opportunity in the list
        TaskUtils.remindOwners(opptys);
    }
    
}

System.Schedule Method

It takes three arguments: 
1) Job name

2) CRON expression used to represent the time and date the job is scheduled to run

3) Instance variable of the class object

//Create a instance variable of the class object
RemindOpptyOwners reminder = new RemindOpptyOwners();

//Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
String sch = '20 30 8 10 2 ?';

String jobID = System.schedule('Remind Opp Owners', sch, reminder);

Sec Min Hours Days Month WeekDay  Years 

0 0,30      * * * ? *

0 5 20 ? Jan,May Mon-Fri 2017

* --> All Values
? --> No Values


Monitoring Scheduled Jobs

It uses a job_Id variable which is returned from the System.schedule method.

CronTrigger ct = [Select TimesTriggered, NextFireTime From CronTrigger Where Id= :jobID];

Monitoring Queued Jobs

To query information about your submitted job, perform a SOQL query on AsyncApexJob by filtering on the job ID that the System.enqueueJob method returns.

AsyncApexJob jobInfo = [SELECT Id, Status, JobItemsProcessed,
 NumberOfErrors FROM AsyncApexJob WHERE Id = :jobID];

Schedule Apex Consideration:
1) You can only have 100 scheduled Apex jobs at one time, and the maximum number of scheduled Apex executions per a 24-hour period. 

2) Synchronous Web service callouts are not supported from scheduled Apex. But we can make an asynchronous callout by placing the callout in a method annotated with @future(callout=true) and call this method from scheduled Apex. 

(However, if your scheduled Apex executes a batch job, callouts are supported from the batch class.)












Asynchronous Apex: Future Method in Salesforce.com

Future Method

1) Making callouts to external web services. 
i.e. When we are making callouts to external web services from a trigger or after performing a DML operation, we must use a future or queueable method.

2) Process we want to run in its separate thread in the back-end.
i.e. When we are working on some sort of resource consuming calculation or processing of records.

3) Isolating DML operations on different types of sObject.
i.e. When we are performing DML on setup and non-setup sObject types, it isolates DML to prevent the "Mixed DML Errors".


Future Method Syntax:
global class FutureClass {
     @future
     public static void futureMethod(List<Id> recordIds) {
     List<Account> accounts = [Select Id, Name from Account Where Id IN :recordIds];
     // process account records to do awesome stuff
     }
}


Future Method Considerations

1) Future methods must be static methods and return a void.

2) Parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
Note:- Future methods can’t take standard or custom objects as arguments.

3) Common pattern is to pass a List<record IDs> that you want to process asynchronously.

4) Future methods can’t be used in Visualforce controllers in getMethod(), setMethod(), nor in the constructor.

5) You can’t call a future method from a future method.

6) You can not invoke a trigger that calls a future method while running a future method.

7) The getContent() and getContentAsPDF() methods can’t be used in future method.

8) You’re limited to 50 future calls per Apex invocation.

9) To test future methods, enclose your test code between the test.startTest() and test.stopTest() test methods.


Validation in flow input text field in sfdc: Maximum 255 characters limit

The input text field validate in sfdc flow behaves opposite of the generic sfdc validation rule.  Here the validation formula is evaluating ...