You need to sign in to do that
Don't have an account?
Trigger on Attachments
I have the requirement create a trigger on Attachment object, when i add new attachment ,check the attchment alredy aded or not?
i write like this ,please suggest me, is it correct or not?
trigger dntAddDuplicateAttchmnt on Attachment (before insert) {
if(trigger.isBefore){
for(Attachment atch:Trigger.new){
String parentID=atch.ParentId;
String atchName=atch.Name;
List<Attachment> listatch=[SELECT Name,ParentId FROM Attachment];
if(parentID==trigger.oldmap.get(parentID).ParentId && atchName==trigger.oldmap.get(parentID).Name){
atch.addError(' This file already having with same name in your attachmntes');
}
else if{
for(Attachment at:listatch){
if(at.Name==atchName && at.ParentId==parentID){
atch.addError('This file already having with same name in your attachmntes');
}
}
}
}
}
}
Your code has a few mistakes:
Below sample code will meet your requirement:
All Answers
You can remove the first if loop after querying records from your attachment object. The other if loop will do good.
More details on the requirement? An Attachment instance must be related to another record via the ParentId field. Adding a trigger like you described will enforce the requirements on any attachment, regardless of what it's attached to. Is there a specific object you're trying to target?
Your code has a few mistakes:
Below sample code will meet your requirement:
Thank you kartikeyan
ok and Thank you Rohit
You may want to consider wrapping the code in an if block to only execute for Account instances:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_id.htm
Otherwise the trigger logic will execute for attachments to any other object.