+ Start a Discussion
devsalesforce27devsalesforce27 

Help:Display Error on visualforce page depending upon checkbox

Hey All  ,

 

I have a visualforce page which allows the user to enter a part number in the UI and click a submit button.   The system then goes to the compliance part object and uses data from that record to generate some compliance certificates (.pdf) that the user can then print.

 

I’d like  to update this to interrupt this process:   Instead of simply generating and displaying the certificate back to the user, I’d like the application to first check for the  “Reviewed_By_Compliance_Team__c”(its a checkbox on part_compliance__c object)  on the part compliance record associated with the part number entered by the user.   If FALSE,  display a message saying.   “Under Review:Please check back in 7 days”.And , It should not display copliance Certificate page.

 

Below is the Apex class corresponding to the visulaforce page:-

public with sharing class ProductComplianceRequestController {

  public String partNumber {get;set;}

  public ProductComplianceRequestController() {

  }
  
  public PageReference requestCompliance() {
    
    list<Product2> part = [Select Id From Product2 Where Part_Number__c =: partNumber];
    
    if(part.size() == 0) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'Part not found.'));
      return null;
    }
    return new Apexpages.Pagereference('/apex/ProductComplianceCertificate?id=' + part[0].Id);
  }
  
  static testMethod void testRequest() {
    
    Product2 testPart = new Product2();
    testPart.Part_Number__c = 'E000000';
    testPart.Name = 'E000000';
    insert testPart;
    
    ProductComplianceRequestController controller = new ProductComplianceRequestController();
    
    controller.partNumber = testPart.Part_Number__c;
    
    test.startTest();
      String resultURL = controller.requestCompliance().getUrl();
    test.stopTest();
    
    System.assertEquals('/apex/ProductComplianceCertificate?id=' + testPart.Id, resultURL);
  }
  
  static testMethod void testRequestNotExist() {
    
    Product2 testPart = new Product2();
    testPart.Part_Number__c = 'E000000';
    testPart.Name = 'E000000';
    insert testPart;
    
    Pagereference pageRef = Page.ProductComplianceRequest;
    Test.setCurrentPage(pageRef);
      
    ProductComplianceRequestController controller = new ProductComplianceRequestController();
    
    controller.partNumber = 'E000001';
    
    test.startTest();
      controller.requestCompliance();
    test.stopTest();
        
    System.assertEquals(ApexPages.getMessages().size(), 1);
  }
}