You need to sign in to do that
Don't have an account?

bulkify trigger
can any one tell me how can i bulkify this trigger
trigger pointamount on Opportunity ( after insert,after update,before delete) { if(trigger.isInsert) { for (Opportunity o : Trigger.new) { if(o.accountid!=null && o.Purchase_Type__c == 'Pilot'&& o.Catalog_Source__c== 'SimCenter') { points__c a = new Points__c(); a.Account__c = o.Accountid; a.Points_Transaction_Amount__c =o.amount-o.amount*2; a.Points_Transaction_Reason__c =' Points Redeemed'; a.Points_Transaction_Date__c=o.CloseDate; a.Purchaser_Email__c=o.Contact_Email__c; a.Opportunity__c=o.id; a.Purchaser_Name__c=o.Purchaser_Name__c; insert a; } } } if(trigger.isupdate) { for (Opportunity o : Trigger.new) { if(o.accountid!=null && o.Purchase_Type__c == 'Pilot'&& o.Catalog_Source__c== 'SimCenter') { Points__c[] fn =[select id,account__c,opportunity__c from Points__c where opportunity__c=:o.id]; // fn[0].Points_Transaction_Amount__c=o.amount-o.amount*2; // update fn; if(fn.size()==0){ points__c a = new Points__c(); a.Account__c = o.Accountid; a.Points_Transaction_Amount__c =o.amount-o.amount*2; a.Points_Transaction_Reason__c =' Points Redeemed'; a.Points_Transaction_Date__c=o.CloseDate; a.Purchaser_Email__c=o.Contact_Email__c; a.Opportunity__c=o.id; a.Purchaser_Name__c=o.Purchaser_Name__c; insert a; } if(fn.size()>0){ fn[0].Points_Transaction_Amount__c=o.amount-o.amount*2; update fn; } } } } if(trigger.isdelete ) { for (Opportunity o : Trigger.old) { if(o.accountid!=null &&o.Purchase_Type__c == 'Pilot'&& o.Catalog_Source__c== 'SimCenter') { Points__c fn =[select id,account__c,opportunity__c from Points__c where opportunity__c=:o.id ]; delete fn; } } } }
Hi,
Try this.
For further details : http://wiki.developerforce.com/index.php/Best_Practice:_Bulkify_Your_Code
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Chamil's Blog
Hi,
Ideally you should nto be using dml statements inside a for loop. In a bulk trigger, this alone may cause your code to cross governor limits.
For e.g. -
Instead of this part -
Write it like this
This will ensure lesser DML statements run, when running in bulk.
Hope this helps.
Regards,
Ritika
hi brother very much thanks for u r reply and u r trigger is failing in two cases one update and delete when i am deleting a record its giving this
error
pointamount: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.pointamount: line 5, column 31: []
and while update ting this part of code is not workin
Hello srikant,
Below is the code updated to be bulkified. Although I have had some difficulty uinderstanding some of your code. Specially this part:
Points__c[] fn =[select id,account__c,opportunity__c from Points__c where opportunity__c=:o.id];
and then here:
if(fn.size()>0{
fn[0].Points_Transaction_Amount__c=o.amount-o.amount*2;
Basically you are getting a list of points for a given Opportunity, BUT then you only look at the first returned points record for the Opportunity and update only that one. Is this on purpose or do you actually want to update all the points for the Opportunity??
Anyhow, below is the bulkified code, I do hope it helps you.