Thursday, 16 April 2015

Trigger on Notes and Attachments



If we need to write a trigger on ay object in salesforce , salesforce itself gives as a cool UI to create the same? Ever you tried to do the same for Notes and Attachments in salesforce ? No... Nowhere can see such a UI. Amazing ! We need to create trigger under the Notes and attachments. Then there is a solution... Read through...

Method 1 : Create trigger from Developer console.

Click New > Apex Trigger
Select SObject as "Attachment" form dropdown available.

Method 2 : Using Force.com IDE or ANT.

Sample code :
trigger SetTitleToAttachment on Attachment (after insert) {
String Title;
Id pId;
for(Attachment att: Trigger.new){
Title=att.Name;
pId=att.ParentId;
}
List<Case> c=[select Id , Title__c from Case where Id=:pId];
List<Case> caseRecordToUpdate = new List<Case>();
if(c != Null){
for(integer i=0 ; i<c.size(); i++){
c[i].Title__c=Title;
caseRecordToUpdate.add(c[i]);
}
}
if( caseRecordToUpdate.size() > 0 ){
update caseRecordToUpdate;
}
}

No comments:

Post a Comment