You need to sign in to do that
Don't have an account?
Alex Wong 4
How to write a trigger for renaming attachments in custom objects?
I am quite new for salesofrce and I want to write a trigger to modify attachments name after user inserting the attachments. For my situation, my page is form for public input their information. It includes 3 question that requires users to insert attachments. One question can allow users to upload 1-3 attachmetns, and the other two require only 1. Since I want to classify them from the their name, I want a trigger to rename the attachments after insert. Thank you for your great effort.
All Answers
Yes you have to write a trigger on Attachment, running in after insert context.
Example:
There should be 3 type of attachment, A, B and C. For example, team A submit the form. I want the attachments to be TeamA_A.
Thank you.
If the user is call UserA, I want the name of the attachments input become UserA_QA, UserA_QB and UserA_QC.
Thank you.
Data for UserName should be the data insert by the public and the data will be inserted into a field of custom object (API Name: Artist_English_Name__c)
Thank you.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_userinfo.htm
To be specify, the format of name should be like this: (RecordID)_(UserName)_(DateOfCreate)_(QA). Can a trigger identify which attachment belongs to which question?
(RecordID)_(UserName)_(DateOfCreate)_(Q)
RecordID is the name of the record of custom object.
UserName is the data insert by users in the field "Artist_English_Name__c"
DateOfCreate is the date of creating the record of custom object.
Q is the question that the attachment belongs to.
Because I want to trace back the attachment ie owned by who and which question.
Really appreciate your effort, thank you.
The method is below:
https://help.salesforce.com/apex/HTViewSolution?id=000007269&language=en_US
Thank you!
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_attachment.htm Below link you can refer for the valid values:
http://salesforce.stackexchange.com/questions/102542/valid-values-for-contenttype (http://salesforce.stackexchange.com/questions/102542/valid-values-for-contenttype)
And this link as well
http://help.salesforce.com/HTViewSolution?id=000187992&language=en_US
Here is my code:
The example:
"a002800000i5q8UAAQ_Alex_2016-08-03_Technical_field2.png"
With this code, I can retain the the file type in the name. But I still want to delete the original attachment name ("field2" in this case), coz it may be too long for the name. Is there any way to only mantain ".png" in this case, instead of "field2.png"?
Thank you.
Here is the recent result: "a002800000i5q8UAAQ_Alex_2016-08-03_Technical_photo.png"
with this code: But I want it to be: ""a002800000i5q8UAAQ_Alex_2016-08-03_Technical.png""
Thank you.