Asynchronous Apex GJGBN
Asynchronous Apex GJGBN
Salesforce Developer
@future
public static void MyFutureMethod(){
Map<Id,Account> mapacct = new Map<Id,Account>([SELECT ID,Name FROM ACCOUNT]);
system.debug('test method'+mapacct);
// MyFutureMethod1(); // You can try to call one future method in another future method
}
@future(callout=true)
public static void MyFutureMethod1(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/');
request.setMethod('GET');
// System.debug('>>>>>>>'+id);
HttpResponse response = http.send(request);
Object animals;
String returnValue;
BatchClassPractice bcn = new BatchClassPractice() ; // You can try to call batch class from future method
ID batchprocessid = Database.executeBatch(bcn);
}
}
2. Can we call batch from schedule apex ?
Ans - Yes, we can call from finish method but you cannot call in start or excute method.
Ans- No, we cannot call otherwise Fatal error(System.AsyncException) error will occur.
System.AsyncException: Future method cannot be called from a future or batch method:
futureMethodExample.MyFutureMethod()
/* Write a batch a class to update the Teacher's records whose specialization is math */
return Database.getQueryLocator(teacherList);
}
public void execute(Database.BatchableContext BC, List<Teacher__c> tchList){
futureMethodExample.MyFutureMethod();
if(!tchList.isEmpty()){
for(Teacher__c tch: tchList){
if(tch.Specialization__c == 'Math'){
tch.Name = 'Sir/Madam'+' '+ tch.Name;
}if(tch.Specialization__c == 'Physics'){
tch.Name = 'Respected Madam'+' '+ tch.Name;
}
count++;
}
try{
update tchList;
system.debug('Count of Teacher'+count);
}
catch(exception e){
System.debug(e);
}
}
}
Ans- No, We cannot call otherwise Fatal Error(System.AsyncException) error will occur.
@future
public static void MyFutureMethod(){
Map<Id,Account> mapacct = new Map<Id,Account>([SELECT ID,Name FROM ACCOUNT]);
system.debug('test method'+mapacct);
// MyFutureMethod1(); // You can try to call one future method in another future method
}
@future(callout=true)
public static void MyFutureMethod1(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/');
request.setMethod('GET');
// System.debug('>>>>>>>'+id);
HttpResponse response = http.send(request);
Object animals;
String returnValue;
Ans: Yes we can call future and batch from Queueable and vice-versa.
futureMethodExample.MyFutureMethod1();
}
}
Ans- 1. Queueable,
2. Schedulable,
3. Database.Batchable
10. Can we call future method from Trigger ?
// DML Operation performs on Setup and Non Setup Method in a single transaction
public static void insertSetWithNonSetup(){
Account act = new Account();
act.Name = 'Non Setup Record';
insert act;
//
InsertUserSetup();
System.debug(act);
}
@future
public static void insertUserSetup(){
Profile p = [SELECT Id FROM Profile WHERE Name='Cross Org Data Proxy User'];
UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
// Create new user with a non-null user role ID
User usr = new User();
usr.Lastname ='SetupTest';
usr.Alias = 'stest';
usr.email = '[email protected]';
usr.ProfileId = p.Id;
usr.UserRoleId = r.Id;
insert usr;
system.debug(usr);
}
}
Trigger AccountTrigger on Account(Before delete, Before update){
Ans: // Retrieve the CronTrigger Id of the scheduled job String jobName = 'YourScheduledJobName';
CronTrigger jobTrigger = [SELECT Id FROM CronTrigger WHERE CronJobDetail.Name = :jobName
LIMIT 1];
// Abort the job using the CronTrigger Id
System.abortJob(jobTrigger.Id);