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
CushtyCushty 

java button to run approval process class code coverage

Hi,

I have created a custom button on the Opportunity object.  When clicked it runs a class which in turn starts off the approval process. 

This works fine in my developer sandbox but is not deploying due to code coverage issues.  I wonder if anyone can help, bearing in mind I have got as far as I can with some research as I am not a programmer and wanting to learn

Thanks

Class code with no coverage: -

global class Opportunity_Approval_to_PrepBid {

        public String myopi {get; set;}
        Webservice static String callApproval(String myopi) {

        Opportunity op = new Opportunity(Id=myopi);
        update op;

                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                req1.setComments('Submitted for Approval');
                req1.setObjectId(myopi);
                Approval.ProcessResult res = Approval.Process(req1);   
                
                return null;
              
    }
}


Test class I tried: -

@IsTest

Public Class testApprovePrepare
 {
    static testMethod void testApprovePrepare()
    {
    
    Account a = new Account();
    a.Name ='demo';
    a.Industry = 'Education';
    a.BillingCountry = 'United Kingdom';
    insert a;
    
    Opportunity o = new Opportunity();
    o.AccountId = a.Id;
    o.StageName = 'Pipeline';
    o.Business_Unit__c = '1Spatial United Kingdom';
    o.Name = 'Test';
    o.Opportunity_Type__c = 'Contract Renewal';
    o.Contract_Type__c = 'Framework';
    o.Estimated_Value_Number__c = 10000;
    o.Opportunity_Submission_Date__c = Date.TODAY();
    o.CloseDate = Date.TODAY() +2;
    o.Probability__c = '80-99%';
    o.Purchase_Order__c = 'PO Pending';
    o.Solution_Summary__c = 'Hardware';
    o.Description = 'Renewal Contract for the next financial year';
    o.Opportunity_Process_Stage__c= 'Qualified Opportunity';
    insert o;
    
    Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                req1.setComments('Submitted for Approval');
                req1.setObjectId(o.Id);
                req1.setProcessDefinitionNameOrId('Auto_Approval_to_Prepare_Bid');
                Approval.ProcessResult res = Approval.Process(req1); 
                
                try{
            res = Approval.process(req1);
        }catch(Exception e){
            System.debug('No approval process has been setup yet.');


     }
}
}

Any help would be appreciated in getting this deployed to production

Many Thanks
Best Answer chosen by Cushty
Manish BhatiManish Bhati
Actually method callApproval returns null. So, I gave the expected value to be null and it also came to be null. There will be some space issues.
For now you can use below code and your code coverage will be fine.
 
@IsTest

Public Class testApprovePrepare
 {
    static testMethod void testApprovePrepare()
    {
    
    Account a = new Account();
    a.Name ='demo';
    a.Industry = 'Education';
    a.BillingCountry = 'United Kingdom';
    insert a;
    
    Opportunity o = new Opportunity();
    o.AccountId = a.Id;
    o.StageName = 'Pipeline';
    o.Business_Unit__c = '1Spatial United Kingdom';
    o.Name = 'Test';
    o.Opportunity_Type__c = 'Contract Renewal';
    o.Contract_Type__c = 'Framework';
    o.Estimated_Value_Number__c = 10000;
    o.Opportunity_Submission_Date__c = Date.TODAY();
    o.CloseDate = Date.TODAY() +2;
    o.Probability__c = '80-99%';
    o.Purchase_Order__c = 'PO Pending';
    o.Solution_Summary__c = 'Hardware';
    o.Description = 'Renewal Contract for the next financial year';
    o.Opportunity_Process_Stage__c= 'Qualified Opportunity';
    insert o;
    
    String str = Opportunity_Approval_to_PrepBid.callApproval(o.Id);
    System.debug(str);
}
}

 

All Answers

CushtyCushty
Hi I get the following error when trying to tesrt it

System.AssertException: Assertion Failed: Expected: null, Actual: null
lie 32, do you know what this means?
Thanks
Nick
 
Manish BhatiManish Bhati
Actually method callApproval returns null. So, I gave the expected value to be null and it also came to be null. There will be some space issues.
For now you can use below code and your code coverage will be fine.
 
@IsTest

Public Class testApprovePrepare
 {
    static testMethod void testApprovePrepare()
    {
    
    Account a = new Account();
    a.Name ='demo';
    a.Industry = 'Education';
    a.BillingCountry = 'United Kingdom';
    insert a;
    
    Opportunity o = new Opportunity();
    o.AccountId = a.Id;
    o.StageName = 'Pipeline';
    o.Business_Unit__c = '1Spatial United Kingdom';
    o.Name = 'Test';
    o.Opportunity_Type__c = 'Contract Renewal';
    o.Contract_Type__c = 'Framework';
    o.Estimated_Value_Number__c = 10000;
    o.Opportunity_Submission_Date__c = Date.TODAY();
    o.CloseDate = Date.TODAY() +2;
    o.Probability__c = '80-99%';
    o.Purchase_Order__c = 'PO Pending';
    o.Solution_Summary__c = 'Hardware';
    o.Description = 'Renewal Contract for the next financial year';
    o.Opportunity_Process_Stage__c= 'Qualified Opportunity';
    insert o;
    
    String str = Opportunity_Approval_to_PrepBid.callApproval(o.Id);
    System.debug(str);
}
}

 
This was selected as the best answer
CushtyCushty
Excellent, thanks.  I ended up almost writing a huge test class for the entire approval process which didn't work, this helped me a lot....pour yourself an extra beer this evening