• CRNR
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 4
    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

We are integrating our ASP.NET applications with SF and the ASP.NET application is going to check to see if there are records in SF product table that have been modified and if so then update our database. We have created a workflow rule that sets "isUpdate" checkbox field if anything is updated on the record(s). Then our other application is setup to set the "isUpdate" to false after it comes through. The problem is that by virtual of changing "isUpdate" to false it is modifying the record and thus triggering our workflow rule again and setting it to true. We tried to handle it with an apex trigger but still were not able to get the logic right.

 

Any ideas?

 

 

trigger NRisUpdate on NRProducts__c (before update) {
    for (NRProducts__c r: Trigger.new) {
        NRProducts__c oldCase = Trigger.oldMap.get(r.ID);
       if (r.isUpdate__c != oldCase.isUpdate__c) {
       	       	if(oldCase.isUpdate__c == false){
            r.isUpdate__c = false;
        }
    }
}
}

 

 

  • May 08, 2011
  • Like
  • 0

We have the following wrapper class:

 

 

public class wrapperClassController {

    //Our collection of the class/wrapper objects cTest__c 
    public List<cProducts> productsList {get; set;}
    
    //This method uses a simple SOQL query to return a List of NRProducts__c
    public List<cProducts> getProducts(){
        if(productsList == null){
            productsList = new List<cProducts>();
        
            for(NRProducts__c c : [select Id, Name, Comments_to_Content_Team__c, Product_Description__c, Feature1__c, Feature2__c, Feature3__c, Feature4__c from NRProducts__c WHERE Feature1__c = null limit 100]){
                /* As each NRProducts__c is processed we create a new cProducts__c object and
                add it to the testProducts */
                productsList.add(new cProducts(c));
            }
        }
        return productsList;
    }
    
    public PageReference processSelected(){
        /*We create a new list of NRProducts__c that will be populated only with NRProducts__c
        if they are selected*/
        List<NRProducts__c> selectedProducts = new List<NRProducts__c>();
        
        /*We will cycle through our list of NRProducts__c and will check to see if the 
        selected property is set to true, if it is we add the NRProducts__c to the 
        selectedNRProducts__c list. */
        for(cProducts cCon : getProducts()){
            if(cCon.selected == true){
                selectedProducts.add(cCon.con);
            }
        }
        
        /* Now we have our list of selected NRProducts__c and can perform any type of 
        logic we want, sending emails, updating a field on the NRProducts__c, etc */

        for(NRProducts__c con : selectedProducts){
            system.debug(con);
update selectedProducts;
        }
        return null;
    }
    
    /* This is our wrapper/container class. A container class is a class, a data 
    structure, or an abstract data type whose instances are collections of other 
    objects. In this example a wrapper class contains both the standard salesforce 
    object NRProducts__c and a Boolean value */
    public class cProducts{
        public NRProducts__c con {get; set;}
        public Boolean selected {get; set;}
        
        /*This is the contructor method. When we create a new cNRProducts__c object we pass a 
        NRProducts__c that is set to the con property. We also set the selected value to false*/
        public cProducts(NRProducts__c c){
            con = c;
            selected = false;
        }
    }
}

 And the following page:

 

 

<apex:page controller="wrapperClassController">
    <apex:form>
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Process" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cAccount records -->
            <apex:pageBlockTable value="{!Products}" var="c" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!c.selected}"/>
                </apex:column>
                <!-- This is how we access the account values within our cAccount container/wrapper -->           
                <apex:column value="{!c.con.Name}" />

                         <apex:column headerValue="Feature1">             
                <apex:inputField value="{!c.con.Feature1__c}" />
                      </apex:column>
                         <apex:column headerValue="Feature2">             
                <apex:inputField value="{!c.con.Feature2__c}" />
                      </apex:column>
                         <apex:column headerValue="Feature3">             
                <apex:inputField value="{!c.con.Feature3__c}" />
                      </apex:column>
                          <apex:column headerValue="Feature4">             
                <apex:inputField value="{!c.con.Feature4__c}" />
                      </apex:column>
                 <apex:column value="{!c.con.Comments_to_Content_Team__c}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Everything works as expected when a field is updated, checkbox is checked, and "Process" button is clicked. Except....the table does not rerended. The result that is expected is that the record with thefield that was updated should no longer be in the list, because the Controller statement is looking for ones with this field == null.

 

