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
ArrgHunterArrgHunter 

Insert failed. First exception on row 12; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID:

Hi,

 

i have a class that i run from system log screen ervery month. it was working fine but all of a suddon it has started giving errors as below.

 

 

*********************************** Error Log******************************

 

Apex script unhandled exception by user/organization: 00520000001grzo/00D20000000BaRL

 

Failed to process batch for class 'clsInvoiceRun' for job id '7072000000AspWh'

 

caused by: System.DmlException: Insert failed. First exception on row 12; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]

 

Class.clsInvoiceRun.execute: line 180, column 9

External entry point

 

Debug Log:

6 (476287000)|SYSTEM_METHOD_ENTRY|[163]|MAP.containsKey(ANY)

14:53:05.476 (476365000)|SYSTEM_METHOD_EXIT|[163]|MAP.containsKey(ANY)

14:53:05.476 (476400000)|STATEMENT_EXECUTE|[163]

14:53:05.476 (476458000)|VARIABLE_ASSIGNMENT|[161]|i|103

14:53:05.476 

.

.

.long list continue.....

.

.

14:53:05.818 (818405000)|WF_RULE_FILTER|[Invoice : Invoice Status equals Finance Approved]

AND [Invoice : Record Type equals Invoice Stage 2]

14:53:05.818 (818527000)|WF_RULE_EVAL_VALUE|1

14:53:05.818 (818595000)|WF_CRITERIA_END|false

14:53:05.818 (818681000)|WF_SPOOL_ACTION_BEGIN|Workflow

14:53:05.818 (818828000)|WF_ACTION| None

14:53:05.818 (818897000)|WF_RULE_EVAL_BEGIN|Escalation

14:53:05.818 (818960000)|WF_RULE_EVAL_END

14:53:05.819 (819194000)|WF_ACTIONS_END| None

14:53:05.819 (819278000)|CODE_UNIT_FINISHED|Workflow:01I20000000cVKI

14:53:05.820 (820086000)|DML_END|[180]



 

any help will be appriciated

 

thanks,

 

Best Answer chosen by Admin (Salesforce Developers) 
spraetzspraetz

This query on line 154:

 

List<Gift__c> qryGift = [SELECT Id, Donor__c, Donor__r.IRP_Country__c, Item__c, Orphan_Id__c, Gift_Amount__c, Gift_Date__c, Invoiced_On__c,
                                        Donor__r.IRP_Country__r.Primary_PO_User__c, Orphan_Id__r.OwnerId
                                   FROM Gift__c 
                                  WHERE Gift_Date__c <= :dateEnd
                                    AND Invoiced_On__c = NULL FOR UPDATE];

 appears to be returning a blank value in your Donor__r.IRP_Country__r.Primary_PO_User__c field for one of the records returned. 

All Answers

spraetzspraetz

Would need to see the code being executed.

ArrgHunterArrgHunter

below is the code, i invoke this class from system log -> execute  Database.executeBatch(new clsInvoiceRun());

 

thanks,



 

//DEBUG
system.debug('Invoice Header 1, country ' + qrySponsors[i].Country__c);

                Invoice__c itmHdr = new Invoice__c (                   
                    IRP_Country__c = qrySponsors[i].Country__c,
                    Invoice_Date__c = invDate,
                    Invoice_Status__c = 'Generated',
                    RecordTypeId = hdrRT,
                    OwnerId = qrySponsors[i].Country__r.Primary_PO_User__c
                );
                newhdr.add(itmHdr);
            }
        }

        // Get any unbilled gifts
        List<Gift__c> qryGift = [SELECT Id, Donor__c, Donor__r.IRP_Country__c, Item__c, Orphan_Id__c, Gift_Amount__c, Gift_Date__c, Invoiced_On__c,
                                        Donor__r.IRP_Country__r.Primary_PO_User__c, Orphan_Id__r.OwnerId
                                   FROM Gift__c
                                  WHERE Gift_Date__c <= :dateEnd
                                    AND Invoiced_On__c = NULL FOR UPDATE];

        // Make sure a header appears for this country                       
        for (Integer i = 0; i < qryGift.size(); i++) {
           
            if(!setPtnr.containsKey(qryGift[i].Donor__r.IRP_Country__c)){
                setPtnr.put(qryGift[i].Donor__r.IRP_Country__c,'Inv Hdr');

//DEBUG
system.debug('Invoice Header 2, country ' + qryGift[i].Donor__r.IRP_Country__c);

                Invoice__c itmHdr1 = new Invoice__c (                   
                    IRP_Country__c = qryGift[i].Donor__r.IRP_Country__c,
                    Invoice_Date__c = invDate,
                    Invoice_Status__c = 'Generated',
                    RecordTypeId = hdrRT,
                    OwnerId = qryGift[i].Donor__r.IRP_Country__r.Primary_PO_User__c
                );
                newhdr.add(itmHdr1);
            }
        }
       
        insert newHdr;

 

 

 

 

 

spraetzspraetz

This query on line 154:

 

List<Gift__c> qryGift = [SELECT Id, Donor__c, Donor__r.IRP_Country__c, Item__c, Orphan_Id__c, Gift_Amount__c, Gift_Date__c, Invoiced_On__c,
                                        Donor__r.IRP_Country__r.Primary_PO_User__c, Orphan_Id__r.OwnerId
                                   FROM Gift__c 
                                  WHERE Gift_Date__c <= :dateEnd
                                    AND Invoiced_On__c = NULL FOR UPDATE];

 appears to be returning a blank value in your Donor__r.IRP_Country__r.Primary_PO_User__c field for one of the records returned. 

This was selected as the best answer
ArrgHunterArrgHunter

Thanks Ryan, you definitely saved my many hours. You are a STAR