Ads

Monday 15 April 2013

Trigger to Warn for Duplicate Attachment Names and Contents in Salesforce?

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

Trigger:

/*    Trigger to Warn for Duplicate Attachment Names and 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.Name == attachmnt.Name)
{
attachmnt.Name.addError('Duplicate Name. Filename already exists.');
}
if(temp.Description == attachmnt.Description)
{
String str = 'Similar content already exists in ' + temp.Name;
attachmnt.Description.addError(str);
}
}
}
}

No comments:

Post a Comment