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
Trif Cristian 7Trif Cristian 7 

how can i transform this trigger in a batch class?

Hello, I'm trying to convert my trigger to a batch class. Can anyone help me with this one, I would much appreciate:

 

trigger SetTitleToAttachment on Attachment (before insert) { 
//store the id of the WO related to the attachment
 List<String> woIDs= new List<String>();
    for (Attachment  orderRec : Trigger.new) {
        woIDs.add(orderRec.ParentID);
    }
System.Debug('>>>>'+woIDs);

//store the work order(s) where the ID in woIDs
Map<String, SVMXC__Service_Order__c > woByIds= new Map<String, SVMXC__Service_Order__c >([
        SELECT Id, Name, SVMX_PS_Ship_To_Name__c, SVMXC__Order_Type__c, SerialNumberIP__c
        FROM SVMXC__Service_Order__c 
        WHERE Id =: woIDs
    ]);
  
//change the name of the attachment
 System.debug('woByIds'+ woByIds);
        for(Attachment a: Trigger.new){
           SVMXC__Service_Order__c workorder = woByIds.get(a.ParentId);
            System.debug('workorder '+ workorder );
                if(workorder != null){
                    if(a.Name.startsWith('SIG_Work_Order_Service__Reports')){
                        a.Name =  'SR'+ '_'+  date.today().format()+ '_'+workorder.SVMX_PS_Ship_To_Name__c+ '_' + workorder.Name+ '_'+ workorder.SVMXC__Order_Type__c+'_'+ workorder.SerialNumberIP__c +'.pdf';
                    }
                    else if (a.Name.startsWith('SIG_HandoverCheckList_Customer_To_SIG_Report')){
                        a.Name = 'Handover Customer To SIG' +'.pdf';
                    } 
                    else if (a.Name.startsWith('SIG_HandoverCheckList_SIG_To_Customer')){ 
                        a.Name = 'Handover SIG_To_Customer'+'.pdf';
                    }
                    else if (a.Name.startsWith('SIG_Work_Order_Service')){ 
                        a.Name = 'SR'+ '_'+  date.today().format()+ '_'+workorder.SVMX_PS_Ship_To_Name__c+ '_' + workorder.Name+ '_'+ workorder.SVMXC__Order_Type__c+'_'+ workorder.SerialNumberIP__c+'.pdf' ;
                    }
                }
             
         } 

}
Maharajan CMaharajan C
Hi Trif:

Try the below class:
 
global class SetTitleToAttachmentBatch implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
         
        String query = 'SELECT Id, ParentID FROM Attachment';
        return Database.getQueryLocator(query);
    }
     
    global void execute(Database.BatchableContext BC, List<Account> attList) {
        Set<String> woIDs= new Set<String>();

		for (Attachment  orderRec : attList) {
			woIDs.add(orderRec.ParentID);
		}
		
		Map<String, SVMXC__Service_Order__c > woByIds= new Map<String, SVMXC__Service_Order__c >([
        SELECT Id, Name, SVMX_PS_Ship_To_Name__c, SVMXC__Order_Type__c, SerialNumberIP__c
        FROM SVMXC__Service_Order__c 
        WHERE Id =: woIDs
		]);
		
		for(Attachment a: attList){
           SVMXC__Service_Order__c workorder = woByIds.get(a.ParentId);
            System.debug('workorder '+ workorder );
                if(workorder != null){
                    if(a.Name.startsWith('SIG_Work_Order_Service__Reports')){
                        a.Name =  'SR'+ '_'+  date.today().format()+ '_'+workorder.SVMX_PS_Ship_To_Name__c+ '_' + workorder.Name+ '_'+ workorder.SVMXC__Order_Type__c+'_'+ workorder.SerialNumberIP__c +'.pdf';
                    }
                    else if (a.Name.startsWith('SIG_HandoverCheckList_Customer_To_SIG_Report')){
                        a.Name = 'Handover Customer To SIG' +'.pdf';
                    } 
                    else if (a.Name.startsWith('SIG_HandoverCheckList_SIG_To_Customer')){ 
                        a.Name = 'Handover SIG_To_Customer'+'.pdf';
                    }
                    else if (a.Name.startsWith('SIG_Work_Order_Service')){ 
                        a.Name = 'SR'+ '_'+  date.today().format()+ '_'+workorder.SVMX_PS_Ship_To_Name__c+ '_' + workorder.Name+ '_'+ workorder.SVMXC__Order_Type__c+'_'+ workorder.SerialNumberIP__c+'.pdf' ;
                    }
                }
             
        } 
		
	
        try {
            update attList;
         
        } catch(Exception e) {
            System.debug(e);
        }
         
    }   
     
    global void finish(Database.BatchableContext BC) {
    }
}


Thanks,
Maharajan.C
Trif Cristian 7Trif Cristian 7

Hi Maharajan,

It works! Thanks bro :).

I changed a little bit the code, like this:

 

global class SetTitleToAttachmentBatch implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
         
        String query = 'SELECT Id, ParentID, NAME FROM Attachment';
        return Database.getQueryLocator(query);
    }
     
    global void execute(Database.BatchableContext BC, List<Attachment> attList) {
        Set<String> woIDs= new Set<String>();

        for (Attachment  orderRec : attList) {
            woIDs.add(orderRec.ParentID);
        }
        
        Map<String, SVMXC__Service_Order__c > woByIds= new Map<String, SVMXC__Service_Order__c >([
        SELECT Id, Name, SVMX_PS_Ship_To_Name__c, SVMXC__Order_Type__c, SerialNumberIP__c
        FROM SVMXC__Service_Order__c 
        WHERE Id =: woIDs
        ]);
        
        for(Attachment a: attList){
           SVMXC__Service_Order__c workorder = woByIds.get(a.ParentId);
            System.debug('workorder '+ workorder );
                if(workorder != null){
                    if(a.Name.startsWith('SIG_Work_Order_Service__Reports')){
                        a.Name =  'SR'+ '_'+  date.today().format()+ '_'+workorder.SVMX_PS_Ship_To_Name__c+ '_' + workorder.Name+ '_'+ workorder.SVMXC__Order_Type__c+'_'+ workorder.SerialNumberIP__c +'.pdf';
                    }
                    else if(a.Name.Startswith('SIG_HandoverCheckList_Customer_To_SIG_Report')){
                        a.Name = 'Handover - Customer to SIG' +'.pdf';
                    }
                   
                }
             
        } 
        
    
        try {
            update attList;
         
        } catch(Exception e) {
            System.debug(e);
        }
         
    }   
     
    global void finish(Database.BatchableContext BC) {
    }
}

 

But now I would need somehow to increment this attachments by 1 everytime it is changed. It is possible to do that?

User-added image

 

You see? They are the same and I would need something like:

 

SR1_6/16/2020_GreenSpot
SR2_6/16/2020_GreenSpot
SR3_6/16/2020_Greenspot

How can i increment this everytime a new attachment is updated... any idea?

 

Thanks!