0

I have written this below trigger on revenue object.whenever i am trying to create a record

"Error: Invalid Data. Review all error messages below to correct your data. Apex trigger RevenueWithoutST caused an unexpected exception, contact your administrator: RevenueWithoutST: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.RevenueWithoutST: line 105, column 1"

error is showing. so any suggestions on how to avoid it?

Trigger RevenueWithoutST on Revenue__c (after insert, before update) {

        List<SCSCHAMPS__Appointment__c> appList;

        List<Revenue__c> revlist;

        Revenue__c r= new Revenue__c();

        string eCode;

        date salaryDate;

        string category;

        string type;

        for(Revenue__c rev:Trigger.new)

        {

            eCode=rev.ECode__c;

            salaryDate=rev.Salary_Processed_Month__c;

            category=rev.InvoiceCategory__c;

          type=rev.Invoice_Type__c;



            if(eCode!=null)

            {

               appList =[select AvvasECode__c,Name_of_the_Candidate__c,Client_Name__c,Date_of_Onboarding__c,

                        Last_Working_Date__c,Location__c from SCSCHAMPS__Appointment__c where AvvasECode__c=: rev.ECode__c] ;



                for(SCSCHAMPS__Appointment__c app : appList)

                {

                    r.Talent_Name__c= app.Name_of_the_Candidate__c;



                }



            }

            if(eCode!=null && category=='individual' && salaryDate!=null && type=='Time& Material' )

            {

                List<Invoice__c> InvList;

                Invoice__c inv= new Invoice__c();

                InvList=[select id,Name,Emp_Code__c,Invoice_Category__c,

                         Sub_Total__c from Invoice__c where Emp_Code__c=: rev.ECode__c

                           ];

                    FOR(Invoice__c inc : InvList)

                    {

                      r.Revenue_Without_ST__c=inc.Sub_Total__c;

                    }



            }

            else if(eCode!=null && category=='consolidated')

            {

                List<Invoice_Line_Item__c> ILineList;

              Invoice_Line_Item__c iline=new Invoice_Line_Item__c();

                ILineList=[select id,Name,Employee_Code__c,Total__c

                           from Invoice_Line_Item__c where Employee_Code__c=:rev.ECode__c ];

                for(Invoice_Line_Item__c IL : ILineList)

                {

                  r.Revenue_Without_ST__c=IL.Total__c; 

                }

            }



            revlist.add(rev);





        }

        update revlist;
}
1
  • Where is line 105.
    – Eric
    Commented Jul 28, 2016 at 6:01

2 Answers 2

3

There are couple of issues with your code.

  1. You have not initialized list.
  2. You have soql inside for loop.
  3. You have unnecessary variables in your code.

So I have modified your code with some best practices.

    Trigger RevenueWithoutST on Revenue__c (after insert, before update) {

    List<Revenue__c> revlist = new List<Revenue__c>(); //initialize list


    Set<String> eCodeSet = new Set<String>();
    for(Revenue__c rev:Trigger.new)
    {
        if(rev.ECode__c!=null){
            eCodeSet.add(rev.ECode__c);
        }
    }


    //get all the SCSCHAMPS__Appointment__c records
    Map<String,SCSCHAMPS__Appointment__c> appMap = new Map<String,SCSCHAMPS__Appointment__c>();
    Map<String,Invoice__c> invoiceMap = new Map<String,Invoice__c>();
    Map<String,Invoice_Line_Item__c> invoiceLineItemMap = new Map<String,Invoice_Line_Item__c>();
    for(SCSCHAMPS__Appointment__c appointment : [select AvvasECode__c,Name_of_the_Candidate__c,Client_Name__c,Date_of_Onboarding__c,

                    Last_Working_Date__c,Location__c from SCSCHAMPS__Appointment__c where AvvasECode__c in : eCodeSet])
    {
        appMap.put(appointment.AvvasECode__c,appointment);
    }

    //get all invoice records
    for(Invoice__c invoice : [select id,Name,Emp_Code__c,Invoice_Category__c,

                     Sub_Total__c from Invoice__c where Emp_Code__c where Emp_Code__c in : eCodeSet])
    {
        invoiceMap.put(invoice.Emp_Code__c,invoice);
    }

    //get all invoice lineitem records
    for(Invoice_Line_Item__c lineItem : [Select id,Name,Employee_Code__c,Total__c

                       from Invoice_Line_Item__c where Employee_Code__c : eCodeSet])
    {
        invoiceLineItemMap.put(lineItem.Employee_Code__c,lineItem);
    }



    for(Revenue__c rev:Trigger.new)

    {


        if(rev.ECode__c!=null && appMap.containsKey(rev.ECode__c))
        {
                    r.Talent_Name__c=  appMap.get(rev.ECode__c).Name_of_the_Candidate__c;
        }

        if(eCode!=null && rev.InvoiceCategory__c=='individual' && rev.Salary_Processed_Month__c!=null && rev.Invoice_Type__c=='Time& Material' && invoiceMap.containsKey(rev.ECode__c))
        {
                  r.Revenue_Without_ST__c=invoiceMap.get(rev.ECode__c).Sub_Total__c;
        }

        else if(rev.ECode__c!=null && rev.InvoiceCategory__c=='consolidated' && && invoiceLineItemMap.containsKey(rev.ECode__c))

        {
              r.Revenue_Without_ST__c=invoiceLineItemMap.get(rev.ECode__c).Total__c; 

        }

    }
}
1
  • thank u. but now "Apex trigger RevenueWithoutST caused an unexpected exception, contact your administrator: RevenueWithoutST: execution of AfterInsert caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: Trigger.RevenueWithoutST: line 113, column 1" is showing
    – codex91
    Commented Jul 28, 2016 at 6:08
1

You have to initialize revlist variable. line 2:

List<Revenue__c> revlist = new List<Revenue__c>();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .