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
Anna MillerAnna 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. 
Raghu Reddy 14Raghu Reddy 14
Hi Anna,

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

 










Anna MillerAnna Miller
Hi 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
David "w00t!" LiuDavid "w00t!" Liu
Very strange - the only thing I can think of is you might have other triggers or workflows toying the same data.

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