• ClydeSavage
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies

I am new to test writing test methods and was hoping I could figure this one out on my own, but at some point I have to give in! I am getting 48% coverage on this controller. It is based on the dynamic search page provided by Jeff Douglas (http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/). For now I have  place the test method in the controller. Here is the controller with test method:

 

 

public with sharing class ProductSearchController {
 
  // the soql without the order and limit
  private String soql {get;set;}
  // the collection of products to display
  public List<NRProducts__c> products {get;set;}
 
  // the current sort direction. defaults to asc
  public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }
 
  // the current field to sort by. defaults to NRCode
  public String sortField {
    get  { if (sortField == null) {sortField = 'name'; } return sortField;  }
    set;
  }
 
  // format the soql for display on the visualforce page
  public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200'; }
    set;
  }
 
  // init the controller and display some sample data when the page loads
  public ProductSearchController() {
    soql = 'select Name, FBA_Total_Value__c, Total_Qty_Purchased__c,  Create_PO__c, isFBA__c, Vendors__c, VendorID__c, Total_Sold__c, FBA_Inventory__c, Open_Qty__c, Total_Qty__c, Inventory_Burn__c, FBA_Turn__c,   Reorder_Point__c, Reorder__c, Reorder_Amount__c, Reorder_Cost__c  from NRProducts__c where isFBA__c = True';
    runQuery();
  }
 
  // toggles the sorting of query from asc<-->desc
  public void toggleSort() {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query again
    runQuery();
  }
 
  // runs the actual query
  public void runQuery() {
 
    try {
      products = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }
 
  }
 
  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
    

    String name = Apexpages.currentPage().getParameters().get('name');
    String lastSold = Apexpages.currentPage().getParameters().get('lastSold');
    String vendors_id = Apexpages.currentPage().getParameters().get('vendors_id');
    String fbaInventory = Apexpages.currentPage().getParameters().get('fbaInventory');
    String openQty = Apexpages.currentPage().getParameters().get('openQty');
    String totalQty = Apexpages.currentPage().getParameters().get('totalQty');
    String inventoryBurn = Apexpages.currentPage().getParameters().get('inventoryBurn');
    String fbaTurn = Apexpages.currentPage().getParameters().get('fbaTurn');
    String reorderPoint = Apexpages.currentPage().getParameters().get('reorderPoint');
    String reorder = Apexpages.currentPage().getParameters().get('reorder');
    String reorderAmount = Apexpages.currentPage().getParameters().get('reorderAmount');
    String reorderCost = Apexpages.currentPage().getParameters().get('reorderCost');    
                      

 
    soql = 'select Name, isFBA__c, Total_Qty_Purchased__c, FBA_Total_Value__c, Create_PO__c, Vendors__c, VendorID__c, Total_Sold__c, FBA_Inventory__c, Open_Qty__c, Total_Qty__c, Inventory_Burn__c, FBA_Turn__c,   Reorder_Point__c, Reorder__c, Reorder_Amount__c, Reorder_Cost__c  from NRProducts__c where isFBA__c = True';

    if (!name.equals(''))
      soql += ' and name LIKE \''+String.escapeSingleQuotes(name)+'%\'';
    if (!vendors_id.equals(''))
          soql += ' and VendorID__c LIKE \''+String.escapeSingleQuotes(vendors_id)+'%\'';
   if (!lastSold.equals(''))
      soql += ' and Total_Sold__c >= '+lastSold+'';
         if (!fbaInventory.equals(''))
      soql += ' and FBA_Inventory__c >= '+fbaInventory+'';
         if (!openQty.equals(''))
      soql += ' and Open_Qty__c >= '+openQty+'';     
         if (!totalQty.equals(''))
      soql += ' and Total_Qty__c >= '+totalQty+'';     
         if (!inventoryBurn.equals(''))
      soql += ' and Inventory_Burn__c >= '+inventoryBurn+'';      
         if (!fbaTurn.equals(''))
      soql += ' and FBA_Turn__c >= '+fbaTurn+'';      
         if (!reorderPoint.equals(''))
      soql += ' and Reorder_Point__c >= '+reorderPoint+'';      
          if (!reorder.equals(''))
          soql += ' and Reorder__c LIKE \''+String.escapeSingleQuotes(reorder)+'%\'';       
         if (!reorderAmount.equals(''))
      soql += ' and Reorder_Amount__c >= '+reorderAmount+'';      
          if (!reorderCost.equals(''))
      soql += ' and Reorder_Cost__c >= '+reorderCost+'';
    // run the query again
    runQuery();
 
    return null;

}

    ///////////////////////////////////////////////
    // 
    // Unit Tests
    //
    ///////////////////////////////////////////////
    
    public static testMethod void testController()
    {

        
        // instantiate the controller
        ProductSearchController ctrl=new ProductSearchController();

       // instantiate the controller
        //ctrl.runSearch();
        System.assert(ctrl.runSearch() == null);

    }
}

 I have color coded based on what the testing shows. Blue = coverage, Red = no coverage.

 

Thank you for your help!

 

 

 

 

  • May 24, 2011
  • Like
  • 0

Hello Everyone,

 

I'm trying to make some files we uploaded into salesforce crm content downloadable or accessible by another page via Sites. For instance, say, an mp3, either downloaded or requested by mediaplayer on a webpage.

 

According to the docs 'To download a document via the API, you must export the VersionData of the document.'

 

Well... I've tried a couple variations with a visualforce page to retrieve/export the contentversion, but (as expected) no luck.

 

Does anyone have any good ideas on how to do this?

 

Thanks in advance,

e.