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 

Please initialize the field value for before insert 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=='DD' && 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 = 'DD-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); 
  }Else If(Adding.DSYDonationType__c=='Cash' && month==  Adding.createddate.month() ){ 
  if(String.ValueOf(i).length() == 3){
    /*Do Nothing*/
  }else if(String.ValueOf(i).length() == 2){
  st2 = 'Cash-0'+String.valueOf(i);
  }else if(String.ValueOf(i).length() == 1){
  system.debug('========ST2=========='+ST2); 
  st2 = 'Cash-00'+String.valueOf(i);
  system.debug('========ST=========='+ST2); 
  }
  Adding.Reset_number__c= st2;
  system.debug('========Reset Number=========='+Adding.Reset_number__c); 
  }Else if((month!=  Adding.createddate.month()) || (month==month1)  ){
  st2='Cash-000';
  Adding.Reset_number__c=st2;
  system.debug('========Cash-000=========='+Adding.Reset_number__c); 
  }
  system.debug('========DSYDonationType=========='+Adding.DSYDonationType__c);
  If(Adding.DSYDonationType__c=='Online' && month==  Adding.createddate.month() ){ 
  System.debug('====enter the loop=====');
  system.debug('========DSYDonationType=========='+Adding.DSYDonationType__c);
  if(String.ValueOf(i).length() == 3){
  /*Do Nothing*/
  }else if(String.ValueOf(i).length() == 2){
  st2 = 'Online-0'+String.valueOf(i);
  system.debug('========ST=========='+ST2);
  }else if(String.ValueOf(i).length() == 1){
  st2 = 'Online-00'+String.valueOf(i);
  system.debug('========ST=========='+ST2);
  }else if(String.ValueOf(i).length() == null){
  st2 = 'Online-000';
  }
  Adding.Reset_number__c= st2;
  system.debug('========ST=========='+ST2);
  } Else if((month!=  Adding.createddate.month()) || (month==month1)  ){
  st2='Online-000';
  Adding.Reset_number__c=st2;
  system.debug('========Cash-000=========='+Adding.Reset_number__c); 
  }
  If(Adding.DSYDonationType__c=='Cheque' && month==  Adding.createddate.month() ){ 
  System.debug('====enter the loop=====');
  system.debug('========DSYDonationType=========='+Adding.DSYDonationType__c);
  if(String.ValueOf(i).length() == 3){
  /*Do Nothing*/
  }else if(String.ValueOf(i).length() == 2){
  st2 = 'Cheque-0'+String.valueOf(i);
  system.debug('========ST=========='+ST2);
  }else if(String.ValueOf(i).length() == 1){
  st2 = 'Cheque-00'+String.valueOf(i);
  system.debug('========ST=========='+ST2);
  }else if(String.ValueOf(i).length() == null){
  st2 = 'Cheque-000';
  }
  Adding.Reset_number__c= st2;
  system.debug('========ST=========='+ST2);
  }  
  Else if((month!=  Adding.createddate.month()) || (month==month1)  ){
  st2='Cheque-000';
  Adding.Reset_number__c=st2;
  system.debug('========Cheque-000=========='+Adding.Reset_number__c); 
  }    
  If(Adding.DSYDonationType__c=='Card' && month==  Adding.createddate.month() ){
  system.debug('========DSYDonationType=========='+Adding.DSYDonationType__c);
  if(String.ValueOf(i).length() == 3){
    /*Do Nothing*/
  }else if(String.ValueOf(i).length() == 2){
  st2 = 'Card-0'+String.valueOf(i);
  system.debug('========ST=========='+ST2);
  }else if(String.ValueOf(i).length() == 1){
  st2 = 'Card-00'+String.valueOf(i);
  system.debug('========ST=========='+ST2);
  }else if(String.ValueOf(i).length() == null){
  st2 = 'Card-000';
  }
  Adding.Reset_number__c= st2;
  system.debug('========ST=========='+ST2);
  }   
  Else if((month!=  Adding.createddate.month()) || (month==month1)  )
  {
  st2='Card-000';
  Adding.Reset_number__c=st2;
  system.debug('========Card-000=========='+Adding.Reset_number__c); 
  }   
  If(Adding.DSYDonationType__c=='Direct transfer' && month==  Adding.createddate.month() ){
  if(String.ValueOf(i).length() == 3){
   /*Do Nothing*/
  }else if(String.ValueOf(i).length() == 2){
  st2 = 'DT-0'+String.valueOf(i);
  }else if(String.ValueOf(i).length() == 1){
  st2 = 'DT-00'+String.valueOf(i);
  }else if(String.ValueOf(i).length() == null){
  st2 = 'DT-000';
  }
  Adding.Reset_number__c= st2;
  }  
  Else if((month!=  Adding.createddate.month()) || (month==month1)  ){
  st2='DT-000';
  Adding.Reset_number__c=st2; 
  }    
   }
  Trigger.New[0].Reset_number__c=St2;
  }

 
Keyur  ModiKeyur Modi
Hi,
i observe your code where i found one problem is .. you are not initializing the value "i"  anywhere
 
integer i;
system.debug('==value of i=='+i);

if you see the system debug for this value .. it will return as null so you need to intialize integer i as i=0 at initial level.

please let me know if this will help,

Thanks,
Keyur Modi
 
manjunath vivekmanjunath vivek
Hi Keyur,

Thanks for replying me, Value I is coming from the field Reset number of the previous record, so that it can be incremented for the current record, so we don't need to initialize I, the  problem is when first record is created Reset number field is empty. from next record onwards it is getting incremented.My requirement is Reset number field should be increment right from the first record.

Thanks,
Manjunath
manjunath vivekmanjunath vivek
Simplified trigger
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;
  }