0

I'm getting the below error while running the test class. Can anyone help on this please?

Error :

System.DmlException: Insert failed. First exception on row 0 with id a057h000002BqVUAA0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

enter image description here

Class:

public class CS_ExceptionLoggerFlow {
       
    @InvocableMethod(label='Exception Log From Flow')
    public static void exceptionLogFromFlow(List<Params> inputVars) {
        set<Id> logId = new set<Id>();
        createExceptionLog(logId);
    }
    
    @future
    public static void createExceptionLog(set<Id> logId){
        String apexClass;
        String methodName;
        String exceptionMessage;
        DateTime exceptionDated;
        String userName;
        
        for(Id sfID : logId){                         
            ExceptionLogger__c log = new ExceptionLogger__c();
            log.Id = sfID;
            log.Apex_Class__c = apexClass;
            log.Method_Name__c = methodName;
            log.Description__c = exceptionMessage;
            log.Exception_Dated__c = exceptionDated;
            log.User_Name__c = userName;
            insert log;
        }           
    }        
    public class Params {
        @InvocableVariable public String apexClass;
        @InvocableVariable public String methodName;
        @InvocableVariable public String exceptionMessage;
        @InvocableVariable public DateTime exceptionDated;
        @InvocableVariable public Boolean isAPIFailure;
        @InvocableVariable public String userName;
    }
    
} 

Test Class :

@IsTest
public with sharing class CS_ExceptionLoggerFlowTest {
    
    @IsTest
    public static void testExceptionLog() {
        
        ExceptionLogger__c log = new ExceptionLogger__c(Apex_Class__c = 'CS_ExceptionLoggerFlow',
                                                        Method_Name__c = 'exceptionLogFromFlow',
                                                        Description__c = 'message',
                                                        Exception_Dated__c = System.today(),
                                                        User_Name__c = 'userName'
                                                       );                                     
        insert log;
                
        Set<Id> logIds = new Set<Id>();
        logIds.add(log.id);
        Test.startTest();
        CS_ExceptionLoggerFlow.createExceptionLog(logIds);
        Test.stopTest();
        List<ExceptionLogger__c> logs = [SELECT Id FROM ExceptionLogger__c];
        System.AssertEquals(1, logs.size(), 'Should insert one ExceptionLogger__c record');
    }
}  
3
  • 1
    Please always include code/errors as text rather than images.
    – Adrian Larson
    Commented Oct 6, 2022 at 13:54
  • Welcome to Salesforce Stack Exchange (SFSE)! To elaborate on Adrian Larson's comment, Please do not post error text exclusively as a screenshot - it is not as accessible to users who use screen readers or other assistive technology. The convention here on SFSE is to format error text with a greater than symbol (>) at the beginning to format it as a quote block - it helps it to stand out.
    – Moonpie
    Commented Oct 6, 2022 at 13:57
  • I have updated the post with error message. Sorry for the inconvenience and thank you for your suggestion.
    – user122320
    Commented Oct 6, 2022 at 14:01

2 Answers 2

1

Your @future method has a syntax error in it.

for(Id sfID : logId){                         
    ExceptionLogger__c log = new ExceptionLogger__c();
    log.Id = sfID;
    log.Apex_Class__c = apexClass;
    log.Method_Name__c = methodName;
    log.Description__c = exceptionMessage;
    log.Exception_Dated__c = exceptionDated;
    log.User_Name__c = userName;
    insert log; // This is the problematic line
}

As the error is telling you, you cannot insert a record if it already has an Id. You are specifically setting the Id with log.Id = sfID;.

You need to perform an update here instead.

0

You cannot specify Id value for any record which you are inserting, it will be unique value for a record. Either insert the record without Id value (i.e. log.Id = sfID;) or if those log records are present thn create new field on the object and assign sfID value to that new field in update operation.

You must log in to answer this question.