+ Start a Discussion
PRAVEEN KUMAR PASUPULETIPRAVEEN KUMAR PASUPULETI 

Trigger to update when attachment is attached to update one Checkbox?


Iam having 3 Fields in one object

1.Picklist(Yes or No)
2.Checkbox(Marked)
3.Checkbox(Unmarked)

When ever an attachment is attached the third field will be marked if Picklist Value is YES & 2nd Checkbox is marked.
Anoop yadavAnoop yadav
Hi,
Refer the below link to write the trigger on Attachment and modify accordingly.
http://www.sfdcpoint.com/salesforce/trigger-on-attachment-in-salesforce/
https://help.salesforce.com/HTViewSolution?id=000181538&language=en_US (https://help.salesforce.com/HTViewSolution?id=000181538&language=en_US)
Bhanu MaheshBhanu Mahesh
HI Praveen,

Lets suppose below Api names
test Object  ==>test_object__c
picklist Field = pickList__c
Checkbox marked = Checkbox_marked__c
Checkbox Unmarked = Checkbox_Unmarked__c

And also refer the below article how to create trigger on Attachment object
https://help.salesforce.com/HTViewSolution?id=000181538&language=en_US

Please check the below code

trigger testAttachment on Attachment (after insert) {
    Set<Id> parentId = new Set<Id>();
    List<test_object__c> lstTestObjToUpdate = new List<test_object__c>();
    Map<Id,test_object__c> maptestObjWIthId = new Map<Id,test_object__c>();
    Set<Id> testObjProcessed = new Set<Id>();
    String parentPrefix = Schema.getGlobalDescribe().get('test_object__c').getDescribe().getKeyPrefix();
    for(Attachment att: trigger.new){
        if(att.ParentId != null && String.valueOf(att.parentId).startsWith(parentPrefix)){
            parentId.add(att.ParentId);    
        } 
    }
    if(parentId.size() > 0){
        for(test_object__c tstObj :[SELECT Id,pickList__c,Checkbox_marked__c,Checkbox_Unmarked__c FROM test_object__c WHERE Id IN :parentId]){
            maptestObjWIthId.put(tstObj.Id,tstObj);    
        }   
    }
    for(Attachment att: trigger.new){
        if(att.ParentId != null && String.valueOf(att.parentId).startsWith(parentPrefix)&& !testObjProcessed.contains(att.parentId) && maptestObjWIthId != null && maptestObjWIthId.get(att.parentId) != null){
            test_object__c tstobj = maptestObjWIthId.get(att.parentId); 
            if(tstobj.pickList__c == 'Yes' && tstobj.Checkbox_marked__c && !Checkbox_Unmarked__c){
                Checkbox_Unmarked__c  = true;
               lstTestObjToUpdate.add(tstobj);
                testObjProcessed.add(tstobj.Id);
            }
        } 
    }
    if(lstTestObjToUpdate.size() > 0){
        update lstTestObjToUpdate;    
        
    }
}

Regards,
Bhanu Mahesh