I'm trying to clone a Case and modify a lookup using an external ID before inserting.

The problem is that I have this error:

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_FIELD, Cannot >specify both an external ID reference SubReason__r and a salesforce id, SubReason__c

here is my code

String unikkey = [SELECT UniqueKey__c FROM CaseQualification__c WHERE UniqueKey__c =: CASEQUALIF_COPYCONTRACRTSIGNED LIMIT 1].UniqueKey__c;
            for(Case_Request_Return_Contract__mdt caseRequestMdt : [SELECT Unique_Key__c FROM Case_Request_Return_Contract__mdt]){
                if(caseRequestMdt.Unique_Key__c == c.TechSubReasonUniqueKey__c){
                    CaseQualification__c cq = new CaseQualification__c(UniqueKey__c = unikkey);
                    Case clonedCase = CaseCloneService.cloneCase(c);
                    clonedCase.ParentId = c.Id;
                    clonedCase.SubReason__c = null;
                    clonedCase.SubReason__r = cq;
            insert listClonedCase;

EDIT: the case clone service is just a standard clone(false,true,false,false). I can't specify SubReason__c to not be set, Can I?.

As you see in the code I want to have my lookup SubReason__c be set to an external id, that is why I use SubReason__r. The problem is even if I'm setting subReason__c to null I still have the error above.

Went to check this post: Error Message When Using Clone Method in External ID scenario

And the guy have the same issue, resolved using maps which I could use but it would be more code and I would like to understand why it is not working when using clone.


  • Make sure your "case clone service" can be told to not include the SubReason__c field when querying the original Case and creating the clone.
    – Phil W
    Commented Aug 30, 2023 at 9:57
  • Thanks Phil, i'm updating my post to explain what is in the clone service which is a basic apex Clone Commented Aug 30, 2023 at 10:10

1 Answer 1


As the error says, you can't specify both the __c and __r, even if one of those are null. You'll need to change your method to not set the SubReason__c field at all, not even a null value, or query the SubReason__c values and use the ID.

  • Yeah, it kind of destroy the purpose of my clone method, which is a standard clone. Thanks for the answer tho. I'll just use maps instead. Commented Aug 30, 2023 at 10:09

You must log in to answer this question.

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