You need to sign in to do that
Don't have an account?
manjunath 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
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; }
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
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!
Thanks for replying me, I tried this But
Reset_number__c field is empty.
Thanks for your idea, I have tried based on your idea, still no go,
The code I used is
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
Sorry to trouble you again and again
I tried the code below
still
Reset_number__c i s blank
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!
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 The complete code is as follows
I tried in different ways
I tried
I tried 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).
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!
I appreciate your help and time, I can't forget your help, my issue got resolved.
My final code is as below.
Thanks,
Manjunath.