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
manjunath vivekmanjunath vivek 

Not getting value for first record using trigger

This is my requirement
Create auto number field in the object: npe03_Recurring_Donation. This field will have the property that it will reset every month. This field will be unique according to another field: DSYDonationType_c.
Meaning if DSYDonationType_c='cheque' , the auto number field will start from chq-1,chq-2,chq-3...so on...on 1 st of every month and reset on 1 st of next month. If DSYDonationType_c='Online', auto number will start from onl-1,onl-2,onl-3 ...and so on.
I am working on the below code
It is working fine but the when the first record is created, it will not fetch any value in the field Reset number, from next record onwards it will start incrementing, but I don't want the first record to be empty because of which I need to enter the value manually, can some one help me on this...
Below is my code
trigger Reset_restart_autonum on npe03__Recurring_Donation__c (before insert) {
  Public integer i;
  Public integer month1;
  List<npe03__Recurring_Donation__c> hold=New list<npe03__Recurring_Donation__c>(); 
  String st2;
  Public Date todaysDate = system.today();
  Integer month = todaysDate.month();
  //Integer month=9;
  month1=month+1;
  hold=[Select id,createddate,DSYDonationType__c,Reset_number__c from npe03__Recurring_Donation__c where DSYDonationType__c=:trigger.new[0].DSYDonationType__c order by CreatedDate DESC LIMIT 1];  
  for(npe03__Recurring_Donation__c Adding:Hold){  
  String st = Adding.Reset_number__c; 
  system.debug('========Reset=========='+Adding.Reset_number__c);   
  if(st== null){
  st = 'DD-000';
  }  
  system.debug('========ST=========='+ST);  
  i = Integer.valueOf(st.split('-')[1]);   
  i++;
  system.debug('========Increment=========='+i);
  If(Adding.DSYDonationType__c!=Null && month==  Adding.createddate.month() ){    
  if(String.ValueOf(i).length() == 3){
    /*Do Nothing*/
  }else if(String.ValueOf(i).length() == 2){
    st2 = 'DD-0'+String.valueOf(i);
    system.debug('========ST=========='+ST2);
  }else if(String.ValueOf(i).length() == 1){
    st2 =  Adding.DSYDonationType__c+'-00'+String.valueOf(i);
    system.debug('========ST=========='+ST2);
  } 
 
  Adding.Reset_number__c= st2;
  system.debug('========ST=========='+ST2);
  
  }Else if((month!=  Adding.createddate.month()) || (month==month1)  ){
     st2='DD-000';
     Adding.Reset_number__c=st2;
     system.debug('========Cash-000=========='+Adding.Reset_number__c); 
  }
   }
  Trigger.New[0].Reset_number__c=St2;
  }
Best Answer chosen by manjunath vivek
Shailendra Singh ParmarShailendra Singh Parmar
trigger Reset_restart_autonum on npe03__Recurring_Donation__c(before insert) {
    Public integer i;
    Public integer month1;
    List < npe03__Recurring_Donation__c > hold = New list < npe03__Recurring_Donation__c > ();
    String st2;
    Public Date todaysDate = system.today();
    Integer month = todaysDate.month();
    //Integer month=9;
    month1 = month + 1;
    hold = [Select id, createddate, DSYDonationType__c, Reset_number__c from npe03__Recurring_Donation__c where DSYDonationType__c = : trigger.new[0].DSYDonationType__c order by CreatedDate DESC LIMIT 1];
    //npe03__Recurring_Donation__c firstrecord;
    if (hold.isempty()) {
        Trigger.new[0].reset_number__C = 'DD-000';
        
    } else {
        for (npe03__Recurring_Donation__c Adding: Hold) {
            String st = Adding.Reset_number__c;
            if (st == null || String.isBlank(st)) {
                st = 'DD-000';
            }
            i = Integer.valueOf(st.split('-')[1]);
            i++;
            If(Adding.DSYDonationType__c != Null && month == Adding.createddate.month()) {
                if (String.ValueOf(i).length() == 3) {
                    //   Do Nothing
                } else if (String.ValueOf(i).length() == 2) {
                    st2 = 'DD-0' + String.valueOf(i);
                    system.debug('========ST==========' + ST2);
                } else if (String.ValueOf(i).length() == 1) {
                    st2 = Adding.DSYDonationType__c + '-00' + String.valueOf(i);
                    system.debug('========ST==========' + ST2);
                }
                Adding.Reset_number__c = st2;
                system.debug('========ST==========' + ST2);
            }
            Else
            if ((month != Adding.createddate.month()) || (month == month1)) {
                st2 = 'DD-000';
                Adding.Reset_number__c = st2;
                system.debug('========Cash-000==========' + Adding.Reset_number__c);
            }
        }
		Trigger.New[0].Reset_number__c = St2;
    }
    
}

