0

I want to show an error message if on inserting an opportunity the total of Amount field of all the opportunities inserted that day exceeds 100,000.

trigger LimitTotalAmountInOpportunity on Opportunity (before insert) {

    double totalamount = 0;
    for(opportunity op1 : [select Amount from opportunity where createdDate = today and createdById =: UserInfo.getUserId()]){
        totalamount += op1.amount;                      
    }

    for(opportunity op2 : trigger.new){
        totalAmount += op2.Amount;
        if(totalAmount > 100000){
            op2.addError('Total Amount Limit Exceded'); 
        }  
   }   
}

I am getting a run time error on line " totalamount += op1.amount; "

Error :

LimitTotalAmountInOpportunity: execution of BeforeInsert caused by: System.NullPointerException: Argument cannot be null

1 Answer 1

1

Make sure op1.amount; field has a value.

Otherwise add a null check before adding value into totalamount in both the for loops

if(op1.amount != null)
    totalamount += op1.amount; 
0

You must log in to answer this question.

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