Apex trigger to show error message if sum of Opportunity amount is greater then 100000 of logged in user.

No comments
Trigger:

trigger LimitTotalAmountInOpportunity on Opportunity (before insert) {

         LimitTotalAmountInOpportunityHandler.LimitAmount(Trigger.new);
}


Handler Class:

public class LimitTotalAmountInOpportunityHandler {
public static void LimitAmount(list<Opportunity> OpportunityList)
{
        double totalamount = 0;
       for(opportunity objopportunity : [select Amount from opportunity where createdDate = today                and createdById =: UserInfo.getUserId()]){
             if(objopportunity.amount != null)
                    totalamount += objopportunity.amount;
         }

for(opportunity objopportunity : OpportunityList){
      if(objopportunity.amount != null)
            totalamount += objopportunity.amount;
              if(totalAmount > 100000){
                  objopportunity.addError('Total Amount Limit Exceded');
              }
  }
}
}

No comments :

Post a Comment