You need to sign in to do that
Don't have an account?
Help with Script
I want to write a one time script to populate a new field on old records.
Since this is a new field it will get populated going further, however i don't want the field blank for old records hence this ine time script.
can someone please correct my code?
//get all records that has value in startDate and endDate but no value in numberofdays
List<Custom_Object_c> sec = [SELECT Id,startDate,endDate,numberofdays from Custom_Object_c WHERE startDate !=null && endDate !=null && numberofdays==null)
//Loop through the list and update the numberofdays field
for (Custom_Object_c s: sec){
datetime t = sec.startDate;
date d = Date.newInstance(t.year(),t.month(),t.day());
datetime tt = sec.endDate;
date dd = Date.newInstance(t.year(),t.month(),t.day());
integer sec.numberofdays = startDate.daysBetween(endDate);
update sec;
}
}
you are getting this error because you are using list reference instead of SObject reference within for loop. Pleae refer my above comment. I have mentioned that.
Thanks
Chandra
All Answers
Why are you calculating this in a trigger? Why not use a formula field to calculate it - that way all records are always up to date - even the old ones.
Because I need the numberofdays which is business days only and that too in GMT.
Formula can't handle that.
Assuming your business days are weekdays, it looks like formulas can calculate the number of week days. Please see this and this.
I think i am on the right track with this but getting an error,
Initial term of field expression must be a concrete SObject: LIST<Custom_Object__c>
List<Custom_Object__c> sList = new List<Custom_Object__c>
List<Custom_Object__c> sec = [SELECT Id,startDate,endDate,numberofdays from Custom_Object__c WHERE startDate !=null AND endDate !=null AND numberofdays = null)
for(Custom_Object__c s: sec){
Date cd = sec.startDate__c.dateGmt(); // this is where the error is
Date ad = sec.endDate__c.dateGMT();
sec.numberofdays__c = BusinessDays.getDiffBusinessDays(ad, cd);
sList.add(sec);
}
update sList;
The biggest problem with your code is you have DML statement within a for loop. Put your dml statement outside loop.
Second one could be typing mistake. Your second date instance i believe should use tt dateTime. Also you need to use 's.startDate' , 's.endDate' , 's.numberofdays' instead of 'sec.startDate' ,'sec.endDate' ,'sec.numberofdays' respectively within loop.
I hope this helps.
Thanks
Chandra
Blog: Apex on Force.com - An Object Oriented Approach
you are getting this error because you are using list reference instead of SObject reference within for loop. Pleae refer my above comment. I have mentioned that.
Thanks
Chandra
Thanks for your reply. Yes both were typos DML inside for loop and t instead of tt.
I will try s.startdate
@cml, thanks that worked.
List<Custom_Object__c> sList = new List<Custom_Object__c>
List<Custom_Object__c> sec = [SELECT Id,startDate,endDate,numberofdays from Custom_Object__c WHERE startDate !=null AND endDate !=null AND numberofdays = null)
for(Custom_Object__c s: sec){
Date cd = s.startDate__c.dateGmt();
Date ad = s.endDate__c.dateGMT();
s.numberofdays__c = BusinessDays.getDiffBusinessDays(ad, cd);
sList.add(s);
}
update sList;
@colemab
Thanks for your response.
Formula will calculate the business days but as mentioned I want the days according to GMT that's the reason why i am doing it programatically.