-1

Afternoon all,

Am trying to write my first complex trigger to auto generate opportunities on activation of a contract but my test class keeps failing. I recognise the error as indicating there is nothing in the list to insert but I can't figure out where the issue is. Can anyone point me in the right direction?

Trigger

trigger CreateContractTrigger2YearDealTerm on Contract (after insert, after update) {
    List <Opportunity> listOppor = new list <Opportunity>();
    for (Contract con : Trigger.new) {
{

   if(con.RPI__c == 'Yes' && con.ContractTerm == 24 && con.Status == 'Activated' && con.Triggers_Auto_Fired__c == FALSE && con.RPI_Trigger_Frequency__c == 'Annually')
       {
            Opportunity oppNew = new Opportunity ();
          oppNew.Type = 'Incremental Contract Trigger';

          oppNew.StageName = '10% - Gathering Info/Preparing Solution';
          oppNew.Name = con.Account +' - ' + con.Highlevel_Product_Type__c + ' - ' + oppNew.Type;
           oppNew.Service_Live_Date__c = con.StartDate.addYears(1);
           oppNew.CloseDate = con.StartDate.addDays(364);
           oppNew.Highlevel_Product_Type__c = con.Highlevel_Product_Type__c;
           oppNew.Trigger_Type__c = 'Retail Price Index';


          listOppor.add(oppNew);
       }


   if(con.PPUP_Mechanism__c == 'Yes' && con.ContractTerm == 24 && con.Status == 'Activated' && con.Triggers_Auto_Fired__c == FALSE && con.PPUP_Trigger_Frequency__c == 'Annually')
       {
            Opportunity oppNew = new Opportunity ();
          oppNew.Type = 'Incremental Contract Trigger';

          oppNew.StageName = '10% - Gathering Info/Preparing Solution';
          oppNew.Name = con.Account +' - ' + con.Highlevel_Product_Type__c + ' - ' + oppNew.Type;
           oppNew.Service_Live_Date__c = con.StartDate.addYears(1);
           oppNew.CloseDate = con.StartDate.addDays(364);
           oppNew.Highlevel_Product_Type__c = con.Highlevel_Product_Type__c;
           oppNew.Trigger_Type__c = 'Price Per Usage Point';


          listOppor.add(oppNew);
       }
    if(listOppor.size() > 0)
     insert listOppor;    

}
}
}

Test Class

@isTest

public class TestCreateOppsOnContracts24Months
{
Static testMethod void TestCreateOppsOnContracts24Months(){
    Account a = [select Id from Account limit 1];
    Contract c = new Contract();

    c.Accountid = a.id;
    c.Status = 'Activated';
    c.Contract_Sponsor__c = 'Craig Coates';
    c.StartDate = date.today();
    c.Contract_Type__c = 'IAR';
    c.ContractTerm = 24;
    c.Business_Division__c = 'Commercial Team';
    c.EndDate = c.StartDate.addYears(2);
    c.Pricing_Signed_off_by__c = 'Rate Card';
    c.Margin__c = 0.1;
    c.RPI__c = 'Yes';
    c.RPI_Description__c = 'Test RPI Description';
    c.RPI_Trigger_Frequency__c = 'Annually';
    c.PPUP_Mechanism__c = 'Yes';
    c.PPUP_Mechanism_Description__c = 'Test PPUP Description';
    c.PPUP_Trigger_Frequency__c = 'Annually';
    c.Triggers_Auto_Fired__c = FALSE;
    insert c;

    }
}

My Error Message

Time Started 06/10/2015 14:12 Class TestCreateOppsOnContracts24Months Method Name TestCreateOppsOnContracts24Months Pass/Fail Fail Error Message System.QueryException: List has no rows for assignment to SObject Stack Trace Class.TestCreateOppsOnContracts24Months.TestCreateOppsOnContracts24Months: line 6, column 1

Thanks

UPDATE

@isTest

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

