Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm confused by this. When I was using a dev account it allowed me to create classes in Apex Class like this and deploy it with no problem, but now that I'm using our real account it won't let me upload the code and insist that I have to test it. I know how to test triggers, but I really don't know how tests work for something like this.

public with sharing class ListSalesByTerritory
    //Real Code starts here
    public String terr { get; set;}
    List<Sales__c> sal;    

    public void search()
        terr = String.escapeSingleQuotes(terr);
        string searchquery = 'SELECT Order_Number__c,, Territory__c FROM Sales__c WHERE Territory__c LIKE \'' + terr + '%\'';
        sal = Database.query(searchquery);

    public List<Sales__c> getSal()
        return sal;

I'm only using this for a VisualForce Page so that our sales guys can specify which territory they want to see (input field) It works in the Dev account, but I can't even create new class in our real account. I had to use eclipse to upload it(may be this is the reason?) and it won't upload because it hasn't been tested... but how would you test something like this?

share|improve this question
up vote 4 down vote accepted

The process for all tests is essentially the same, regardless of if it is a trigger or a class.

  1. Set up your test data
  2. Instantiate your class and call it's methods / use DML to invoke your trigger
  3. Check the results

A very basic test method for this class would look something like this:

private static void searchTest()
    // Set up test data
    Sales__c sales = new Sales__c();
    sales.terr = 'Test Territory';
    // Populate fields...
    insert sales;

    // Instantiate class and call it's methods
    ListSalesByTerritory salesTerritoryList = new ListSalesByTerritory();
    salesTerritoryList.terr = 'Test';;

    // Check the results
    List<Sales__c> salesList = salesTerritoryList.getSal();
    System.assert(1, salesList.size());

You will want to improve this by inserting more Sales__c records to start with and checking the correct ones are returned.

You will also want to add more test methods to your test class to test situations such as where there are no Sales__c objects that meet the criteria and to test that String.escapeSingleQuotes() is causing your query to give you the results you expect.

share|improve this answer

Your test inserts an Account and a few Sales__c objects then creates a new instance of ListSalesByTerritory, sets a value in the terr field then invokes the search method then checks that what getSal returns is what is expected.

See e.g. Testing Custom Controllers and Controller Extensions or An Introduction to Apex Code Test Methods.

share|improve this answer
I have actually been trying to do that, but when ever I try ListSalesByTerritory lst = new ListSalesByTerritory(); lst.terr = 'ORD1'; I get an error for lst.terr does not exists for some reason – Nataku Jul 14 '14 at 16:50
@Nataku I suggest you post an exact copy of what you have tried because I would expect that to work... – Keith C Jul 14 '14 at 17:06
i think i know what's going on, eclipse isn't uploading ListSalesByTerritory because its not tested and my test classes are seeing the blank one that is on Salesforce, I think my connection isn't using the Sandbox. let me poke around some more in a minute and get back to you. Our network connection is getting hammered right now, I need to figure out what's going on there first, Thanks, – Nataku Jul 14 '14 at 18:27
@Nataku Yeah you need to get everything working in a sandbox or dev org and then deploy all the code (controllers, triggers etc) and their tests together. – Keith C Jul 14 '14 at 18:33
it works now, I didn't notice my IDE never saved to use the Sandbox, instead it was trying to use production/developer edition the whole time :( your comment and @Alex Tennant's definitely helped me avoid a lot of coding errors after I fixed that little problem though, Thanks :) – Nataku Jul 14 '14 at 21:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.