Trigger to Warn for Duplicate Attachment Names in Salesforce


The below trigger is used to warn the users, if the Filename of  an attachment already exists.

Trigger:

/*    Trigger to Warn for Duplicate Attachment    */
trigger DuplicateAttachment on Attachment (before insert)
{
for(Attachment attachmnt:trigger.New)
{
String sql = 'SELECT Name FROM Attachment';
List<Attachment> atchmntNams = Database.Query(sql);

for(Attachment temp:atchmntNams)
{
if(temp.Name == attachmnt.Name)
{
attachmnt.Name.addError('Duplicate Name. Filename already exists.');
}
}
}
}

No comments:

Post a Comment