1

We have the following code and Apex Test Class, and for some reason during Apex Test Execution (Developer Console-->Run Test) it is throwing the following error message below. Any ideas why the field could not be writable? The field in question is a Long Text Area(32768). I checked that the user has CRUD on the object as well as FLS read/write on the field. I even gave the profile "Modify All" on the object and still the error below.

System.SObjectException: Field is not writeable: My_API_Logs__c.My_Request__c

EDIT: It seems like the only way to go around is to set the apex to "Without Sharing". This is strange and seems like a bug.

    public with sharing class My_API_Log {
 public static My_API_Log__c setErrorLogs(String strRequest, String strResponse, String strCode, String strErrorDetails, Id strContactId, String strType){
        try{
            My_API_Logs__c objError = new My_API_Logs__c();
            objError.My_Request__c = strRequest;
            objError.My_Response__c = strResponse;
            objError.My_Status_Code__c = strCode;
            objError.My_Error_Details__c = strErrorDetails;
            objError.My_Contact__c = strContactId;
            objError.My_Type__c = strType;
            return objError;
        }catch(Exception e){
            throw e;
        }
    }   
}

And the line of code that is failing is where the "insert" statement line is below: (DISCLAIMER: this is just a code snippet for simplicity)

public with sharing class MyClass {
    public static list<My_API_Log__c> lstLogs = new lits<My_API_Log__c>();
    
    @AuraEnabled
    public static string myMethod(){
       string strresult = 'test';

       HttpRequest req = new HttpRequest();
       if(req.getStatusCode() == 200){
       
           insert My_API_Log.setErrorLogs('Test', 'Test,'Test', 'Test',null, 'Test'); //This is where its erroring out!
       }
    
       return strresult;
    }
}

Apex Test Class

@isTest(seeAllData=false)
public class MyClassTest{
    static testMethod void testMyMethod(){
        //Created Account
        //Created Contact
        //Created the Contact as a User (CustomerCommunityUser).
 
        System.runAs(CustomerCommunityUser){
        Test.startTest();

        Map<String, String> headers = new Map<String, String>();
        headers.put('Content-Type','application/json');


        
        MyClassMock mock= new MyClassMock(
            200,
            'success',
            'success',
            headers
        );
        Test.setMock(HttpCalloutMock.class, mockFailure);

        String str = MyClass.myMethod();
        Test.stopTest();

        }
    }
}
10
  • What's the data type for My_Request__c?
    – sfdcfox
    Nov 9 '20 at 18:59
  • 7
    Does this answer your question? Setting/mocking out formula fields during tests Nov 9 '20 at 19:01
  • Hi @sfdcfox, the My_Request__c field is a Long Text Area(32768).
    – jojopogi
    Nov 9 '20 at 20:11
  • @RenatoOliveira - the field is a Long Text Area(32768) and not a formula field. Unfortunately, the article does not apply in my situation.
    – jojopogi
    Nov 9 '20 at 20:13
  • Are you sure the error is on that field/line? This error usually happesn with lookup fields... Nov 9 '20 at 21:22