You need to sign in to do that
Don't have an account?

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
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
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
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.