function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LaaralLaaral 

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! :)

 

hitesh90hitesh90

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/

LaaralLaaral

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 });
 
         }

      }

vikas88vikas88

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

 

 

hitesh90hitesh90

Hi,

 

Replace your apex trigger with following.

 

Apex trigger:

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()){
		ImplementingServiceAvailabilityUpdater objBatchToUpdateService_Availability_Type=new ImplementingServiceAvailabilityUpdater();
		Database.executeBatch(objBatchToUpdateService_Availability_Type, 200);
	}
}

 

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/

 

souvik9086souvik9086

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

LaaralLaaral

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);
    }

souvik9086souvik9086

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

 

 

 

hitesh90hitesh90

Is there any Lookup or Master-detail field of Case object in Service_Availability__c object?

LaaralLaaral

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?

hitesh90hitesh90

what is the relationship between Case object and Service_Availability__c (Custom Object)?

why you have used SetCaseId in your trigger?

LaaralLaaral

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.