I've got a class that generates a new opportunity record. I want to verify that the opportunity was generated in some instances but not in others.

I'm new to apex, how would I go about getting that?

Test Class:

public class OpportunityTestClass {

    static testMethod void renewInvalidOpportunity(){

        date dateClose = date.parse('1/1/2016');

        Opportunity opp = new Opportunity(
            Name='Renewal 1', StageName = 'Negotiation/Review',
            CloseDate = dateClose,Amount = 10000);

        opp.AccountId = '00137000002h6VpAAI';
        insert opp;
        opp.StageName = 'Closed Won';
        update opp;
  • Do you mean 'instances' == salesforce orgs? (i.e. na15, na1, etc). If so, then I understand you have a class that you deploy in each instance and you run the method that generates the opportunity, is that correct? – Joca Jul 20 '15 at 1:33
  • You have a test class here, but what code are you testing? Is there a class you are testing, a workflow rule you want to test, etc. Please post what you are actually testing. At this point, all your test code is doing is inserting an opportunity without any context as to what you are trying to test. – Eric Jul 20 '15 at 1:34
  • @JoseCarlos, I mean situations, not instances. – shartshooter Jul 20 '15 at 4:56
  • @Eric You're correct, my test code is inserting an opportunity, my real code is generating an additional opportunity. How would I get the information about that newly generated opportunity? – shartshooter Jul 20 '15 at 4:57
  • You would query for it. Without knowing what your other code is there is not much else we can do as it would all depend on your other code – Eric Jul 20 '15 at 13:07

Use something like

System.assert(op.Id != null) in case when you want to check whether opportunity instance is generated and

System.assert(op.id ==null) in case of Opportunity instance not being generated

| improve this answer | |
  • Thanks, what would be the syntax of the new opportunity? You used op.id I have opp is that a typo? – shartshooter Jul 20 '15 at 4:55
  • Yes thats a typo – Goki Jul 20 '15 at 4:58
  • what do I need to do to get the new record that is generated by my controller class? – shartshooter Jul 20 '15 at 5:02
  • 1
    I am confused. What do u mean by controller class, are you referring to the test class which you have written? In that case the insert pop statement would set the Id value after successful insertion. If you want to retrieve all the values of the Opportunity created, you can retrieve it by SOQL. – Goki Jul 20 '15 at 5:04
  • 2
    The best bet is writing SOQL, as you know the logic of opportunityController.insertRenewal. You can try retrieving the record which you are looking for. If the query returns data, then you know it got created else not. Make sure to introduce the proper conditions in the query to identify and retrieve the created renewal opportunity. Don't forget to vote for the answer, if this helped. – Goki Jul 20 '15 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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