You need to sign in to do that
Don't have an account?
"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.
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
Status field on a Case is a regular editable picklist, while Status on EmailMessage is read only.
Status
Type - picklist
Properties - Create, Filter, Restricted 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!
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.
Manu,
Thanks for posting this one. I didn't know it.
Best regards
Thanks!
I wasn't aware of this either.