Skip to main content Join the Agentforce Virtual Hackathon to build innovative solutions and compete for a $50k Grand Prize. Sign up now. Terms apply.

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.
2 answers
  1. Jan 7, 2015, 9:31 AM
    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
Loading
0/9000