• Daniel Peñaloza
  • NEWBIE
  • 0 Points
  • Member since 2016
  • Salesforce Developer
  • Avanxo


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies

Hello Developers!

I am trying to write a trigger on opportunity where it would throw an error when user exceeds 10,000 daily limit of opportunity amount. One sales rep can create number of opportunities up to the amount where Amount can not exceed 10,000 per day.

Here is my logic: When there is a new record inserted in system, I am getting that record's userId, and createdDate in variables. Then I have a SOQL which fetches the all opportunities in database which is created by that user and on the today's  date (I am not too sure my SOQL in the code below). Once the records are fetched, I have a loop which sumes up the amount from the fetched opportunities in SOQL and if that sum is greater then 10,000, I am throwing an error.
 

Trigger TenThousandDollarsLimit on Opportunity(Before Insert){
    // We need to write a trigger so one user can not create more then
    // $10,000 of opportunity per day.
    
    User OppOwner;
    ID OppOwnerID;
    Double AmountSum;
    Date TodaysDate;
    
    For(Opportunity opp: Trigger.New){
        
        OppOwner = opp.Owner;
        OppOwnerID = opp.OwnerId;
        TodaysDate = opp.createdDate.date();
        
        
        List<Opportunity> opps;
        opps = [Select Id, Amount FROM Opportunity WHERE OwnerId = :OppOwnerId AND CreatedDate = :TodaysDate];
        
        
        For(Integer I = 0; I < opps.size(); I++)
        {
            AmountSum = opps[I].Amount;
        }
        
        If (AmountSum > 10000){
            opp.addError('You have exceed your daily limit of $10,000');
        }
        else{
            // do nothing
        }
    }
    
}
Here is an Error Message while saving the first record:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger TenThousandDollarsLimit caused an unexpected exception, contact your administrator: TenThousandDollarsLimit: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.TenThousandDollarsLimit: line 14, column 1


Thank you for the help guys!
JSON STRING:

{"size":21,"totalSize":21,"done":true,"queryLocator":null,"entityTypeName":"ValidationRule","records":[{"attributes":{"type":"ValidationRule","url":"/services/data/v33.0/tooling/sobjects/ValidationRule/03d2800000083bXAAQ"},"Id":"03d2800000083bXAAQ","FullName":"SmartvCard__Note__c.SmartvCard__Not_new","CreatedDate":"2016-11-30T19:36:50.000+0000","TableEnumOrId":"01I280000022PApEAM","ValidationName":"Not_new","Metadata":{"description":null,"errorConditionFormula":"SmartvCard__Validation_Record__c = True","errorDisplayField":null,"errorMessage":"vCard Note record already exists. There can be only one vCard Note Record.","urls":null,"active":true}}]}
APEX:

JSONParser parser = JSON.createParser(response.getBody());
       List<String> fullName= new List<string>();
       List<string> Tot_size=new list<string>();
       List<string> metaData=new list<string>();
       while (parser.nextToken() != null) {
           if( (parser.getCurrentToken() == JSONToken.FIELD_NAME) && ( parser.getText() == 'FullName') ){
           parser.nextValue();
           fullName.add(parser.getText());
           
           }
           if( (parser.getCurrentToken() == JSONToken.FIELD_NAME) && ( parser.getText() == 'Size') ){
           parser.nextValue();
           Tot_size.add(parser.getText());
            system.debug('====================='+ parser.nextValue());
           }
            if( (parser.getCurrentToken() == JSONToken.FIELD_NAME) && ( parser.getText() == 'Metadata') ){
           parser.nextValue();
           metaData.add(parser.getText());
           system.debug(metaData);
           }
          
           
          }
        }

 i want to get values from "Metadata" object in the json string. I easy get "FullName", "Size" but when I am parsing Metadata , it only return "{" in list<string> MetaData. How can i get "active":values in this list.

Thanks