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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Test class for custom object approval

Hi, 

  We have approvals on custom object based some conditions we are displaying message once the conditions are met approval is fired. below is the class written Need suggestion to write test class for below controller please suggest. 
 
public class  check_spr_coterm
{
 
 Integer Olicheck;
 Integer Athcheck;
 String PageID; 
 String P_SPR_Approver_Director;
 String P_SPR_Approver_RVP;
 String P_SPR_Approver_GVP;
 String P_SPR_Approver_EVP;  
 
 public check_spr_coterm()
 {
   SPR__c SP;
   //OpportunityLineItem OLI;
    
   SP = [select id,Opportunity__c from SPR__c 
          where Id = : ApexPages.currentPage().getParameters().get('id') limit 1];
   
   PageID = ApexPages.currentPage().getParameters().get('id');
   
  List<SPR__c> SPRO = new List<SPR__c>();

   SPRO = [select id,SPR_Approver_Director__c,SPR_Approver_RVP__c,SPR_Approver_GVP__c,SPR_Approver_EVP__c from SPR__c 
           where 
           id = :ApexPages.currentPage().getParameters().get('id') and
           ( SPR_Approver_Director__c = null or SPR_Approver_RVP__c = null or 
             SPR_Approver_GVP__c = null or SPR_Approver_EVP__c = null ) limit 1 ];
             
      if(!SPRO.isEmpty()) // check if list is not empty
        {       
       P_SPR_Approver_Director = SPRO[0].SPR_Approver_Director__c;
       P_SPR_Approver_RVP = SPRO[0].SPR_Approver_RVP__c;  
       P_SPR_Approver_GVP = SPRO[0].SPR_Approver_GVP__c;
       P_SPR_Approver_EVP = SPRO[0].SPR_Approver_EVP__c;
        }
      else
      {
        P_SPR_Approver_Director = 's';
        P_SPR_Approver_RVP =  's';  
        P_SPR_Approver_GVP =  's';
        P_SPR_Approver_EVP =  's';
       }     
     
   List<AggregateResult> OLI = [select count(id) olicnt from OpportunityLineItem 
                                where opportunityid = :SP.Opportunity__c and name like '%COTERM%' ];
    
   List<AggregateResult>  ATH = [select count(id) athcnt from  Attachment 
                                 where parentid = :ApexPages.currentPage().getParameters().get('id')];  
 
   Olicheck = (Integer)OLI[0].get('olicnt');
   Athcheck = (Integer)ATH[0].get('athcnt');  
     
 }
 public void pageAction(){
 
     if ( P_SPR_Approver_Director == NULL || P_SPR_Approver_RVP == NULL || P_SPR_Approver_GVP == NULL || P_SPR_Approver_EVP == NULL  )
     {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info,'Your SPR approvers have not been set up, please contact the SFDC Administrator @ SFDC@fortinet.com'));  
     }
 
     else if( Olicheck > 0 && Athcheck ==0) // COTERM product exist and attached is missing this will fire
     {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info,'An RTS Quote is required for the COTERM product.Please attach quote(s) and then resubmit your SPR for approval'));     
     }
     
     else if ( P_SPR_Approver_Director <> NULL && P_SPR_Approver_RVP <> NULL && P_SPR_Approver_GVP <> NULL && P_SPR_Approver_EVP <> NULL  )
     {
       Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
       req.setObjectId(PageID);
       Approval.ProcessResult result = Approval.process(req);
       ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info,'Sucessfully submitted for approval'));
       redirectpage();
      }
     
 }
 
 
 public PageReference redirectpage()
 {
   PageReference pageRef = new PageReference('/' + PageID);
   pageRef.setRedirect(true);
   return pageRef;
 }
 
 
}

Visualforce code
<apex:page controller="check_spr_coterm" action="{!pageAction}">
<apex:pageMessages />
<apex:form >
        <apex:commandButton value="Ok" action="{!redirectpage}" oncomplete="abc();"/>
    </apex:form>
<script>
function abc() {
    window.location = 'https://www.google.com';
}
</script>
</apex:page>

 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Experts,


 Wrote below class i dont see any code coverage still please suggest me
 
@isTest(seealldata=true)
public class  test_check_spr_coterm
{
   static testMethod void test_check_spr_coterm(){
   
   SPR__c SPR = new  SPR__c(Opportunity__c='00622000001uiKv',Partner_Account__c='0018000000pwEyq',Distributor__c='a0N8000000Aiif0',Reason_Needed__c='Budget Constraints'); 
   
   insert SPR;
     
  SPR__c SPR1 = [SELECT Id, CreatedDate FROM SPR__c WHERE Id = :SPR.id];
  
  
  if([select count() from ProcessInstance where targetobjectid=:SPR1.id] < 1)
        {       
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Approve.');
            req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
            req.setObjectId(SPR1.Id);

            //Submit the approval request
            Approval.ProcessResult result = Approval.process(req);

        }
        
   }     

}

Thanks
Sudhir