2

I've created a custom field of type DateTime in Task object.

I try to add to my custom field an object from type DateTime.

But I receive the following error message:

A value cannot be stored to my_custom_field in type Task

Here is the code:

Datetime now = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(now);
Datetime localTime = now.addSeconds(offset/1000);
DateTime realEndTime = localTime;
task.Scheduled_Timeframe__c = realEndTime;

Do you know how to solve and avoid this error?

  • Please share your actual code... Also did you add any validation rules? – Adrian Larson Aug 9 '17 at 20:54
  • I have edited and updated my question with the actual code the way you requested. – sfdev Aug 9 '17 at 21:00
  • 1
    You need to set the field on a record, not the object definition itself. Are you trying to update one or create a new one? – Adrian Larson Aug 9 '17 at 21:03
2

Create an instance of Task, make sure variable should not be reserved word.

It will like this:

Task tsk = new Task();

Datetime now = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(now);
Datetime localTime = now.addSeconds(offset/1000);
DateTime realEndTime = localTime;
tsk.Scheduled_Timeframe__c = realEndTime;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.