Not sure what I am missing?

 

  • April 26, 2011
  • Like
  • 0

I have created a Trigger on our sandbox that works great when we upsert from the Data Loader:

 

trigger ProductTrigger on NRProducts__c (before update, before insert) {

for(NRProducts__c r : Trigger.new){

if(r.Name == null){

r.Name = 'NEWPRODUCT';

}

}

}

 

I have written the following test method:

 

public class ProductTrigger {

    static testMethod void myTest() {

 

     // Create that new product record.

     NRProducts__c r = new NRProducts__c(name='NEWPRODUCT');

     insert r;

     // Verify that the name field was set as "NEWPRODUCT".

     NRProducts__c insertNRProducts = [SELECT name FROM NRProducts__c WHERE Id = :r.Id];

     System.assertEquals('NEWPRODUCT', insertNRProducts.name);

}

}

 

It compiles, however when I ran test coverage it comes back as 66% with the following

 

trigger ProductTrigger on NRProducts__c (before update, before insert) {

for(NRProducts__c r : Trigger.new){

if(r.Name == null){

r.Name = 'NEWPRODUCT';

}

}

}

 

Any thoughts on how to make the test method complete?

  • April 23, 2011
  • Like
  • 0

We have a custom object NRProducts__c that is made up of all of our products. We want to be able to import a file update (via import wizard) to update inventory levels. The import file will not be based on the Name record, but rather an external number (Model_Number__c). Our goal is that if a new Model_Number exist in the import file that a new record is inserted with the default name "NEWPRODUCT". Below is the trigger that was created, but when we run an import with a new Model Number and Name as null/blank we get an email saying the record already exist (see below)

trigger ProductTrigger on NRProducts__c (before update, before insert) {
for(NRProducts__c r : Trigger.new){
if(r.Name == null && r.Name.length() <= 0){
r.Name = 'NEWPRODUCT';
}
}
}


Email from SF-
Force.com Sandbox

Alert: All information in the import file was already in salesforce.com.

Result: No data was created or modified in salesforce.com.

If you encounter any problems or have any questions, please contact us by clicking Help & Training at the top right of any salesforce.com page and choosing the My Cases tab.

Thank you!

Customer Support
salesforce.com


Any ideas how to make this work?

  • April 12, 2011
  • Like
  • 0

We have the following wrapper class:

 

 

public class wrapperClassController {

    //Our collection of the class/wrapper objects cTest__c 
    public List<cProducts> productsList {get; set;}
    
    //This method uses a simple SOQL query to return a List of NRProducts__c
    public List<cProducts> getProducts(){
        if(productsList == null){
            productsList = new List<cProducts>();
        
            for(NRProducts__c c : [select Id, Name, Comments_to_Content_Team__c, Product_Description__c, Feature1__c, Feature2__c, Feature3__c, Feature4__c from NRProducts__c WHERE Feature1__c = null limit 100]){
                /* As each NRProducts__c is processed we create a new cProducts__c object and
                add it to the testProducts */
                productsList.add(new cProducts(c));
            }
        }
        return productsList;
    }
    
    public PageReference processSelected(){
        /*We create a new list of NRProducts__c that will be populated only with NRProducts__c
        if they are selected*/
        List<NRProducts__c> selectedProducts = new List<NRProducts__c>();
        
        /*We will cycle through our list of NRProducts__c and will check to see if the 
        selected property is set to true, if it is we add the NRProducts__c to the 
        selectedNRProducts__c list. */
        for(cProducts cCon : getProducts()){
            if(cCon.selected == true){
                selectedProducts.add(cCon.con);
            }
        }
        
        /* Now we have our list of selected NRProducts__c and can perform any type of 
        logic we want, sending emails, updating a field on the NRProducts__c, etc */

        for(NRProducts__c con : selectedProducts){
            system.debug(con);
update selectedProducts;
        }
        return null;
    }
    
