You need to sign in to do that
Don't have an account?
Ajith 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;
}
}
}
}
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;
}
}
}
}
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:
another thing to consider is that you have this code repeated multiple times within each IF statement
Consider would the code be more efficient if it was restructured as:
HTH
regards
Andrew
and the code is
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