1

I'm trying to create a Task via Apex, and I'm trying to set the Type to "Call":

Task task = new Task();
task.Type = 'Call';
task.Subject = 'Paid signup';
task.WhatId = account.Id;
task.Status = 'New';
task.ActivityDate = Date.Today().AddDays(7);
task.OwnerId = account.OwnerId;
task.Description = 'Contact: ' + contact.Name + '\r\n' + 'Contract: ' + contract.ContractNumber;
insert task;

And I'm getting the following error on the second line where I assign "Call":

Line: 2, Column: 6

Variable does not exist: Type

I'm basing my code off this example: https://www.brcline.com/blog/creating-tasks-salesforce-apex

Type definitely exists:

enter image description here

How do I fix this code so that it works?

4
  • Check if the field has field level permission enabled for your user profile?
    – Raul
    Commented Jul 30, 2020 at 7:41
  • @Raul I'm using the administrator account. Commented Jul 30, 2020 at 7:41
  • @Raul You were right. It wasn't visible. Commented Jul 30, 2020 at 7:42
  • 1
    BTW, there is a more compact way of doing this: Task task = new Task(Type = 'Call', Subject = 'Paid signup', ...);
    – Phil W
    Commented Jul 30, 2020 at 8:57

1 Answer 1

5

Usually this problem happens when the FLS is not enabled for the user's profile.

First thing to check in such cases is to create the record from Salesforce UI to verify if the field is visible.

1
  • FLS = Field Level Security
    – Bahman.A
    Commented Sep 30, 2021 at 21:18

You must log in to answer this question.

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