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
Cris9931Cris9931 

trigger to update attachment name

Hello, I want to update my attachments name after they are created. This works but the name of the Work Order(custom object) is not automatically. User-added imageThis is my attachments and you can see Related to : WO-0003061.

my trigger, it works if i hard code the name:

 

trigger SetTitleToAttachment on Attachment (before insert) { 


        for(Attachment a: Trigger.new){
          
            if(a.Name.startsWith('SIG_PM_Work_Order_Service_Report')){
                a.Name = 'Work Order name dynamically ';
            } else if (a.Name.startsWith('SIG_HandoverCheckList_Customer_To_SIG_Report')){
                a.Name = 'ServiceReport.pdf';
            }
            
        }


}


I want to on the line  a.Name = 'Work Order name dynamically '; to be instead of this string to be the name WO-0003061. How can i do this?

Please help. I would appreciate a lot :)

Agustin BAgustin B
Hey Sarah, try this: a.Name = a.RelatedTo.Name
Being relatedTo the field you have under that label for example a.RelatedTo__r.Name

Check that out. 
If it helps please like and mark as correct as it may help others.
If it does not work please use a system.debug(a.RelatedTo__r.Name) to check if you can see that field, also system.debug(a.RelatedTo__c) to check if you at least have the reference.
Cris9931Cris9931

HI Agustin,

Nope... I tried with RelatedTo.. in fact it is ParentID cause its a reference
my code works fine like this:

 

trigger SetTitleToAttachment on Attachment (before insert) { 

 List<String> woIDs= new List<String>();
    for (Attachment  orderRec : Trigger.new) {
        woIDs.add(orderRec.ParentID);
    }
System.Debug('>>>>'+woIDs);


Map<String, SVMXC__Service_Order__c > woByIds= new Map<String, SVMXC__Service_Order__c >([
        SELECT Id, Name, SVMX_PS_Ship_To_Name__c, CreatedDate, SVMXC__Order_Type__c
        FROM SVMXC__Service_Order__c 
        WHERE Id =: woIDs
    ]);
    
 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_PM_Work_Order_Service_Report')){
                a.Name =  'SR'+ '_'+ workorder.CreatedDate + '_'+workorder.SVMX_PS_Ship_To_Name__c+ '_' + workorder.Name+ '_'+ workorder.SVMXC__Order_Type__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';
             }
            }
        }

}


Do you know how can I make an like an autonumber in this trigger?
Integer i=1;

for example where a.Name = 'Handover Customer To SIG'+ '-'+ i +'.pdf';

so everytime should be incremented by one so the result to be:

Handover Customer To SIG -1.pdf
Handover Customer To SIG -2.pdf..

 

Agustin BAgustin B
HI Sarah you can created like you wrote above.
Integer i=1;
a.Name = 'Handover Customer To SIG'+ '-'+ i +'.pdf';
and before the closing } of the for you put i++; to increment the counter by 1.
Cris9931Cris9931

User-added image

I created two attachments.. not working..

my code:

 

trigger SetTitleToAttachment on Attachment (before insert) { 

 List<String> woIDs= new List<String>();
    for (Attachment  orderRec : Trigger.new) {
        woIDs.add(orderRec.ParentID);
    }
System.Debug('>>>>'+woIDs);


Map<String, SVMXC__Service_Order__c > woByIds= new Map<String, SVMXC__Service_Order__c >([
        SELECT Id, Name, SVMX_PS_Ship_To_Name__c, CreatedDate, SVMXC__Order_Type__c
        FROM SVMXC__Service_Order__c 
        WHERE Id =: woIDs
    ]);
    
 System.debug('woByIds'+ woByIds);
 Integer i = 1;
        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_PM_Work_Order_Service_Report')){
                a.Name =  'SR'+ '_'+ workorder.CreatedDate + '_'+workorder.SVMX_PS_Ship_To_Name__c+ '_' + workorder.Name+ '_'+ workorder.SVMXC__Order_Type__c+'.pdf';
             } else if (a.Name.startsWith('SIG_HandoverCheckList_Customer_To_SIG_Report')){
                a.Name =  'Handover Customer To SIG'+ '-'+ i +'.pdf';
                i++;
             } else if (a.Name.startsWith('SIG_HandoverCheckList_SIG_To_Customer')){ 
                a.Name = 'Handover SIG_To_Customer'+'.pdf';
             }
            }
        }

}