    /* This is our wrapper/container class. A container class is a class, a data 
    structure, or an abstract data type whose instances are collections of other 
    objects. In this example a wrapper class contains both the standard salesforce 
    object NRProducts__c and a Boolean value */
    public class cProducts{
        public NRProducts__c con {get; set;}
        public Boolean selected {get; set;}
        
        /*This is the contructor method. When we create a new cNRProducts__c object we pass a 
        NRProducts__c that is set to the con property. We also set the selected value to false*/
        public cProducts(NRProducts__c c){
            con = c;
            selected = false;
        }
    }
}

 And the following page:

 

 

<apex:page controller="wrapperClassController">
    <apex:form>
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Process" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cAccount records -->
            <apex:pageBlockTable value="{!Products}" var="c" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!c.selected}"/>
                </apex:column>
                <!-- This is how we access the account values within our cAccount container/wrapper -->           
                <apex:column value="{!c.con.Name}" />

                         <apex:column headerValue="Feature1">             
                <apex:inputField value="{!c.con.Feature1__c}" />
                      </apex:column>
                         <apex:column headerValue="Feature2">             
                <apex:inputField value="{!c.con.Feature2__c}" />
                      </apex:column>
                         <apex:column headerValue="Feature3">             
                <apex:inputField value="{!c.con.Feature3__c}" />
                      </apex:column>
                          <apex:column headerValue="Feature4">             
                <apex:inputField value="{!c.con.Feature4__c}" />
                      </apex:column>
                 <apex:column value="{!c.con.Comments_to_Content_Team__c}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Everything works as expected when a field is updated, checkbox is checked, and "Process" button is clicked. Except....the table does not rerended. The result that is expected is that the record with thefield that was updated should no longer be in the list, because the Controller statement is looking for ones with this field == null.

 

Not sure what I am missing?

 

  • April 26, 2011
  • Like
  • 0

I have created a Trigger on our sandbox that works great when we upsert from the Data Loader:

 

trigger ProductTrigger on NRProducts__c (before update, before insert) {

for(NRProducts__c r : Trigger.new){

if(r.Name == null){

r.Name = 'NEWPRODUCT';

}

}

}

 

I have written the following test method:

 

public class ProductTrigger {

    static testMethod void myTest() {

 

     // Create that new product record.

     NRProducts__c r = new NRProducts__c(name='NEWPRODUCT');

     insert r;

     // Verify that the name field was set as "NEWPRODUCT".

     NRProducts__c insertNRProducts = [SELECT name FROM NRProducts__c WHERE Id = :r.Id];

     System.assertEquals('NEWPRODUCT', insertNRProducts.name);

}

}

 

It compiles, however when I ran test coverage it comes back as 66% with the following

 

trigger ProductTrigger on NRProducts__c (before update, before insert) {

for(NRProducts__c r : Trigger.new){

if(r.Name == null){

r.Name = 'NEWPRODUCT';

}

}

}

 

Any thoughts on how to make the test method complete?

  • April 23, 2011
  • Like
  • 0

We have a custom object NRProducts__c that is made up of all of our products. We want to be able to import a file update (via import wizard) to update inventory levels. The import file will not be based on the Name record, but rather an external number (Model_Number__c). Our goal is that if a new Model_Number exist in the import file that a new record is inserted with the default name "NEWPRODUCT". Below is the trigger that was created, but when we run an import with a new Model Number and Name as null/blank we get an email saying the record already exist (see below)

trigger ProductTrigger on NRProducts__c (before update, before insert) {
for(NRProducts__c r : Trigger.new){
if(r.Name == null && r.Name.length() <= 0){
r.Name = 'NEWPRODUCT';
}
}
}


Email from SF-
Force.com Sandbox

Alert: All information in the import file was already in salesforce.com.

Result: No data was created or modified in salesforce.com.

If you encounter any problems or have any questions, please contact us by clicking Help & Training at the top right of any salesforce.com page and choosing the My Cases tab.

Thank you!

Customer Support
salesforce.com


Any ideas how to make this work?

  • April 12, 2011
  • Like
  • 0