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
Ajith MDAjith MD 

System.null.exception

Im getting null.exception while save record on Sales__c. And the child record is also not created on Recurring__c. Can anyone help me to resolve this. 
trigger Grossamount on Sales__c ( before insert, before update, after insert) {
    if( Trigger.isUpdate || Trigger.isInsert) {
        for(Sales__c t: Trigger.new){
            if(t.Type__c == 'One Time' ){
                t.Gross_Amount__c = t.Amount__c;
            } 
            else if(t.Type__c == 'Recurring' ){
                t.Amount_Recurring__c = t.Amount__c * t.Term__c;
                t.Gross_Amount__c = t.Amount_Recurring__c;
                Date new1= t.Sale_Date__c;
                if(t.Frequency__c== 'Weekly'){
                    for(integer i =0; i < t.Term__c; i++){  
                        Recurring__c child = new Recurring__c();
                        child.Sales__c = t.Id;
                        System.debug(child.Sales__c);
                        child.Name = t.Name;
                        child.Amount__c= t.Amount__c;
                        child.Due_Date__c = new1;
                        Date new2 = new1.addDays(7);
                        new1 = new2;    
                        System.debug('Hi');
                        insert child; 
                    } 
                }
                else if(t.Frequency__c == 'Monthly'){
                    for(integer i =0; i < t.Term__c; i++){  
                        Recurring__c child = new Recurring__c();
                        child.Sales__c = t.Id;
                        System.debug(child.Sales__c);
                        child.Name = t.Name;
                        child.Amount__c= t.Amount__c;
                        child.Due_Date__c = new1;
                        Date new2 = new1.addMonths(1);
                        new1 = new2;    
                        System.debug('Hi');
                        insert child; 
                    } 
                }
                else if(t.Frequency__c == 'Quarterly'){
                    for(integer i =0; i < t.Term__c; i++){  
                        Recurring__c child = new Recurring__c();
                        child.Sales__c = t.Id;
                        System.debug(child.Sales__c);
                        child.Name = t.Name;
                        child.Amount__c= t.Amount__c;
                        child.Due_Date__c = new1;
                        Date new2 = new1.addMonths(4);
                        new1 = new2;    
                        System.debug('Hi');
                        insert child; 
                    } 
                }
                else {
                    for(integer i =0; i < t.Term__c; i++){  
                        Recurring__c child = new Recurring__c();
                        child.Sales__c = t.Id;
                        System.debug(child.Sales__c);
                        child.Name = t.Name;
                        child.Amount__c= t.Amount__c;
                        child.Due_Date__c = new1;
                        Date new2 = new1.addMonths(12);
                        new1 = new2;    
                        System.debug('Hi');
                        insert child; 
                    } 
                }
            }
            
            else {
                t.Amount_Service__c = t.Amount__c;
                t.Gross_Amount__c = t.Amount_Service__c;
            }
        }
        
    } 
}
Andrew GAndrew G
your issue is that you are doing after insert and using Trigger.new

if we check this reference
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

and check the reference for new

Returns a list of the new versions of the sObject records.
This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers.


note also, if we consider your logic, if this code is invoked before insert and after insert (successfully) then you would be double inserting your child records.  And if you invoke the code as is, on update would also generate additional child records. so if i inserted and then made a change to the Sales__c record with your existing code (if it ran), then you would have 3 times the number of child records than is required.



Also, your code would not survive a bulk insert as you a doing individual inserts of child records within eact loop.  Max DMLs limit is 150 inserts per operation.

better practice would be to use a list .  Add the child to the list and then do a single insert outside the Loop.
brief example:
trigger Grossamount on Sales__c ( before insert, before update, after insert) {
    if( Trigger.isUpdate || Trigger.isInsert) {
        List<Recurring__c> childListToUpdate = new List<Recurring__c>();
        for(Sales__c t: Trigger.new){
            //do stuff
            childListToUpdate.add(child);
        } //end loop
        insert childListToUpdate;
  }
}

another thing to consider is that you have this code repeated multiple times within each IF statement
Recurring__c child = new Recurring__c();
                        child.Sales__c = t.Id;
                        System.debug(child.Sales__c);
                        child.Name = t.Name;
                        child.Amount__c= t.Amount__c;
                        child.Due_Date__c = new1;

Consider would the code be more efficient if it was restructured as:
 
Recurring__c child = new Recurring__c();
                        child.Sales__c = t.Id;
                        System.debug(child.Sales__c);
                        child.Name = t.Name;
                        child.Amount__c= t.Amount__c;
                        child.Due_Date__c = new1;
    if(t.Frequency__c = "weekly") {
        //do weekly date stuff
    else if(t.Frequency__c == 'Monthly'){
        // do monthly date stuff

// and so on

HTH

regards
Andrew
 
Ajith MDAjith MD
Im trying like whatever you said but right now also Im getting error like System.FinalException: Record is read-only Trigger.Grossamount
and the code is
trigger Grossamount1 on Sales__c ( after insert, after update) {
    if (trigger.isAfter){
        if( Trigger.isUpdate || Trigger.isInsert) {
            List<Recurring__c> childListToUpdate = new List<Recurring__c>();
            
            for(Sales__c t: Trigger.new){
                if(t.Type__c == 'One Time' ){
                    t.Gross_Amount__c = t.Amount__c;
                }  
                else if(t.Type__c == 'Recurring' ){
                    Date new1= t.Sale_Date__c;
                    t.Amount_Recurring__c = t.Amount__c * t.Term__c;
                    Recurring__c child = new Recurring__c();
                    child.Sales__c = t.Id;
                    System.debug(child.Sales__c);
                    child.Name = t.Name;
                    child.Amount__c= t.Amount__c;
                    child.Due_Date__c = new1;
                    if(t.Frequency__c == 'Quarterly'){
                        for(integer i =0; i < t.Term__c; i++){  
                            Date new2 = new1.addMonths(4);
                            new1 = new2;	
                            System.debug('Hi');
                            childListToUpdate.add(child);
                        } 
                    }
                    else if(t.Frequency__c== 'Weekly'){
                        for(integer i =0; i < t.Term__c; i++){  
                            
                            Date new2 = new1.addDays(7);
                            new1 = new2;	  
                            childListToUpdate.add(child);
                        } 
                    }
                    else if(t.Frequency__c== 'Monthly'){
                        for(integer i =0; i < t.Term__c; i++){  
                            Date new2 = new1.addMonths(1);
                            new1 = new2;	
                            System.debug('Hi');
                            childListToUpdate.add(child);
                        } 
                    }
                    else {
                        for(integer i =0; i < t.Term__c; i++){  
                            Date new2 = new1.addMonths(12);
                            new1 = new2;	
                            System.debug('Hi');
                            childListToUpdate.add(child);
                        } 
                    }
                }
                else {
                    t.Amount_Service__c = t.Amount__c;
                    t.Gross_Amount__c = t.Amount_Service__c;
                }    
            }
            insert childListToUpdate;
        }
    }
}

 
Andrew GAndrew G
I would then check your permissions for the Sales__c object and the Recurring__c object.
If access to the objects looks correct, check the FLS and then check the other triggers and process builders that may be involved.  For example, there may be a trigger or process builder on the Recurring object.  

If all that looks ok, then i would increase the debugs in the code and run the code with logging enabled and review the logs.

Regards
Andrew
Andrew GAndrew G
I also just noticed that you have renamed your trigger.  Did you disable the other trigger? the original Grossamount trigger, as i notice it is used in the error message and your revised trigger is Grossamount1