function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chiranjib routchiranjib rout 

how to bulkify trigger,getting issue in trigger

I have 3 objects appointment,invoice and revenue.Invoice is having look up to appointment. There are some appointments which is having invoice and some appointments does not have invoice.There is a common field in both objects that is ecode. So here my requirement is in the revenue object i will put the ecode.If the ecode is present in the invoice then the revenue calculation should happen from invoice object.If the ecode does not present in the invoice then the calculation will happen from appointment. I have written the trigger also. So when i am performing the action manually the trigger is working fine.But when i am uploading through data loader since i have taken invoicelist.size the size is coming more than zero and the calculation is going to the second loop.
trigger Revenue_calculations on Revenue__c (before insert, before update) {

Map<id,String>reids=new Map<id,String>();   
set<string>ecode=new set<string>();
set<integer>Bmonth=new set<integer>(); 
List<Revenue__c> revlist=new List<Revenue__c>();
List<Invoice__c>invlist=new List<Invoice__c>();

List<Appointment__c >apps= new List<Appointment__c>();
List<Appointment__c >apps2= new List<Appointment__c>();

Decimal InvoiceFinalamount;   
String clientname;
String talentname;


Decimal amount;

for(Revenue__c re:trigger.new){

    ecode.add(re.ECode__c);

    }

    apps= [select Contact_Talent__r.name,DOJ__c,Last_Working_Date__c,Employer__r.name, ECode__c  from Appointment__c where ECode__c IN:ecode];

    system.debug('***ecode**'+ecode);
    system.debug('&&Bmonth&&'+Bmonth);
    invlist=[select id,name,Emp_Code__c,Sub_Total__c,Status_of_Invoice__c,T from Invoice__c where Emp_Code__c IN:ecode AND CALENDAR_MONTH(Billing_Month__c) IN:Bmonth ];

    system.debug('%%%invlist%%'+invlist);

    system.debug('invlist.size()'+invlist.size());



    if(invlist.size()==0 ){

    apps2=[select HR_Initial_BillRate__c,Initial_Billable_Hours_Days__c,ECode__c from Appointment__c where ECode__c IN:ecode ];
    system.debug('apps2***'+apps2);
    for(Revenue__c re:trigger.new){

    amount=0;
    for(Appointment__c ap:apps2){

       if(ap.ECode__c ==re.ECode__c){

         system.debug('inside new loop'+re.ECode__c);

             amount= ap.HR_Initial_BillRate__c*66;



          re.Revenue_Without_ST__c=amount;
          re.invoice_exist__c =false;


         }

         }}}






    if( invlist.size()!=0)
{
    for(Revenue__c re:trigger.new){   
    ecode.add(re.ECode__c);
    Bmonth.add(re.Salary_Processed_Month__c.month());                
    integer count=0;
    InvoiceFinalamount=0;

     Integer count1=0;


     system.debug('++invlist size++'+invlist.size());
    for(Invoice__c Inv:invlist){
     system.debug('^^Inv.Emp_Code__c^^'+Inv.Emp_Code__c);
     system.debug('22Inv.Type_of_Invoices__c22'+Inv.Type_of_Invoices__c);
     system.debug('$$re.Invoice_Type__c$$'+Inv.Billing_Month__c);


     if(re.Invoice_Type__c==Inv.Type_of_Invoices__c ) {



       if(re.ECode__c==Inv.Emp_Code__c && re.Salary_Processed_Month__c.month()==Inv.Billing_Month__c.month()&& re.Salary_Processed_Month__c.year()==Inv.Billing_Month__c.year() ){
        count=count+1;
        SYSTEM.DEBUG('Inv.Sub_Total__c+++++ '+Inv.Sub_Total__c);


       InvoiceFinalamount+= Inv.Sub_Total__c*72;

       }


      }

      system.debug('CCCcountCCC'+count);
      system.debug('$$InvoiceFinalamount$$'+InvoiceFinalamount);
    }




   for(Appointment__c ap :apps){  
    if(re.ECode__c == ap.ECode__c ){
  re.Talent_Name__c=ap.Contact_Talent__r.name;

  re.Client_Name__c=ap.Employer__r.name;
  re.DOJ__c=ap.DOJ__c;
  re.Last_Working_Date__c=ap.Last_Working_Date__c;
  re.Actual_PO_value__c =ap.HR_Initial_BillRate__c;
  re.Rate_pattern__c=ap.HR_Rate_Pattern__c;
  re.Currency_Type__c=ap.Currency_Type__c;



   }
  }


  re.invoice_exist__c =true;
  re.Revenue_Without_ST__c=InvoiceFinalamount;

  }
  } 
}