function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MaxaMaxa 

Trigger test method

HI,

I have a triger that creates new task when certain fields in opportunity are changed,

however ic annto deplaoy it because i have no test method.

I have been looking thru the posts here and to be honest i dont get it, in normal apex test method is withing he code, but for trigger its not?

in sll samples i saw there is no reffernce to the trigger or maybe i dont undestand how it suppose to be refferenced.

Here is my trigger below, cna anyone please help me out with a simpe test

trigger assignTask on Opportunity (after update) { List<Task> task = new List<Task>(); List<Opportunity> newOpportunity = new List<Opportunity>(); for (Integer i = 0; i < Trigger.new.size(); i++) { if ((Trigger.new[i].Type == 'New Business')&&(Trigger.new[i].CC_Co_ordinator__c !=null)&&(Trigger.new[i].Finance_Status__c =='Pre-Approved')&&(Trigger.new[i].Optimised_Date__c ==NULL)&& (Trigger.new[i].First_Contact_task_created__c != True)) { System.debug('triggerfire'); task.add(new Task( WhatId = Trigger.new[i].Id, OwnerId=Trigger.new[i].CC_Co_ordinator__c, Subject='First Contact ', ActivityDate = System.today() ) ); } } insert task; }

 

Best Answer chosen by Admin (Salesforce Developers) 
JonSimmonsJonSimmons

Is the above code the actual test or just part of it?  I don't see any actual test, just creating an opportunity.  Once you create the opportunity you then have to test to see if your trigger ran correctly.

 

In your case you would create your opportunity, change it appropriately, then search to see if your task was created assert the result.

 

 

 here is a bit of code from one of my test classes, in this part I create a contact and then test to ensure that it was created correctly.  In your case you would create an opportunity but then test to see if a task was created correctly.  

 

 

        //create a contact

        Contact newContact = new Contact();
        newContact.firstname = 'Jane';  //first
        newContact.lastname = 'Smith';  //and last
        newContact.recordtypeid = theRecordType.id;
        //newContact.nickname = newUser.CommunityNickname;
        newContact.accountid = theAccount.id;
        newContact.contact_type__c = 'Employee';
        newContact.leadsource = 'Other';
        newContact.currencyisocode = 'USD';  
        newContact.eMail = 'jane@smith.com';
        newContact.phone = '617-555-1212';
        newContact.Employee_UserId__c = thisUser.id;
        newContact.ownerid = thisUser.id;
        
        insert newContact;      
        
        System.Debug('Check to see if our test Contact is created');


        //test to verify that the new contact has been created
        integer theNewContactCount1 = [select count() from Contact where firstname = :newContact.firstname and lastname = :newContact.lastname and accountid = :theAccount.id];


        if (theNewContactCount1 > 0)
        {
            System.assert(true);
        }
        else
        {
            System.assert(false);  //false means the contact has not been created
        }

All Answers

JonSimmonsJonSimmons

 

 Your test method will go into an APEX class and it doesn't have to call the trigger, so long as the code will cause the trigger to be called.  Often the code that does all the work for a trigger is put into a class of it's own, and the trigger simply calls methods out of that class.  I usually create my triggers with the bulk of the code in a class, and the test methods in the same class, just to keep it all in one place.

 

 

Your trigger creates a task on opportunity update, so your test class will have to create an opportunity, change the opportunity, then test to see if the proper task was created.  You should test to ensure that your trigger runs when the right fields are changed and doesn't run when the right fields are not changed.

 

 

MaxaMaxa

Thanks for explanation, i get it now, i have different problem now, i have created thsi test method

@isTest private class TestNewTask { static TestMethod void testassignTaskTrigger() { Opportunity o = new Opportunity((Opportunity.Type == 'New Business')&&(Opportunity.CC_Co_ordinator__c =='Max Alexander')&&(Opportunity.Finance_Status__c =='Pre-Approved')&&(Opportunity.First_Contact_task_created__c == False)); insert o; } }

and i get strange eroro that i cannot find on any posts

Save error: SObject constructor must use name=value pairs

 

what deos that actaully mean

MaxaMaxa

i found the problem and fixed it, ned to use commas :)

another proble is that it still gives me error that 0% tested, must be at leas 1%

why? any ideas? it create opportuntiy with the fields that must trigger the trigger

JonSimmonsJonSimmons

Is the above code the actual test or just part of it?  I don't see any actual test, just creating an opportunity.  Once you create the opportunity you then have to test to see if your trigger ran correctly.

 

In your case you would create your opportunity, change it appropriately, then search to see if your task was created assert the result.

 

 

 here is a bit of code from one of my test classes, in this part I create a contact and then test to ensure that it was created correctly.  In your case you would create an opportunity but then test to see if a task was created correctly.  

 

 

        //create a contact

        Contact newContact = new Contact();
        newContact.firstname = 'Jane';  //first
        newContact.lastname = 'Smith';  //and last
        newContact.recordtypeid = theRecordType.id;
        //newContact.nickname = newUser.CommunityNickname;
        newContact.accountid = theAccount.id;
        newContact.contact_type__c = 'Employee';
        newContact.leadsource = 'Other';
        newContact.currencyisocode = 'USD';  
        newContact.eMail = 'jane@smith.com';
        newContact.phone = '617-555-1212';
        newContact.Employee_UserId__c = thisUser.id;
        newContact.ownerid = thisUser.id;
        
        insert newContact;      
        
        System.Debug('Check to see if our test Contact is created');


        //test to verify that the new contact has been created
        integer theNewContactCount1 = [select count() from Contact where firstname = :newContact.firstname and lastname = :newContact.lastname and accountid = :theAccount.id];


        if (theNewContactCount1 > 0)
        {
            System.assert(true);
        }
        else
        {
            System.assert(false);  //false means the contact has not been created
        }

This was selected as the best answer
MaxaMaxa

its working now i managed to deploy and it works fine in SF , thank you for your help