Contract c = new Contract();
c.AccountID = testAccount.Id;
c.Status = 'Draft';
c.Contract_Sponsor__c = 'Craig Coates';
c.StartDate = date.today();
c.Contract_Type__c = 'IAR';
c.ContractTerm = 24;
c.Business_Division__c = 'Commercial Team';
c.EndDate = c.StartDate.addYears(2);
c.Pricing_Signed_off_by__c = 'Rate Card';
c.Margin__c = 0.1;
c.RPI__c = 'Yes';
c.RPI_Description__c = 'Test RPI Description';
c.RPI_Trigger_Frequency__c = 'Annually';
c.PPUP_Mechanism__c = 'Yes';
c.PPUP_Mechanism_Description__c = 'Test PPUP Description';
c.PPUP_Trigger_Frequency__c = 'Annually';
c.Triggers_Auto_Fired__c = FALSE;
insert c;

}

}

My only issue now is that it isn't triggering as the status is incorrect. Got any ideas on how I can insert an Activated contract for the test?

  • Christoph answered your question, but your test is still failing. In your updated test, you're setting c.Status = 'Draft'; instead of c.Status = 'Activated'; but your trigger only fires when Status is Activated. Is there a reason you are setting the status to draft? – Christian Carter Oct 6 '15 at 14:38
  • Because when I tried to insert the Status as Activated I got the below error. System.DmlException: Insert failed. First exception on row 0; first error: INVALID_STATUS, invalid status: – CaptainHaZ Oct 6 '15 at 14:50
  • 1
    Please mark as answered and start a new question. Changing your original question when it was answered and asking a new on makes it difficult for others with similar issue to find an answer – Eric Oct 6 '15 at 16:05
  • Apologies, still quite new to this. – CaptainHaZ Oct 7 '15 at 9:18
2

by default, whenever you run your tests, they do not have access to any data in your org. That said, your query

Account a = [select Id from Account limit 1];

Returns a list of size 0, which cannot be assigned to your variable "a". To avoid this kind of error, you could employ something like

List<Account> accounts = [select Id from Account limit 1];
System.assertEquals(1, accounts.size()); // This will fail your test if there are no accounts
Account a = accounts.get(0);

To actually have test data to run your tests with, you either need to create it, or allow your tests to access your production data by adding the "(SeeAllData=true)" annotation to your test class/method (you should not do this, if you don't have to).

To create test data, you just insert it before your run your test, for your example:

@isTest

public class TestCreateOppsOnContracts24Months
{
Static testMethod void TestCreateOppsOnContracts24Months(){
    Account tmp = new Account(Name = 'ACME');
    insert tmp;
    Test.startTest();

    Account a = [select Id from Account limit 1];
    Contract c = new Contract();

    c.Accountid = a.id;
    c.Status = 'Activated';
    c.Contract_Sponsor__c = 'Craig Coates';
    c.StartDate = date.today();
    c.Contract_Type__c = 'IAR';
    c.ContractTerm = 24;
    c.Business_Division__c = 'Commercial Team';
    c.EndDate = c.StartDate.addYears(2);
    c.Pricing_Signed_off_by__c = 'Rate Card';
    c.Margin__c = 0.1;
    c.RPI__c = 'Yes';
    c.RPI_Description__c = 'Test RPI Description';
    c.RPI_Trigger_Frequency__c = 'Annually';
    c.PPUP_Mechanism__c = 'Yes';
    c.PPUP_Mechanism_Description__c = 'Test PPUP Description';
    c.PPUP_Trigger_Frequency__c = 'Annually';
    c.Triggers_Auto_Fired__c = FALSE;
    insert c;
   Test.stopTest();
    }
}

Alternatively you can write a test setup method, that's executed before the first test method.

  • Thanks Christoph. I started comparing to my previous test classes and spotted the error re: the account. I ended up going with the update abobe instead which passed. – CaptainHaZ Oct 6 '15 at 14:25

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.