• Lionel Poncet 11
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Following the previous question: https://developer.salesforce.com/forums/ForumsMain?id=9062I000000XsipQAC

Hello,

I need to update a file title after it's upload.

That is an easy trigger, but here is the catch:

On object A is a text field. Each record of object A has some value in the text field. After I upload the file to the object A's record, I need to take the value in the text field of the record and set is as a title of the newly uploaded file.

Mr. PRAKASH JADA 13 helped me with the code:
 
Hi,


Trigger:
--------------------------
trigger AttcahmentTrigger on Attachment (before insert) {
    if(Trigger.isBefore) {
        if(Trigger.isInsert) {
            AttcahmentTriggerHandler.onBeforeInsert(Trigger.New);
        }
    }
}


Trigger Handler:
----------------------------------
/*
 * Author: Prakash
 * CreatedDate: Feb 14, 20202
*/
public with sharing class AttcahmentTriggerHandler {
    public static void onBeforeInsert(List<Attachment> attachments) {
        
        List<Id> accountIds = new List<Id>();
        
        //Loop to iterate over the list of attachments        
        for(Attachment att : attachments) {
            if(att.ParentId != null) {
                accountIds.add(att.ParentId); // Preparing the account Ids
            }
        }
        
        if(!accountIds.isEmpty()) {
            // Preparing the map that holds the account Name as a value and Id as a key
            Map<Id, Account> accountMap = new Map<Id,Account>([SELECT ID, Name FROM Account WHERE Id = :accountIds]);
            
            // Loop to iterate over the list of Attachments
            for(Attachment att : attachments) {
                att.Name = accountMap.get(att.ParentId).Name;
            }
            
        }
        
    }
}
But there is another catch to it - by object A I mean object SERVICE APPOINTMENT, which does not support the Attachment object.

Help me please!