function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Exception for association after successive shedule.



 I am associating two custom objects with respective record id's and external id. I can successfully associate and upsert the records for two custom objects from external web service. When I have sheduled this job for successive run, association and upsert will fail after 3rd or 4th time scheduler runs for the same records. I have given DML exception which I got for the same mentioned below. 


Dml Exception : Upsert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Attempting to update (as part of an upsert) parent field ConnectCompiere__Sales_Order__c with new value a0X40000000FWPAEA4, current value is a0X40000000FWP9EAO: [ConnectCompiere__Sales_Order__c]


Below I have attached the method written for associating two aobjects and upsert the same from external webservice.



public static void associateInvoiceLineWithInvoiceNew(InvoiceLine__c line, String setUpId) {
try {

//Query to select id of the parent object which are matching with child object.

Compiere_Invoice__c[] invId = [select id from Compiere_Invoice__c where C_Invoice_ID__c =: line.C_Invoice_ID__c];
for(Compiere_Invoice__c i : invId) {


//Association object between parent and child object.

InvoiceLineAssociation__c association = new InvoiceLineAssociation__c();
System.debug('Invoice Id ------>' +;
association.Invoice__c =;
System.debug('Invoice Line Id ----->' +;
association.Invoice_Line__c =;
System.debug('Invoice Line Id(External Id)---->' + line.C_InvoiceLine_ID__c);
association.Invoice_Line_Id__c = line.C_InvoiceLine_ID__c;

//Upsert assocociation object with external id.

upsert association Invoice_Line_Id__c;
} catch (DmlException e) {
System.debug('DML Exception in association---->' + e.getMessage());
errorMessage = errorMessage + ' Dml Exception : '+e.getMessage();
}catch(System.Exception exp){
System.debug('System Exception in association---->' + exp.getMessage());
errorMessage = errorMessage + ' Exception : '+exp.getMessage();




Please let me know anyone knows about detals of same exception which I got for above code.


Any help would be great for me to debug the same.


Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 

You can't re-parent a child record's master-detail relationship field. This can only be set upon creation.


Here is a similar board thread on the topic.