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
SirTravSirTrav 

INVALID_CROSS_REFERENCE_KEY only in Production not in Sandbox

I have created a button to recall an approval process.  Here is my code:

    public pagereference TotalRecal(){
        o = [select Needs_Approval__c, recordtypeid from opportunity where id = :apexpages.currentpage().getparameters().get('id')];
        pi = [select id from ProcessInstance where TargetObjectId = :o.id];
        if (pi.isempty()) {
            o.StageName = 'Negotiations';
            o.Needs_Approval__c = true;
        }
        else{
            for (ProcessInstance p : pi) {
                piID.add(p.id);
            }
            ProcessInstanceWorkitem[] piwi = [select id from ProcessInstanceWorkitem where ProcessInstanceId in :piID];
            for (ProcessInstanceWorkitem p : piwi) {
                approval.ProcessWorkitemRequest pwr = new Approval.ProcessWorkitemRequest();
                pwr.setWorkitemId(p.id);
                pwr.setAction('Removed');
                pwr.setComments(com);
                Approval.ProcessResult result =  Approval.process(pwr);
            }
            o.StageName = 'Negotiations';
            o.Needs_Approval__c = true;
        }
        update o;
        pagereference page = new pagereference('/'+o.id);
        return page;
    }

 It works correctly in the sandbox but when I try to move it to production I get this error: 

 

Failure Message: "System.DmlException: Process failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []", Failure Stack Trace: "Class.ApprovalRecalExt.TotalRecal: line 27, column 1 Class.ApprovalRecalExtTest.ApprovalRecalExtTest: line 18, column 1"

 

Here is my test class: 

@istest
public class ApprovalRecalExtTest{
public static testmethod void ApprovalRecalExtTest(){
	account a = new account(name='test');
	insert a;
	opportunity o = new opportunity(name='testopp', account=a, stagename='approved', closedate=date.today());
	insert o;
	pagereference page1 = new pagereference('/apex/approvalrecal?id='+o.id);
    test.setcurrentpage(page1);
    ApprovalRecalExt ar = new ApprovalRecalExt();
    ar.TotalRecal();
    ar.neverMind();
    Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
    psr.setObjectId(o.id);
    Approval.ProcessResult result = Approval.process(psr);
    test.setcurrentpage(page1);
    ApprovalRecalExt ar1 = new ApprovalRecalExt();
    ar1.TotalRecal();
    
}
}

 I can't figure out why it would work in sandbox but not production.  Thanks for any help you can give me.  

 

Avidev9Avidev9
seems like issue is somewhere else.
will you be able to post the code for "ApprovalRecalExt " class?
SirTravSirTrav

Avidev9 wrote:
seems like issue is somewhere else.
will you be able to post the code for "ApprovalRecalExt " class?

the top section of code is the ApprovalRecalExt.  I just cut out a couple of small pieces that don't apply like a second method that is just a pagereference / cancel button.  Line 27 that it references is the line:

Approval.ProcessResult result =  Approval.process(pwr);

 

 

Avidev9Avidev9

Not sure about the actulal problem , On seperate note seems like you never queried Id

 

 o = [select Id, Needs_Approval__c, recordtypeid from opportunity where id = :apexpages.currentpage().getparameters().get('id')];
        pi = [select id from ProcessInstance where TargetObjectId = :o.id];

 

SirTravSirTrav
Queries automatically return id and I think name, but don't quote me on the name thing.
SirTravSirTrav

I think I have made some progress hopefully this will help.  the approval.process requites a ProcessRequest not a ProcessWorkitemRequest.  now how do I go from a ProcessWorkitemRequest to a ProcessRequest.

crop1645crop1645

I don't see in your testmethod setting the Page param that your TotalRecal method requires:

 

o = [select id, Needs_Approval__c, recordtypeid from opportunity where id = :apexpages.currentpage().getparameters().get('id')];