+ Start a Discussion
DevelopementDevelopement 

Trigger - Attachment

Hello,

I have this Trigger that update the checkbox to true if an Opp has an attachment. This Trigger work fine.

 

trigger Att_Aft_Trig on Attachment (after insert, after update) 
 {
                        List<Opportunity> co = [select id, Attachement__c from Opportunity where id =: Trigger.New[0].ParentId];
                        if(co.size()>0)
                        {
                                    co[0].Attachement__c = true;
                                    update co;
                        }                                                                   
            
}

 

 

But I want to know if I remove the Attachment the checkbox should be unchecked?How should I do this in this trigger?

 

Thanks,

Shruti

Best Answer chosen by Developement
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

This is not bulkified... If needed or not its good to bulkify trigger.

 

Go through this for bulkification 

http://wiki.developerforce.com/page/Best_Practice%3A_Bulkify_Your_Code

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_triggers_context_variables.htm|StartTopic=Content%2Fapex_triggers_context_variables.htm|SkinName=webhelp

 

trigger Att_Aft_Trig on Attachment (after insert, after update,after delete)
{
if(trigger.isInsert || trigger.isUpdate){
List<Opportunity> co = [select id, Attachement__c from Opportunity where id =: Trigger.New[0].ParentId];
if(co.size()>0)
{
co[0].Attachement__c = true;
update co;
}
}
if(trigger.isDelete){
List<Opportunity> co1 = [select id, Attachement__c from Opportunity where id =: Trigger.old[0].ParentId];
if(co1.size()>0)
{
co1[0].Attachement__c = false;
update co1;
}

}
}

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

This is not bulkified... If needed or not its good to bulkify trigger.

 

Go through this for bulkification 

http://wiki.developerforce.com/page/Best_Practice%3A_Bulkify_Your_Code

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_triggers_context_variables.htm|StartTopic=Content%2Fapex_triggers_context_variables.htm|SkinName=webhelp

 

trigger Att_Aft_Trig on Attachment (after insert, after update,after delete)
{
if(trigger.isInsert || trigger.isUpdate){
List<Opportunity> co = [select id, Attachement__c from Opportunity where id =: Trigger.New[0].ParentId];
if(co.size()>0)
{
co[0].Attachement__c = true;
update co;
}
}
if(trigger.isDelete){
List<Opportunity> co1 = [select id, Attachement__c from Opportunity where id =: Trigger.old[0].ParentId];
if(co1.size()>0)
{
co1[0].Attachement__c = false;
update co1;
}

}
}

This was selected as the best answer
DevelopementDevelopement

Thanks a lot Sai!