You need to sign in to do that
Don't have an account?
salesforce explorer
How to resolve INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
How to resolve
Error: Line: 68, Column: 1
System.DmlException: Process failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
Code:
global class BMCRF_CustomApprovalProcess implements Schedulable
{
global void execute(SchedulableContext SC)
{
//----Variable declaration--------
string description = 'Do you want to send this to another staff member to complete this form?: No';
list<BMCServiceDesk__Incident__c> incRecord = new list<BMCServiceDesk__Incident__c>();
list<ProcessInstance> workItemList = new list<ProcessInstance>();
list<ProcessInstanceWorkitem> AppoverName = new list<ProcessInstanceWorkitem>();
map<id,id> finalList = new map<id,id>();
map<id,string> userID = new map<id,string>();
map<id,string> records = new map<id,string>();
map<Id,string> approvalRecords = new map<id,string>();
map<id,string> approverList = new map<id,string>();
map<id,BMCServiceDesk__Incident__c> approvedRecordList = new map<id,BMCServiceDesk__Incident__c>();
//--------------------------------
//-----Fetching Incident records----------------------
incRecord = [Select id,name,BMCServiceDesk__Service_Request_Title__c,BMCServiceDesk__Client_Name__c, BMCServiceDesk__incidentDescription__c from BMCServiceDesk__Incident__c where BMCServiceDesk__Service_Request_Title__c = 'New User Request Form For Workday' OR BMCServiceDesk__Service_Request_Title__c = 'New User Request Form'];
for(BMCServiceDesk__Incident__c inc:incRecord)
{
if(inc.BMCServiceDesk__incidentDescription__c.contains(description))
{
records.put(inc.Id,inc.BMCServiceDesk__Client_Name__c);
}
}
//----Fetching records in pending approval state---------
workItemList = [SELECT Id,targetObjectId FROM ProcessInstance WHERE Status='Pending' AND ProcessInstance.targetObjectId IN: records.keySet()];
AppoverName = [SELECT ProcessInstance.targetObjectId,ProcessInstanceId,OriginalActor.Name,OriginalActorId FROM ProcessInstanceWorkitem WHERE ProcessInstance.targetObjectId IN: records.keySet()];
for(ProcessInstanceWorkitem record: AppoverName)
{
approverList.put(record.ProcessInstance.targetObjectId,record.OriginalActor.Name);
userID.put(record.ProcessInstanceId,record.OriginalActorId);
}
//--------Getting records to be Approved----------
for(ProcessInstance pi: workItemList)
{
id recID = pi.targetObjectId;
if(approverList.get(recID).equals(records.get(recID)))
{
finalList.put(pi.id,recID);
}
}
system.debug('KeySet : '+finalList.keySet());
system.debug(' Values : '+finalList.values() );
for(id pi:finalList.keySet())
{
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments('Approving request using CustomApprovalProcess');
req.setAction('Approve');
req.setNextApproverIds(new Id[]{userID.get(pi)});
req.setWorkitemId(pi);
Approval.ProcessResult result = Approval.process(req);
system.debug('Incident Records : '+pi);
}
}
}
Error: Line: 68, Column: 1
System.DmlException: Process failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
Code:
global class BMCRF_CustomApprovalProcess implements Schedulable
{
global void execute(SchedulableContext SC)
{
//----Variable declaration--------
string description = 'Do you want to send this to another staff member to complete this form?: No';
list<BMCServiceDesk__Incident__c> incRecord = new list<BMCServiceDesk__Incident__c>();
list<ProcessInstance> workItemList = new list<ProcessInstance>();
list<ProcessInstanceWorkitem> AppoverName = new list<ProcessInstanceWorkitem>();
map<id,id> finalList = new map<id,id>();
map<id,string> userID = new map<id,string>();
map<id,string> records = new map<id,string>();
map<Id,string> approvalRecords = new map<id,string>();
map<id,string> approverList = new map<id,string>();
map<id,BMCServiceDesk__Incident__c> approvedRecordList = new map<id,BMCServiceDesk__Incident__c>();
//--------------------------------
//-----Fetching Incident records----------------------
incRecord = [Select id,name,BMCServiceDesk__Service_Request_Title__c,BMCServiceDesk__Client_Name__c, BMCServiceDesk__incidentDescription__c from BMCServiceDesk__Incident__c where BMCServiceDesk__Service_Request_Title__c = 'New User Request Form For Workday' OR BMCServiceDesk__Service_Request_Title__c = 'New User Request Form'];
for(BMCServiceDesk__Incident__c inc:incRecord)
{
if(inc.BMCServiceDesk__incidentDescription__c.contains(description))
{
records.put(inc.Id,inc.BMCServiceDesk__Client_Name__c);
}
}
//----Fetching records in pending approval state---------
workItemList = [SELECT Id,targetObjectId FROM ProcessInstance WHERE Status='Pending' AND ProcessInstance.targetObjectId IN: records.keySet()];
AppoverName = [SELECT ProcessInstance.targetObjectId,ProcessInstanceId,OriginalActor.Name,OriginalActorId FROM ProcessInstanceWorkitem WHERE ProcessInstance.targetObjectId IN: records.keySet()];
for(ProcessInstanceWorkitem record: AppoverName)
{
approverList.put(record.ProcessInstance.targetObjectId,record.OriginalActor.Name);
userID.put(record.ProcessInstanceId,record.OriginalActorId);
}
//--------Getting records to be Approved----------
for(ProcessInstance pi: workItemList)
{
id recID = pi.targetObjectId;
if(approverList.get(recID).equals(records.get(recID)))
{
finalList.put(pi.id,recID);
}
}
system.debug('KeySet : '+finalList.keySet());
system.debug(' Values : '+finalList.values() );
for(id pi:finalList.keySet())
{
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments('Approving request using CustomApprovalProcess');
req.setAction('Approve');
req.setNextApproverIds(new Id[]{userID.get(pi)});
req.setWorkitemId(pi);
Approval.ProcessResult result = Approval.process(req);
system.debug('Incident Records : '+pi);
}
}
}
Can you highlight line 68 of the code that is throwing the error message? I see you code is only 63 lines when copied to text editor.
Related posts: https://salesforce.stackexchange.com/questions/157738/invalid-cross-reference-key-invalid-cross-reference-id
https://www.infallibletechie.com/2022/02/invalidcrossreferencekey-invalid-cross.html
Thanks