0

I am trying to update the value of a field inside of a list to be returned to the LWC component. I don't want the update to happen on the database, but just on the UI.

As datetime values are stored in GMT, I am converting it to the users local timezone value just to be displayed in the UI.

With the below code I am getting this error - Field is not writeable - points to the CreatedDate field.

public with sharing class theController {
    @AuraEnabled
    public static List<Task> getAllRecords(String contactRecordId) {  
        TimeZone tz = UserInfo.getTimeZone();
        DateTime yesterday = System.now() - 1;
        List<Task> allTasks = [SELECT ID, CreatedDate FROM Task Where contact = :contactRecordId AND CreatedDate >= :yesterday];
        for(Task theTask: allTasks) {
            Datetime createdDate = theTask.CreatedDate.AddSeconds(tz.getOffset(theTask.CreatedDate)/1000);
            theTask.CreatedDate = createdDate;
        }
        return allTasks;
    }
}

Please advise. Thank you all.

UPDATE as per identigral reference to the post with same issue

TimeZone tz = UserInfo.getTimeZone();
    for(Task theTask: allTasks) {
                Datetime createdDate = theTask.CreatedDate.format('yyyy-MM-dd hh:mm', tz);
                theTask.CreatedDate = createdDate;
            }

What could be wrong above , I get an error saying method does not exist or incorrect signature for the format method.

7

1 Answer 1

1

You could create a WrapperClass and create properties for the changes you want to make just for the UI. Something like

public class TaskWrapper{
    @AuraEnabled
    public Task task;
    @AuraEnabled
    public String createdDate;
}

and your method can be updated to use this wrapper class

public static List<TaskWrapper> getAllRecords(String contactRecordId) {  
    TimeZone tz = UserInfo.getTimeZone();
    DateTime yesterday = System.now() - 1;

    List<TaskWrapper> tasks = new List<TaskWrapper>();
    
    List<Task> allTasks = [SELECT ID, CreatedDate FROM Task Where contact = :contactRecordId AND CreatedDate >= :yesterday];
    for(Task theTask: allTasks) {
        TaskWrapper tw = new TaskWrapper();
        tw.task = theTask;
        tw.createdDate = theTask.CreatedDate.format('yyyy-MM-dd hh:mm', tz);
        tasks.add(tw);
    }
    return tasks;
}
1
  • Thanks Aditya, this was exactly what I was looking for.
    – sumchans
    Commented Aug 6, 2021 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .