+ Start a Discussion
amateur1amateur1 

multiple query bulkification trigger

this is the trigger i have written i know bulkification for singl queries but i am still confused for multiple query bulkification of trigger please check my trigger and give me bulkification code for it

 

 

trigger CreateCloneScheduleRevenue on OpportunityLineItem (after update)
{
public list<Custom_Revenue_Schedule__c > n1 {get;set;}
n1 =new list<Custom_Revenue_Schedule__c >();
set<id> pricebookid=new set<id>();
set<id> oliid=new set<id>();
id opp;
for(opportunitylineitem ols:trigger.new)
{
pricebookid.add(ols.pricebookentryid);
oliid.add(ols.id);
opp=ols.opportunityid;
}
pricebookentry pbe=[select id,Product2Id from pricebookentry where id=:pricebookid];
product2 p2=[select id from product2 where id=:pbe.product2id];
Custom_Revenue_Schedule__c[] c=[select id from Custom_Revenue_Schedule__c where Product__c=:p2.id];
if(c.size()>0)
{
delete c;
}
opportunitylineitemschedule[] olsc=[select id,Quantity,Revenue,ScheduleDate from opportunitylineitemschedule where opportunitylineitemid=:oliid];
for(opportunitylineitemschedule olsc1:olsc)

{
Custom_Revenue_Schedule__c c1=new Custom_Revenue_Schedule__c();
c1.Schedule_Revenue__c=olsc1.Revenue;
c1.Date__c=olsc1.ScheduleDate;
c1.Opportunity__c=opp;
c1.Product__c=p2.id;
n1.add(c1);

}


insert n1;
}

SRKSRK

Is this trigger gives you any Error while upload multiple record

plz share the error it whold me much more help ful

amateur1amateur1

i am not getting error but this is not a bulkified trigger i want to know how this can be bulkified

SRKSRK

u can have follwing error with this trigger

2 Many Script Statement
OR DML lst can update uptp 10000 records at once

 

As u r inserting Custom_Revenue_Schedule__c form this trigger if any tirgger is persent on this obj it also run under this trigger instance

so it would he grate if u can share u error

Bhawani SharmaBhawani Sharma
pricebookentry pbe=[select id,Product2Id from pricebookentry where id=:pricebookid];

 You must use IN clause here. Make sure to use collection properly while dealing with Bulk Triggers.

 

Set<Id> pBEIds = new Set<Id>();

pricebookentry pbe=[select id,Product2Id from pricebookentry where id\ IN :pricebookid];