+ Start a Discussion
ran67ran67 

bulkification of trigger

HI

I had wrote a trigger .it is working fine but now i want to bulkify my trigger...please help me to bulkification.

I am sending thte trigger . 

please help me for bulkification.....thanks in advance

Trigger:

----------------------
trigger UpdateTenantAllocation on Tenant_Allocations__c (after update)
 {
 public list<Invoice_and_Billing_Detail__c> INVBD {get;set;}
INVBD  = new List<Invoice_and_Billing_Detail__c>();
for(Tenant_Allocations__c t:trigger.new)
{
Location__c  l1=[select id,Bill_Tenants__c from location__c where id=:t.Location_new__c];
system.debug('............'+l1);
Invoice_and_Billing_Detail__c[] lb=[select id,Rate__c, name,Location1__c from Invoice_and_Billing_Detail__c where Location1__c=:l1.id and QB_Txn_ID__c=null ]; 
system.debug('............'+lb);
for(Invoice_and_Billing_Detail__c lb1:lb){
if(l1.Bill_Tenants__c==true)
{
lb1.Rate__c=t.Next_Period_Invoice_Amount__c;
}
else 
{
lb1.Rate__c=222;
}
 INVBD.add(lb1);
}
update  INVBD;
system.debug('............'+lb);
}
}
kiranmutturukiranmutturu

strange ..... u wrote a property in trigger...? properties generally used to exchange a value from custom front end to controller... its better first if you go through the apex docs ..

streetstreet

can u give me code for bulkification its an urgent requirement 

Abhay AroraAbhay Arora
trigger UpdateTenantAllocation on Tenant_Allocations__c (after update)
 {
 public list<Invoice_and_Billing_Detail__c> INVBD {get;set;}
INVBD  = new List<Invoice_and_Billing_Detail__c>();
for(Tenant_Allocations__c t:trigger.new)
{
Location__c  l1=[select id,Bill_Tenants__c from location__c where id=:t.Location_new__c];
system.debug('............'+l1);
Invoice_and_Billing_Detail__c[] lb=[select id,Rate__c, name,Location1__c from Invoice_and_Billing_Detail__c where Location1__c=:l1.id and QB_Txn_ID__c=null ]; 
system.debug('............'+lb);
for(Invoice_and_Billing_Detail__c lb1:lb){
if(l1.Bill_Tenants__c==true)
{
lb1.Rate__c=t.Next_Period_Invoice_Amount__c;
}
else 
{
lb1.Rate__c=222;
}
 INVBD.add(lb1);
}
update  INVBD;
system.debug('............'+lb);
}
}
Basically you need to get these queries out of the loop for example
set<id> setLocation=new set<id>();
map<id,Location__c> mLoc=new map<id,Location__c>();
for(Tenant_Allocations__c t:trigger.new){
 setLocation.add(t.Location_new__c);
}
and then do same for
Invoice_and_Billing_Detail__c[] lb=[select id,Rate__c, name,Location1__c from Invoice_and_Billing_Detail__c where Location1__c=:l1.id and QB_Txn_ID__c=null ];
For suggestion :
Get lists and maps of all the required data from trigger.new,trigger.old or triger.newmap etc

Use these for the logic inside the loops for the bulkification of the triggers
 
Abhay AroraAbhay Arora

Please confirm if your issue is solved so that i can mark this as solved