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
Manu ErwinManu Erwin 

"INVALID_OPERATION operation is not allowed" while setting EmailMessage.Status with format() in loop

Anyone know why setting Case.Status inside a loop using format() works, while attempting to set EmailMessage.Status inside a loop using format() fails?

 

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OPERATION, operation is not allowed: []

 

public class testingForLoopProblem {
    public testingForLoopProblem() {}

    @isTest static void Case_ForLoop_Success_Test() {
        Account acc01 = new Account(Name = 'success');
        insert acc01;

        Integer NUM_RECORDS = 6;
        List<Case> cases = new List<Case>();
        for (Integer counter = 0; counter < NUM_RECORDS; counter++){
            Case caseTemp = new Case();
            caseTemp.AccountId = acc01.Id;
            caseTemp.Subject = 'Test: : ' + counter;

            // Why does this work?
            caseTemp.Status = counter.format();

            cases.add(caseTemp);
        }
        insert cases;
    }

    @isTest static void EmailMessage_ForLoop_Fail_Test() {
        Account acc01 = new Account(Name = 'fail');
        insert acc01;
        Case case01 = new Case(AccountId = acc01.Id);
        insert case01;
        Integer NUM_RECORDS = 6;
        List<EmailMessage> emails = new List<EmailMessage>();
        for (Integer counter = 0; counter < NUM_RECORDS; counter++){
            EmailMessage email = new EmailMessage();
            email.ParentId = case01.Id;

            // Why does this NOT work?
            email.Status = counter.format();

            emails.add(email);
        }
        insert emails;
    }
}

 API Version 27.

Best Answer chosen by Admin (Salesforce Developers) 
Manu ErwinManu Erwin

Update: after further testing I've found the problem to be when attempting to save with a Status value of '5' (Draft).

 

All the other values can be set (0 through 4) and saved successfully.

 

Also queried a bunch of records and cannot find any '5' values so am chalking this up to being a temporary picklist value that is never actually saved.

All Answers

empucempuc

Status field on a Case is a regular editable picklist, while Status on EmailMessage is read only.

vishal@forcevishal@force

Status

 

Type             -  picklist

Properties   -  CreateFilterRestricted picklist

Description -  Read only. The status of the email.

 

For example, New, Draft, Unread, Replied, “Sent.”

 

Yes, he's right. As per the documentation (refer above), Status field for EmailMessage object is a Read-Only field, not only "counter.format()" but anything else will fail too!

Manu ErwinManu Erwin

Update: after further testing I've found the problem to be when attempting to save with a Status value of '5' (Draft).

 

All the other values can be set (0 through 4) and saved successfully.

 

Also queried a bunch of records and cannot find any '5' values so am chalking this up to being a temporary picklist value that is never actually saved.

This was selected as the best answer
empucempuc

Manu,

 

Thanks for posting this one. I didn't know it.

 

Best regards

vishal@forcevishal@force

Thanks!

 

I wasn't aware of this either.