+ Start a Discussion
ramya1ramya1 

trigger bulkification

HI

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

I am sending the trigger . 

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

 

 

trigger WOW_Block_Time on Appointment__c (before Insert,before update)
{
for(Appointment__c APP : Trigger.new)
{
Time_blocks__c[] TBS = [select id,name,branch__c,date__c,staff__c from Time_blocks__c where Branch__c =:APP.Branch__c AND Date__c =:APP.Appt_Date__c AND RecordTypeId='012O0000000CldF'];

if(TBS.size()>0)
{
App.adderror('Blocked Branch!!');
}

Time_blocks__c[] TBS1 = [select id, name , Branch__c , date__c , staff__c from Time_blocks__c where Branch__c =:APP.Branch__c AND Date__c =:APP.Appt_Date__c AND staff__c =:App.Doctor__c AND RecordTypeId='012O0000000CldK'];

if(TBS1.size()>0)
{
App.adderror('Blocked staff');
}

Time_blocks__c[] TBS2 = [select id,Appt_24_Frm_Hour__c,Appt_24_Frm_Min__c,Apt_24_To_Hour__c,Apt_24_To_Min__c from Time_blocks__c where
Branch__c =:APP.Branch__c AND Date__c =:APP.Appt_Date__c AND staff__c =:App.Doctor__c AND RecordTypeId='012O0000000ClgY'
AND Appt_24_Frm_Hour__c !=:null AND Apt_24_To_Hour__c !=:null ];
if(TBS2.size()>0)
{
for(Time_blocks__c tbs3:tbs2)
{
integer fromhr=integer.valueof(tbs3.Appt_24_Frm_Hour__c);
integer frommin=integer.valueof(tbs3.Appt_24_Frm_Min__c);
integer tohr=integer.valueof(tbs3.Apt_24_To_Hour__c );
integer tomin=integer.valueof(tbs3.Apt_24_To_Min__c);

integer hr=integer.valueof(app.Appt_Hour__c);
integer min=integer.valueof(app.Appt_Minute__c);

if(app.Appt_AM_PM__c=='AM')
{
if((hr>=fromhr &&hr<=tohr&&min>=frommin&&min<=tomin)||(hr>fromhr &&hr<tohr)|| (hr>=fromhr &&hr<tohr&&min>=frommin&&min>=tomin)||(hr>fromhr &&hr<=tohr&&min<=frommin&&min<=tomin))
{
app.adderror('time blocked');
}
}
else if(app.Appt_AM_PM__c=='PM')
{
if(hr==12)
{
hr=12;
}
else
{
hr=hr+12;
}
if((hr>=fromhr &&hr<=tohr&&min>=frommin&&min<=tomin)||(hr>fromhr &&hr<tohr)|| (hr>=fromhr &&hr<tohr&&min>=frommin&&min>=tomin)||(hr>fromhr &&hr<=tohr&&min<=frommin&&min<=tomin))
{
app.adderror('time blocked1');
}

}


}

}

Time_blocks__c[] TBS4 = [select id,date__c from Time_blocks__c where Date__c =:APP.Appt_Date__c AND RecordTypeId='012O0000000ClhH'];

if(TBS4.size()>0)
{
App.adderror('Blocked date');
}


}

}

Chamil MadusankaChamil Madusanka

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

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.

Praveen_K.ax1208Praveen_K.ax1208

Hi Ramya,

 

We have some best practices to write an apex code. PFA link to understand the best practices.

 

use some collections like set,list and maps.

also avoid SOQL queries in for loops because this will increase the number of queries.

 

Thanks,

Praveen K.