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
EvertonSzekeresEvertonSzekeres 

Date to string one day less

Hi,

I converted my date field in string, so I can updated my other field in account.

    for (Account acc: [SELECT Id, Data_pedido_AR__c, Data_pedido_SA__c FROM Account WHERE id IN: ids]){

    for (Case c: trigger.new) {
   
    if(c.motivo__c == 'Formandos AR' && c.Data_pedido_AR__c != null && Trigger.oldMap.get(c.id).Data_pedido_AR__c != c.Data_pedido_AR__c){
    Datetime d = Date.Valueof(c.Data_pedido_AR__c);   
    String dateStr = d.format('dd/MM/yyyy');
    System.debug('::::::::::: ' + dateStr) ;

        acc.Data_pedido_AR__c = dateStr;
    }
update acc;


It works fine but in my account my field is one day less.
If I put c.Data_pedido_AR__c = 20/05/2014
I get acc.Data_pedido_AR__c = 19/05/2014

Why?
Best Answer chosen by EvertonSzekeres
Edwin VijayEdwin Vijay
As pointed out already the problem is because of TimeZone Conversion.

To avoid this use formatGmt(String) instead of format()

Datetime d = Date.Valueof(c.Data_pedido_AR__c);   
String dateStr = d.formatGMT('dd/MM/yyyy');

Forcetree.com - Code Samples and Tips (http://www.forcetree.com/" target="_blank)

All Answers

Eli Flores, SFDC DevEli Flores, SFDC Dev
I'm not 100% sure but format converts it into the timezone of  the user running the code. The valueOf return the time in the timezone of the company overall (iirc). 


EvertonSzekeresEvertonSzekeres
What should I do to bring me the same date?

Eli Flores, SFDC DevEli Flores, SFDC Dev
Can you directly assign it:
acc.Data_pedido_AR__c = c.Data_pedido_AR__c;
?

If not,  what type is CASE.Data_pedido_AR__c? It looks like ACCOUNT.Data_pedido_AR__c is supposed to be a string. Can you change the type fo Account.Data_pedido_AR__C to match the type on the case?
Edwin VijayEdwin Vijay
As pointed out already the problem is because of TimeZone Conversion.

To avoid this use formatGmt(String) instead of format()

Datetime d = Date.Valueof(c.Data_pedido_AR__c);   
String dateStr = d.formatGMT('dd/MM/yyyy');

Forcetree.com - Code Samples and Tips (http://www.forcetree.com/" target="_blank)
This was selected as the best answer