create one filed in any object like account or contact..create field "attachment obtained" checkbox.if file is attached checkbox should be check .otherwise file is deleted means checkbox should uncheck using triggers ...can anyone help this questions?
You can create an after insert trigger on Attachment that counts the associated Attachments and accordingly updates the AttachmentObtained__c checkbox. The trigger code is as below.
trigger AttachmentObtainedTrigger on Attachment (after insert) {
if(Trigger.isAfter){
if(Trigger.isInsert){
Set<Id> accountRecordIdSet = new Set<Id>();
Set<Id> contactRecordIdSet = new Set<Id>();
for(Attachment attachmentRec :Trigger.new){
//Check if the Parent is an Account
if(attachmentRec.ParentId.startsWith('001'){
accountRecordIdSet.add(attachmentRec.ParentId);
}
//Check if the Parent is an Contact
if(attachmentRec.ParentId.startsWith('003'){
contactRecordIdSet.add(attachmentRec.ParentId);
}
}//for
//Update AttachmentObtained__c for Account
if(!accountRecordIdSet.isEmpty()){
for(Account[] accountList :[Select Id, AttachmentObtained__c
From Account
Where Id IN :accountRecordIdSet]){
for(Account accRec :accountList){
if(accRec != null){
if(accRec.AttachmentObtained__c == false){
accRec.AttachmentObtained__c = true;
}
}
}
update(accountList);
}
}//if
//Update AttachmentObtained__c for Contact
if(!contactRecordIdSet.isEmpty()){
for(Contact[] contactList :[Select Id, AttachmentObtained__c
From Contact
Where Id IN :accountRecordIdSet]){
for(Contact contactRec :contactList){
if(contactRec != null){
if(contactRec.AttachmentObtained__c == false){
contactRec.AttachmentObtained__c = true;
}
}
}
update(contactList);
}
}//if
}
}
}//AttachmentObtainedTrigger ends
You can create an after insert trigger on Attachment that counts the associated Attachments and accordingly updates the AttachmentObtained__c checkbox. The trigger code is as below.