0

I created a queue and set the queue to handle the task Object:

enter image description here

I can assign a task to a queue and insert the task if I do it via the "Anonymous apex"; however, if I run the same code via a batch then it throws the following error message:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Assigned To ID: id value of incorrect type: 00G1l000002utScEAI: [OwnerId]

My code to insert the task using fflib:

fflib_ISObjectUnitOfWork uow = Application.uow.newInstance();

    Group queue = [SELECT Id, Name FROM Group WHERE Type = 'Queue' AND Name = 'my Queue' LIMIT 1];
    
    Task task = new Task(
            Subject = 'Test task',
            Priority = 'Hoog',
            Status = 'Niet gestart',
            WhoId = '0038E00001C80eEQAR',
            OwnerId = queue.Id,
            Description = 'Do something'
    );
    
    uow.registerNew(task);
    
    uow.commitWork();

I read that this issue is due to the API version of my classes so I should use the highest API version, sorry I could not find back this source, I did update all the involved classes to the latest version as I speak is 49.0 but the issue is still not solved. Do you have any hint to tackle this issue?

2 Answers 2

1

The error message states field integrity exception for invalid data type passed to ID field. In your code snippet, I can see that you are passing string to ID data type and not explicitly converting it. You need to convert the queueId (& contactId) to ID type before passing the value to the new Task SObject.

Id queueId = Id.valueof('00G1l000002utScEAI');

or

query and get the ID reference to the queue.

I tried this in my dev org and it executed batch apex with out any exceptions creating the task record. Note that it works fine while executing anonymously without having to convert explicitly to ID type. I suspect the anonymous execution window is handling this internally and hence, you do not see the problem there.

5
  • Hi I am querying and getting the Id, I updated my question, I just wanted to make my example easier. I tried also with Id.valueof method but same issue. Commented Aug 18, 2020 at 15:07
  • This is strange. Now my code works even without explicitly casting to ID type (as well as your updated code sample). Just to be doubly sure, I tried this on a totally different dev org and it worked fine there too. I wonder if there is any org level difference that is causing this issue.
    – arut
    Commented Aug 18, 2020 at 15:37
  • Hi @arut, I updated my question, I am using a library called fflib to persist the task. I just figure out the issue, thanks for your answer. Commented Aug 18, 2020 at 15:38
  • @Cubancoffee What was causing the problem?
    – arut
    Commented Aug 18, 2020 at 15:39
  • I will post the answer in a sec. Commented Aug 18, 2020 at 15:41
1

I changed the Api version of the fflib class fflib_SObjectUnitOfWork from 47.0 to 48.0, now I can persist tasks that are assigned to a queue in a batch context.

You must log in to answer this question.

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