1

I want to instantiate Approval.ProcessWorkitemRequest in my test class. I am creating Approval process for that.

Account objAccount = new Account(Name = 'Test Account',BillingState ='LA'); 
insert objAccount;

Id recordTypeId = [Select Id,Name from RecordType where Name =: 'General Sales Opportunity'].Id;

Opportunity objOpportunity1 = new Opportunity(Name = 'Test Opportunity',AccountId = objAccount.Id,CloseDate = date.today(),StageName = 'Excecution',RecordTypeId = recordTypeId,Price_Approval_Status__c = 'Approval Required, Rejected'); 
insert objOpportunity1;

Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(objOpportunity1.Id);
req1.setProcessDefinitionNameOrId('OPPTY_Discount_Approval_Processv2');
req1.setSkipEntryCriteria(true);
// Submit the approval request for the SBQQ__QuoteLine__c
Approval.ProcessResult result = Approval.process(req1);
// Verify the result
System.assert(result.isSuccess());
system.debug('--------NewWorkitemIds'+result.getNewWorkitemIds());

ProcessInstance is being created successfully, but I am not able to get getNewWorkitemIds(). It showing blank in the debug. I even tried queried ProcessInstanceWorkitem object.It is returning no rows.What can be the reason for this?

1

Based on the code you've provided, it doesn't appear as though you've created any test data. You first have to create the data for your approval process which includes the accounts, Opportunity, user, etc. You can't submit an Opportunity for approval in your test class that hasn't been created, inserted, etc, and therfore doesn't exist for you to query.

Edit

One of the problems I see now that you've provided your test setup data, is in the line of code that creates your objOpportunity record where you have RecordTypeId = recordTypeId. The query to get that isn't valid, you can't add .Id to the end of a query as an assignment as you've done below:

Id recordTypeId = [Select Id,Name from RecordType where Name =: 'General Sales Opportunity'].Id;

I also would recommend against using recordTypeId as the var name since this is a something of a keyword. Instead, your query should look like:

Id rtId = [Select Id from RecordType where Name =: 'General Sales Opportunity']; 
// no need to query for Name since you're not going to use it

When you create your opportunity, you can then use RecordTypeId = rtID.Id

Do you have "SeeAllData" enabled? If not, I don't believe you'll be able to query the RecordType, but I could be mistaken on that (its been a while).

I'd also recommend you check your org's required fields for creating a new account. I suspect you didn't include all of your test class. There should be a valid Owner who created the Account and also a User who submitted the ProcessSubmitRequest who might be different than the Owner.

  • Sorry for insufficient data. Account objAccount = new Account(Name = 'Test Account',BillingState ='LA'); insert objAccount; Id recordTypeId = [Select Id,Name from RecordType where Name =: 'General Sales Opportunity'].Id; Opportunity objOpportunity1 = new Opportunity(Name = 'Test Opportunity',AccountId = objAccount.Id,CloseDate = date.today(),StageName = 'Excecution',RecordTypeId = recordTypeId,Price_Approval_Status__c = 'Approval Required, Rejected'); insert objOpportunity1; – Smitha May 21 '15 at 14:13
  • 1
    You can update your post using the edit link at the bottom left. – crmprogdev May 21 '15 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.