try this one.. it should work. 
I find 2 problem.

#1. You already corrected by using  Trigger.new[0].reset_number__C='DD-000';
#2. In last statement you were overriding value with st2 even when list is empty.

Thanks!

All Answers

Shailendra Singh ParmarShailendra Singh Parmar
Hi 
Not sure why you are not putting condition if hold list is empty then assume that their is no records exists and create record based on type and that is first record.
let me know if that make sense.

Thanks!
manjunath vivekmanjunath vivek
Hi shailender,

Thanks for replying me, I tried this
for(npe03__Recurring_Donation__c Adding:Hold){  
  String st = Adding.Reset_number__c; 
  system.debug('========Reset=========='+Adding.Reset_number__c);
  if(hold.isempty()){
    Adding.Reset_number__c='DD-000';
  }
But
Reset_number__c field is empty.

 
Shailendra Singh ParmarShailendra Singh Parmar
You are checking this inside loop, because Hold is empty that's why you will not enter in loop. Try something lik e
if(hold.isempty()){
  Adding.Reset_number__c='DD-000';
} else {
for(npe03__Recurring_Donation__c Adding:Hold){  
  String st = Adding.Reset_number__c; 
  system.debug('========Reset=========='+Adding.Reset_number__c);

}
manjunath vivekmanjunath vivek
Hi shailendar,

Thanks for your idea, I have tried based on your idea, still no go,
The code I used is
hold=[Select id,createddate,DSYDonationType__c,Reset_number__c from npe03__Recurring_Donation__c where DSYDonationType__c=:trigger.new[0].DSYDonationType__c order by CreatedDate DESC LIMIT 1];  
  npe03__Recurring_Donation__c firstrecord;
  if(hold.isempty()){
   firstrecord.Reset_number__c='DD-000';
   } else {
  
  for(npe03__Recurring_Donation__c Adding:Hold){


I am getting following error

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Reset_restart_autonum caused an unexpected exception, contact your administrator: Reset_restart_autonum: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Reset_restart_autonum: line 13, column 1
 * = Required Information
 
Shailendra Singh ParmarShailendra Singh Parmar
Instance of firstrecord variable is null...initialize it by creating instance of object and you will be fine.
manjunath vivekmanjunath vivek
Hi Shailendra,

Sorry to trouble you again and again
I tried the code below
if(hold.isempty()){
  npe03__Recurring_Donation__c firstrecord=New npe03__Recurring_Donation__c();
  firstrecord.Reset_number__c='DD-000';
   } else {
  
  for(npe03__Recurring_Donation__c Adding:Hold){  
  String st = Adding.Reset_number__c;

still
Reset_number__c i s blank
 
Shailendra Singh ParmarShailendra Singh Parmar
Hi Vivek,
Not sure what issue it could be,  try to put some dubug to check if you are going into section when list is empty and you are adding firstrecord in list to insert.
If still you face issue, please paste complete code, I may got issue.

Thanks!
manjunath vivekmanjunath vivek
Hi shailendra,
I tried debuggin the code,when I debug the code , I get proper value , but that value is not getting displayed in the field.
the debug log is as follows
15:13:57.081 (81146544)|SYSTEM_METHOD_EXIT|[12]|List<npe03__Recurring_Donation__c>.isEmpty()
15:13:57.081 (81458932)|SYSTEM_METHOD_ENTRY|[15]|System.debug(ANY)
15:13:57.081 (81481713)|USER_DEBUG|[15]|DEBUG|=======First reccorde====DD-000
15:13:57.081 (81489540)|SYSTEM_METHOD_EXIT|[15]|System.debug(ANY)
Code is as follows
if(hold.isempty()){
  npe03__Recurring_Donation__c firstrecord=New npe03__Recurring_Donation__c();
  firstrecord.Reset_number__c='DD-000';
  System.debug('=======First reccorde===='+firstrecord.Reset_number__c);
   }
The complete code is as follows
trigger Reset_restart_autonum on npe03__Recurring_Donation__c (before insert) {
  Public integer i;
  Public integer month1;
  List<npe03__Recurring_Donation__c> hold=New list<npe03__Recurring_Donation__c>(); 
  String st2;
  Public Date todaysDate = system.today();
  Integer month = todaysDate.month();
  //Integer month=9;
  month1=month+1;
  hold=[Select id,createddate,DSYDonationType__c,Reset_number__c from npe03__Recurring_Donation__c where DSYDonationType__c=:trigger.new[0].DSYDonationType__c order by CreatedDate DESC LIMIT 1];  
  //npe03__Recurring_Donation__c firstrecord;
  if(hold.isempty()){
  npe03__Recurring_Donation__c firstrecord=New npe03__Recurring_Donation__c();
  firstrecord.Reset_number__c='DD-000';
  System.debug('=======First reccorde===='+firstrecord.Reset_number__c);
   } else {
  for(npe03__Recurring_Donation__c Adding:Hold){  
  String st = Adding.Reset_number__c;   
  if(st== null || String.isBlank(st)){
  st = 'DD-000';
  }  
  i = Integer.valueOf(st.split('-')[1]);   
  i++;
  If(Adding.DSYDonationType__c!=Null && month==  Adding.createddate.month() ){    
  if(String.ValueOf(i).length() == 3){
 //   Do Nothing
  }else if(String.ValueOf(i).length() == 2){
    st2 = 'DD-0'+String.valueOf(i);
    system.debug('========ST=========='+ST2);
  }else if(String.ValueOf(i).length() == 1){
    st2 =  Adding.DSYDonationType__c+'-00'+String.valueOf(i);
    system.debug('========ST=========='+ST2);
  }  
  Adding.Reset_number__c= st2;
  system.debug('========ST=========='+ST2);  
  }Else if((month!=  Adding.createddate.month()) || (month==month1)  ){
     st2='DD-000';
     Adding.Reset_number__c=st2;
     system.debug('========Cash-000=========='+Adding.Reset_number__c); 
  }
   }  
   }
  Trigger.New[0].Reset_number__c=St2;
  }

I tried in different ways
 I tried
if(hold.isempty()){
  Trigger.new[0].reset_number__C='DD-000';
   }

I tried
String st=Trigger.new[0].reset_number__c;
if(String.isBlank(st)){
  st = 'DD-000';
  }
But when I put before update operation,trigger works fine, right from the first record it starts populating values for reset number,
when we change it to before insert, problem arises, I feel it is not treating first record as insert , it is treating first record as update( I am not sure).



 
Shailendra Singh ParmarShailendra Singh Parmar
trigger Reset_restart_autonum on npe03__Recurring_Donation__c(before insert) {
    Public integer i;
    Public integer month1;
    List < npe03__Recurring_Donation__c > hold = New list < npe03__Recurring_Donation__c > ();
    String st2;
    Public Date todaysDate = system.today();
    Integer month = todaysDate.month();
    //Integer month=9;
    month1 = month + 1;
    hold = [Select id, createddate, DSYDonationType__c, Reset_number__c from npe03__Recurring_Donation__c where DSYDonationType__c = : trigger.new[0].DSYDonationType__c order by CreatedDate DESC LIMIT 1];
    //npe03__Recurring_Donation__c firstrecord;
    if (hold.isempty()) {
        Trigger.new[0].reset_number__C = 'DD-000';
        
    } else {
        for (npe03__Recurring_Donation__c Adding: Hold) {
            String st = Adding.Reset_number__c;
            if (st == null || String.isBlank(st)) {
                st = 'DD-000';
            }
            i = Integer.valueOf(st.split('-')[1]);
            i++;
            If(Adding.DSYDonationType__c != Null && month == Adding.createddate.month()) {
                if (String.ValueOf(i).length() == 3) {
                    //   Do Nothing
                } else if (String.ValueOf(i).length() == 2) {
                    st2 = 'DD-0' + String.valueOf(i);
                    system.debug('========ST==========' + ST2);
                } else if (String.ValueOf(i).length() == 1) {
                    st2 = Adding.DSYDonationType__c + '-00' + String.valueOf(i);
                    system.debug('========ST==========' + ST2);
                }
                Adding.Reset_number__c = st2;
                system.debug('========ST==========' + ST2);
            }
            Else
            if ((month != Adding.createddate.month()) || (month == month1)) {
                st2 = 'DD-000';
                Adding.Reset_number__c = st2;
                system.debug('========Cash-000==========' + Adding.Reset_number__c);
            }
        }
		Trigger.New[0].Reset_number__c = St2;
    }
    
}

try this one.. it should work. 
I find 2 problem.

#1. You already corrected by using  Trigger.new[0].reset_number__C='DD-000';
#2. In last statement you were overriding value with st2 even when list is empty.

Thanks!
This was selected as the best answer
manjunath vivekmanjunath vivek
Hi Shailendra,

I appreciate your help and time, I can't forget your help, my issue got resolved.
My final code is as below.
 
trigger Reset_restart_autonum on npe03__Recurring_Donation__c (before insert) {
  Public integer i;
  Public integer month1;
  List<npe03__Recurring_Donation__c> hold=New list<npe03__Recurring_Donation__c>(); 
  String st2;
  Public Date todaysDate = system.today();
  Integer month = todaysDate.month();
  //Integer month=9;
  month1=month+1;
  hold=[Select id,createddate,DSYDonationType__c,Reset_number__c from npe03__Recurring_Donation__c where DSYDonationType__c=:trigger.new[0].DSYDonationType__c order by CreatedDate DESC LIMIT 1];  
 System.debug('=======hold===='+hold);
  for(npe03__Recurring_Donation__c Adding:Hold){  
  String st = Adding.Reset_number__c;   
  if(st== null || String.isBlank(st)){
  st = 'DD-000';
  }  
  i = Integer.valueOf(st.split('-')[1]);   
  i++;
  If(Adding.DSYDonationType__c!=Null && month==  Adding.createddate.month() ){    
  if(String.ValueOf(i).length() == 3){
 //   Do Nothing
  }else if(String.ValueOf(i).length() == 2){
    st2 = Adding.DSYDonationType__c+'-00'+String.valueOf(i);
    system.debug('========ST=========='+ST2);
  }else if(String.ValueOf(i).length() == 1){
    st2 =  Adding.DSYDonationType__c+'-00'+String.valueOf(i);
    system.debug('========ST=========='+ST2);
  }  
  Adding.Reset_number__c= st2;
  system.debug('========ST=========='+ST2);  
  }Else if((month!=  Adding.createddate.month()) || (month==month1)  ){
     st2=Adding.DSYDonationType__c+'-00'+String.valueOf(i);
     Adding.Reset_number__c=st2;
  }
   }  
  Trigger.New[0].Reset_number__c=St2;
  if(hold.isempty()){
  Trigger.new[0].reset_number__C=Trigger.new[0].DSYDonationType__c+'-001';    
   }
  }

Thanks,
Manjunath.