You need to sign in to do that
Don't have an account?
Calling batch apex
Hi,
I made a batch apex that updates / gathers information with SOQL, and now I'm creating a trigger that calls for the batch. It looks like this, but I'm not sure how should I build it and how it works. Should I make a method to an existing trigger class or create a new one? It's giving me these errors : Invalid identifier: objBatchToUpdateService_Availability__c_Type
and Invalid bind expression type of SOBJECT:Service_Availability__c for Id field of SObject Case
trigger ImplementingServiceAvailabilityUpdaterTrigger on Service_Availability__c (before update) {
Set<Id> SetCaseId = new Set<Id>();
for(Case objCase: [Select Id from Case where Id in : Trigger.new])
{
SetCaseId.add(objCase.Id);
}
if(!SetCaseId.isEmpty())
{
string query='select Id, Fault_Classification__c, Setup_Name__c,'+ 'CaseNumber,Related_Setup__c,Status_Explanation__c '+
'from Case '+
'where RecordTypeId ='+ recordType.Id+
' and IsClosed = false'+
' order by CreatedDate desc';
BatchToUpdateService_Availability__c_Type objBatchToUpdateService_Availability__c_Type=new BatchToUpdateService_Availability__c_Type(query);
Database.executeBatch(objBatchToUpdateService_Availability__c);
}
}
Thanks for helping! :)
Hi Laaral,
can you put your Batch class here.. so i can help with the code how to call
your batch apex..
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/
Here is the batch apex class:
global class ImplementingServiceAvailabilityUpdater implements Database.Batchable<sObject>{
global ImplementingServiceAvailabilityUpdater(){
// Batch Constructor
}
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'select Id, Fault_Classification__c, Setup_Name__c,'+ 'CaseNumber,Related_Setup__c,Status_Explanation__c '+
'from Case '+
'where RecordTypeId ='+ recordType.Id+
' and IsClosed = false'+
' order by CreatedDate desc';
return Database.getQueryLocator(query);
}
// Checks objects and returns data
global void execute(Database.BatchableContext BC, List<sObject> scope){
for(Sobject sObj : scope){
Case caseObj = (Case)sObj;
//Now you can do what logic you want to implement with the case records you fetched.
}
}
global void finish(Database.BatchableContext BC){
// Logic to be Executed at finish
// Create and send an email with the results of the batch.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'laaral@test.com'};
mail.setSenderDisplayName('Salesforce Support');
mail.setSubject('New Case Created : ' + case.Id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Hi Laral,
You have to create object for your batch class ImplementingServiceAvailabilityUpdater .
you are creating the object of some other type.
ImplementingServiceAvailabilityUpdater objBatchToUpdateService_Availability__c_Type=new ImplementingServiceAvailabilityUpdater (query);
Database.executeBatch(objBatchToUpdateService_Availability__c);
if this is your solution please marked it as solved
Thanks
Vikas
Hi,
Replace your apex trigger with following.
Apex trigger:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/
You can call the batch apex from your trigger like the following
Id batchinstanceid = database.executeBatch(new ImplementingServiceAvailabilityUpdater(200);
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Thanks, but now it give me an error Invalid bind expression type of SOBJECT:Service_Availability__c for Id field of SObject Case
with this part of the code :
for(Case objCase: [Select Id from Case where Id in : Trigger.new]){
SetCaseId.add(objCase.Id);
}
Your trigger will fire on Service_Availability__c object but you are doing from case where ID in Trigger.new .
use the relationship key field in where clause
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Is there any Lookup or Master-detail field of Case object in Service_Availability__c object?
There isn't any lookup or master- detail field ? Should I create it or find another object that haves them? What does these lookup or master details do?
what is the relationship between Case object and Service_Availability__c (Custom Object)?
why you have used SetCaseId in your trigger?
Sorry, I noticed that I used the wrong Custom Object, because that Service_availability__c is located in Account not Case. :S SetCaseId was in a example of a trigger which called batch apex class.