Trigger to Warn for Duplicate Attachment Contents in Salesforce

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

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

for(Attachment temp:attach)
{
if(temp.Description == attachmnt.Description)
{
String str = 'Similar content already exists in ' + temp.Name +'.';
attachmnt.Description.addError(str);
}
}
}
}

No comments:

Post a Comment