You need to sign in to do that
Don't have an account?
Anna Miller
Why is datetime.now() added an random datetime and UserInfo.getUserId() returning random user on mass update?
I have the following code in a class below:
User u = [SELECT RepName__c FROM User WHERE Id = :UserInfo.getUserId()];
List<String> zipcodes = new List<String>();
//create map to track updated zips within execution context
Map<Id,Zip_Code_Availability__c> zipToUpdate = new Map<Id,Zip_Code_Availability__c>();
for(Zip_Code_Availability__c z :newlist) {
if(newmap.get(z.id).Lock_Zip__c == 'Lock' && oldmap.get(z.id).Lock_Zip__c !='Lock' && !zipToUpdate.containskey(z.Id)) {
z.CalloutPending__c = true;
z.Lock_Zip_Requested_By__c = u.RepName__c;
z.Lock_Zip_Date_Time__c = datetime.now();
zipToUpdate.put(z.Id,z);
} else {
[...]
}
}
For some reason when I do a mass update with dataloader (I've tested with 1000+ records), datetime.now() populates with a random datetime sometimes in the past sometimes in the future, but different for all records. Also, the User returned with the soql query in the top row is not myself even though I'm logged into DataLoader.
Any ideas as to why it would do this? Let me know if you need me to post the whole code for context. Thanks for any help you can offer.
User u = [SELECT RepName__c FROM User WHERE Id = :UserInfo.getUserId()];
List<String> zipcodes = new List<String>();
//create map to track updated zips within execution context
Map<Id,Zip_Code_Availability__c> zipToUpdate = new Map<Id,Zip_Code_Availability__c>();
for(Zip_Code_Availability__c z :newlist) {
if(newmap.get(z.id).Lock_Zip__c == 'Lock' && oldmap.get(z.id).Lock_Zip__c !='Lock' && !zipToUpdate.containskey(z.Id)) {
z.CalloutPending__c = true;
z.Lock_Zip_Requested_By__c = u.RepName__c;
z.Lock_Zip_Date_Time__c = datetime.now();
zipToUpdate.put(z.Id,z);
} else {
[...]
}
}
For some reason when I do a mass update with dataloader (I've tested with 1000+ records), datetime.now() populates with a random datetime sometimes in the past sometimes in the future, but different for all records. Also, the User returned with the soql query in the top row is not myself even though I'm logged into DataLoader.
Any ideas as to why it would do this? Let me know if you need me to post the whole code for context. Thanks for any help you can offer.
Datetime.Now() returns in GMT timezone. You can use format method.
Datetime currDate = System.now();
String formatedDate = currDate.format('EEEE, MMMM d, yyyy,hh,mm');
system.debug('---formatedDate---'+formatedDate);
/Raghu
Datetime.now() is returning the local timezone for me. This is just occurring when I tried a mass update that called this method. Also, the datetimes that are being returned are all over the place. For example, 4/1/14, 6/24/14, 8/8/14, 5/1/15, 6/4/15
Other things you can try:
- Use Workbench to do your data load instead
- Try inserting a few records manually (with the same fields populated) to see what happens
- Make sure the data you're inserting actually goes through the IF statement you have in your code
- Use System.debug() to see what is actually going on in your code