You need to sign in to do that
Don't have an account?
Jan Kopejtko 2
Simple code to update a file name
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:
Help me please!
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!
All Answers
Are you using ContentDocument to upload the files? If so Here is the code. else please ignore the below code.
Trigger:
-------------------
trigger AttachmentTrigger on ContentDocument (before insert) {
if(Trigger.isBefore) {
if(Trigger.isInsert) {
AttcahmentTriggerHandler.onBeforeInsert(Trigger.New);
}
}
}
Handler:
---------------------
public with sharing class AttcahmentTriggerHandler {
public static void onBeforeInsert(List<ContentDocument> contentDocuments) {
List<Id> accountIds = new List<Id>();
//Loop to iterate over the list of Content Document
for(ContentDocument doc : contentDocuments) {
if(doc.ParentId != null) {
accountIds.add(doc.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(ContentDocument doc : contentDocuments) {
doc.Title = accountMap.get(doc.ParentId).Name;
}
}
}
}
I hope this helps.
Thanks
Anyways - could you help me with this - I can't upload files if I put any trigger on ContentDocument. It throws me an error when I upload any file. If I disable the trigger it starts working again. The things is that there is no error message. I'm trying the debug log now.
And this is the bug I'm recieving:
I gave you the trigger before event. It should not lock the record can you please check if there are any other triggers? or something related to isAfter on the content document?
trigger ContentDocumentDescription on ContentDocument (before insert) { --> line changed
for(ContentDocument a :Trigger.New) {
a.Description = 'test';
}
}
if you use after insert then you will get the error because the record is in ready only mode
Oh I see it, yes I fixed it, code looks like this now:
I'm now getting an error:
System.SObjectException: Field is not writeable: ContentDocument.Description
If I open the file, I can edit it manually. Why is it not writeable?
I'm getting this error now:
What do I do?
How did you solved your problem for "Field is not writeable: ContentDocument.Title" ?
Thanks.