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
Arun Yadav 13Arun Yadav 13 

test class for approval process

Hi

I need to write a test class from below approval process. Can anyone please help me

    public static Approval.ProcessResult removeQuoteFromApproval(Id quoteId) {
      List<ProcessInstance> piList = [Select p.TargetObjectId, p.Status From ProcessInstance p where TargetObjectId = :quoteId];
      Approval.ProcessResult result =  null;
      if(piList.size() > 0 && piList[0].Status == 'Pending') {
        Approval.ProcessWorkitemRequest pwr = new Approval.ProcessWorkitemRequest();
      pwr.setAction('Removed');
      pwr.setComments('Quote submitted with updated discounts');
      pwr.setWorkitemId(quoteId);
      result = Approval.process(pwr);
      
      if(!result.isSuccess()) {
          Database.Error[] errors = result.getErrors();
          for(Database.Error dbe : errors) {
            system.debug(dbe.getMessage());
          }
        }
      }
      
      return result;
    }

Thanks
sfdcFanBoysfdcFanBoy
Please let us know what you have tried so far, so we can help with the issue.

Anyways, hope this helps
https://developer.salesforce.com/forums/?id=906F00000008zyqIAA
Veenesh VikramVeenesh Vikram
Hi Arun,

In your test class, you need to create a Quote. And then u need to submit your Quote record for approval using apex. refer: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_process_example.htm.

Post that, call your method "removeQuoteFromApproval" and your test class shall work.

Best Regards
Veenesh
Arun Yadav 13Arun Yadav 13
Hi

I'm not understanding the class code. Hence can you please write the test class for this method.

Arun
Veenesh VikramVeenesh Vikram
public class myClass {
    static testMethod void myTest() {
		//Create a Account Record
		//Create an Opportunity Record
		//Create a Quote record
		
		//Submit Quote for Approval
		Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
		app.setObjectId(Quote.id);
		Approval.ProcessResult result = Approval.process(app);
		Test.StartTest();
		//Call your class method
		Class.removeQuoteFromApproval(Quote.id);
		Test.StopTest();
     }
}
You need to do something like this.

Kindly mark as solved if it helps.

Best Regards
Veenesh