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
Jan Kopejtko 2Jan Kopejtko 2 

Trigger to update file title after insert

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.

I don't know how to query for that. File is ContentDocument, but there has to be a different way. Thanks alot. Jan
Best Answer chosen by Jan Kopejtko 2
PRAKASH JADA 13PRAKASH JADA 13
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;
            }
            
        }
        
    }
}


Here I used the Account as parent object you change it to your object Name and I think helps you. 

All Answers

PRAKASH JADA 13PRAKASH JADA 13
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;
            }
            
        }
        
    }
}


Here I used the Account as parent object you change it to your object Name and I think helps you. 
This was selected as the best answer
Jan Kopejtko 2Jan Kopejtko 2
Thanks Prakash