0

I am creating a @testClass for an Apex Trigger I wrote with the intent to automatically set an Opportunity Checkbox field to False, if the Opportunity was cloned. (The Else statement also defaults the Checkbox field to False)

I updated my core code to be up-to-date with my latest issues

    //Test class for testing the Opportunity isClone Apex trigger
    @isTest 
    public class testOppIsClone {
    static testMethod void insertNewOpp() {

    Account testAccount = new Account();
    testAccount.Name = 'TrylerTest';
    insert testAccount;


    Opportunity newTestOpp = new Opportunity();

    //setting isClone function to true
    newTestOpp.isClone();

    //setting all Required fields
    newTestOpp.Name = 'Trigger testISCLONE Opp';
    newTestOpp.Sales_Owner_Field_Updater__c = 'Owner';
    newTestOpp.AccountId = testAccount.ID;
    newTestOpp.Primary_Media_Opportunity__c = 'El Primo';
    newTestOpp.CloseDate = date.parse(2015, 10, 12);
    newTestOpp.StageName = 'Closed Won';
    newTestOpp.Type = 'New Business';

    newTestOpp.Media_Placement__c = 'Podcasts';


    //create the Test Class!
    insert newTestOpp;
    Opportunity clonedTestOpp = newTestOpp.clone();


    clonedTestOpp.isClone();


    //now asserting that Media Plan Attached field is set to False
    System.assertEquals(false, clonedTestOpp.Media_Plan_Attached__c);

}  }  

Note: I tried both the Id string in primes '' and just using newTestOpp.Account, both of which returned this error.

What data type should I use to successfully link this test opportunity with the Account Name (Lookup)?

Update3: I rolled all the updated fixes into the original. My latest issue now is correctly entering in the 'Date value.'

Salesforce documentation is a little sparse about how to correctly input a Date value. Here is what I have:

newTestOpp.CloseDate = (2015, 10, 12);

However, this error is being returned

Error: Compile Error: expecting a right parentheses, found ','

Anyone know the correct format for inputting a Date value?

2
  • 1
    You did not use the code as posted in the answer: newTestOpp.Account = testAccount.ID; should be newTestOpp.AccountID = testAccount.ID; - Account is an Object, AccountID is the ID
    – Eric
    Nov 20, 2015 at 19:17
  • @Eric thanks Eric, I caught that error. I will update the core code in the OP again.
    – GPP
    Nov 20, 2015 at 19:51

1 Answer 1

2

Morning,

You should never hard code IDs into your test classes, create a test account within the class itself.

public class TestCloneOpp
{
Static testMethod void TestCloneOppTrigger(){
        Account testAccount = new Account();
        testAccount.Name = 'Test Account';
        insert testAccount;

    Opportunity newTestOpp = new Opportunity();

    //setting isClone function to true
    newTestOpp.ISCLONE();

    //setting all Required fields
    newTestOpp.Name = 'Trigger testISCLONE Opp';
    newTestOpp.The_Owner_Field_Updater__c = 'Owner';
    newTestOpp.AccountId = testAccount.ID;
    newTestOpp.Primary_Media_Opportunity__c = 'El Primo';
    newTestOpp.CloseDate = '10/12/2015';
    newTestOpp.StageName = 'Closed Won';
    newTestOpp.Type = 'New Business';

    newTestOpp.Media_Placement__c = 'Media Type';


    //create the Test Class!
    insert newTestOpp;

    //now asserting that Media Plan Attached field is set to False
    System.assertEquals(False, newTestOpp.Media_Plan_Attached__c);
} 
5
  • 1
    ? isClone() returns a Boolean and does nothing in this testmethod (I realize you just copied from OP)
    – cropredy
    Nov 20, 2015 at 17:32
  • @crop1645 I tried to assign this method a 'True' statement like the following: newTestOpp.isClone() = TRUE; But that returned an Error as well. I will try again.
    – GPP
    Nov 20, 2015 at 17:53
  • 1
    @GPP - the isClone() will only return true if the object was cloned - hence to simulate this, you can apply the clone() method against some mocked Opportunity to return a cloned Opportunity object
    – cropredy
    Nov 20, 2015 at 18:24
  • Thanks for the tip. Would this method do the trick: Opportunity clonedTestOpp = newTestOpp.clone();
    – GPP
    Nov 20, 2015 at 18:37
  • Since @CaptainHaz answered this thread's core question, I am marking as answered. I still have questions related to why the Test Class is failing its Run Time, but that is a separate thread. Thanks so much for all the help and feedback!
    – GPP
    Nov 21, 2015 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .