1

I am following this post to related parent child objects. Please review below code which I am testing but getting a DML error. I could not figure out the mistake I am doing. Any guidance will be appreciated.

Case caseRecord = [SELECT Subject, AccountId, ContactId, (SELECT Equipment__c, Quantity__c FROM Equipment_Maintenance_Items__r)
                    FROM Case
                    WHERE Id = '5008c00001J6GNdAAN'];

List<Equipment_Maintenance_Item__c> equipItems = new List<Equipment_Maintenance_Item__c>();

Case newCase = caseRecord.Clone(false, true);

newCase.ExternalId__c = 'test' + Datetime.now();

for (Equipment_Maintenance_Item__c equpItem: caseRecord.Equipment_Maintenance_Items__r)
{
    Equipment_Maintenance_Item__c newEquipItem = equpItem.Clone(false, true);
    newEquipItem.Maintenance_Request__r = new Case (ExternalId__c = newCase.ExternalId__c);
    equipItems.add(newEquipItem);
   // insert newEquipItem;
}

insert newCase;
upsert equipItems;
System.debug ('case Subject is: '+ newCase.Id);

Error:

FATAL_ERROR|System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_FIELD, Cannot specify both an external ID reference Maintenance_Request__r and a salesforce id, Maintenance_Request__c: []

Full debug output:

16:36:17.213 Starting Execute Anonymous Apex

Compiled successfully.
Error: System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_FIELD, Cannot specify both an external ID reference Maintenance_Request__r and a salesforce id, Maintenance_Request__c: []
Error: AnonymousBlock: line 20, column 1

54.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO
Execute Anonymous: Case caseRecord = [SELECT Subject, AccountId, ContactId, (SELECT Equipment__c, Quantity__c FROM Equipment_Maintenance_Items__r)
Execute Anonymous:                     FROM Case
Execute Anonymous:                     WHERE Id = '5008c00001J6GNdAAN'];
Execute Anonymous: 
Execute Anonymous: List<Equipment_Maintenance_Item__c> equipItems = new List<Equipment_Maintenance_Item__c>();
Execute Anonymous: 
Execute Anonymous: Case newCase = caseRecord.Clone(false, true);
Execute Anonymous: 
Execute Anonymous: newCase.ExternalId__c = 'test' + Datetime.now();
Execute Anonymous: 
Execute Anonymous: for (Equipment_Maintenance_Item__c equpItem: caseRecord.Equipment_Maintenance_Items__r)
Execute Anonymous: {
Execute Anonymous:     Equipment_Maintenance_Item__c newEquipItem = equpItem.Clone(false, true);
Execute Anonymous:     newEquipItem.Maintenance_Request__r = new Case (ExternalId__c = newCase.ExternalId__c);
Execute Anonymous:     equipItems.add(newEquipItem);
Execute Anonymous:    // insert newEquipItem;
Execute Anonymous: }
Execute Anonymous: 
Execute Anonymous: insert newCase;
Execute Anonymous: upsert equipItems;
Execute Anonymous: System.debug ('case Subject is: '+ newCase.Id);
16:36:18.216 (216415381)|USER_INFO|[EXTERNAL]|0058c00000BtpT5|[email protected]|(GMT-04:00) Eastern Daylight Time (America/New_York)|GMT-04:00
16:36:18.216 (216443501)|EXECUTION_STARTED
16:36:18.216 (216451164)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
16:36:18.216 (312680957)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Case
16:36:18.216 (333644379)|CODE_UNIT_FINISHED|Workflow:Case
16:36:18.216 (334442626)|CODE_UNIT_STARTED|[EXTERNAL]|SLA
16:36:18.216 (337417301)|CODE_UNIT_FINISHED|SLA
16:36:18.216 (343066459)|EXCEPTION_THROWN|[20]|System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_FIELD, Cannot specify both an external ID reference Maintenance_Request__r and a salesforce id, Maintenance_Request__c: []
16:36:18.216 (343689681)|FATAL_ERROR|System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_FIELD, Cannot specify both an external ID reference Maintenance_Request__r and a salesforce id, Maintenance_Request__c: []

AnonymousBlock: line 20, column 1
16:36:18.343 (343715083)|CUMULATIVE_LIMIT_USAGE
16:36:18.343 (343715083)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 5 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 2 out of 150
  Number of Publish Immediate DML: 0 out of 150
  Number of DML rows: 5 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:36:18.343 (343715083)|CUMULATIVE_LIMIT_USAGE_END

16:36:18.216 (343774546)|CODE_UNIT_FINISHED|execute_anonymous_apex
16:36:18.216 (343782053)|EXECUTION_FINISHED

16:36:17.853 ended Execute Anonymous Apex
3
  • Is Maintenance_Request__c on object Equipment_Maintenance_Item__c ? if so, your clone isn't clearing this field and hence the conflict. Null it out before adding to equipItems
    – cropredy
    Jun 8, 2022 at 22:31
  • Yes. And adding this line does not resolve the issue. newEquipItem.Maintenance_Request__c = null;
    – Umer
    Jun 8, 2022 at 22:52
  • hmm, try the last example's approach here. See also this Q&A which illustrates that setting lookup field to null fails as you observed
    – cropredy
    Jun 8, 2022 at 23:44

1 Answer 1

0

I resolved this error by creating new objects for Case and custom objects.

Instead of:

Case newCase = caseRecord.Clone(false, true);

Used:

Case newCase = new Case (*set fields* );

The clone() method is really messed up and should be avoided whenever possible, just an advice.

5
  • 1
    The clone() method is not messed up. The wording in the documentation does have some deficiencies, but the method does behave consistently and with its intended behavior. The issues you faced were simply due to not having a complete enough understanding of how SObjects behave (mainly that 1: fields can't be unset in memory once set, and 2: you can't set both the relationship field and the embedded object and then perform dml, even if you set one of them to null).
    – Derek F
    Jun 12, 2022 at 2:52
  • This makes sense but then I am wondering how the same answer which I referred in my question is widely accepted as an answer?
    – Umer
    Jun 12, 2022 at 2:55
  • Please advise if I can still resolve my problem by using clone() method. I could not find a way to make it work.
    – Umer
    Jun 12, 2022 at 2:56
  • Nope, clone() isn't the right tool here. You need to completely get rid of one of Maintenance_Request__c or Maintenance_Request__r, and cloning a record can't accomplish that. new Case() is the way to go. My main point was correcting the misconception about clone() being broken.
    – Derek F
    Jun 16, 2022 at 0:44
  • Almost every object has a parent and child relation so Clone() can be more useful for standalone objects.. but your point is well taken. thank you.
    – Umer
    Jun 17, 2022 at 1:21

You must log in to answer this question.

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