+ Start a Discussion
msimondsmsimonds 

Need help with testing for a trigger

ALL,

 

I am not new to developing on Salesforce using the API and PHP, but  I am new to APEX so please bare with me.

 

I wrote this simple trigger that deletes records on a custom object when another custom object record is deleted.

 

 

trigger CleanOppTrackerLineItem on Opportunity_Trackers__c (before delete) { for (Integer i = 0; i < Trigger.old.size(); i++) { try { //Get all the line items to delete when the parent is removed Opportunity_Tracker_Products__c[] oTRs = [select id from Opportunity_Tracker_Products__c where Opportunity_Tracker__c = :trigger.old[i].id]; delete oTRs; } catch (System.QueryException ex) { //Do Nothing - There must not have been any to delete. } } }

 

 I was trying to go over some of the examples so I can test this and I am kind of lost. 

 

 

Do I need to write a custom APEX class to test this trigger because I need to get it into production as fast as possible.

 

We have run some tests in our sandbox on the trigger and it works perfectly.

 

Can someone please help me with what is the simplest way to create a test case and get it tested for this very simple trigger

 

 

I would appreciate it

 

~Mike

 

DFDF

The response I wish I had gotten a couple of weeks ago when I was facing the same issue....

 

 A test case is basically an apex class that executes a series of code, that in effect "simulates" what would happen in salesforce. The object of these simulations is to ensure that every line of your code(test coverage = 100%) gets hit in each of these cases. 

 

So as an example. Lets say your were testing a trigger for a task..you would want your test class do something like

 

public class testtasktolead{
   
    static testMethod void testTtoL() {

    Task tsk5 = new Task(Subject='Call',Call_Success__c='Busy/No Answer',whoId=test_lead.Id);
        insert tsk5;
       
        Task tsk7 = new Task(Subject='Call',Call_Success__c='Busy/No Answer',whoId=test_lead.Id);
        insert tsk7;

   }

}

 

Assuming my trigger acted on these events, It would be called on each new creation of a task. Assuming the line of code that is affected by the test class is hit and works, that adds to your test coverage %. 

 

In your case, you would want to create a test class that creates a new Opportunity_Tracker and then delete it. In doing so, it will cause your trigger to fire and you can see how it would respond in a deployed enviroment.

 

Please note the testMethod is a special type of class that exists only for the duration of its execution. So any inserts or deletes you create within it, are just temporary and never actually applied to your enviroment.  

 

 

 

 

msimondsmsimonds

Hey thanks for those steps, that really helped.

So I was able to get my test running to insert a new record //small steps at a time

 

 

@isTest private class CleanOppTrackerLineItemTest { static testMethod void testDeleteLineItems() { Date myDate = Date.today(); Date freezedate = myDate.addMonths(12); Opportunity_Trackers__c tracker = new Opportunity_Trackers__c(Name = 'Simonds Toliet', Distributor_Name__c = '0014000000FJu5EAAT', Distributor_FAE__c = 'Mike Simonds', Distributor_AM__c = 'William Yam', Market_Segment__c = 'Cell Phones', End_Customer_Name__c = 'Cisco', Distribution_Project_Status__c = '0-Conceptual', Freeze_Date__c = freezedate, Average_Systems_Per_Year__c = 10,Production_Life_months__c = 60 ); insert tracker; } }

 

 then I ran the test and it looks like it worked, but I am not sure because I cannot find the record that was inserted:

 

 

Apex Test Result Help for this Page Code Coverage Help for this PageHelp for this Page (New Window) (Code Covered: 0%) line executions source Summary Test Class CleanOppTrackerLineItemTest Tests Run 1 Test Failures 0 Code Coverage Total % 100 Total Time (ms) 138.0 Test Successes Method Name Total Time (ms) CleanOppTrackerLineItemTest.testDeleteLineItems 138.0 Debug Log *** Beginning Test 1: CleanOppTrackerLineItemTest.static testMethod void testDeleteLineItems() 20091214200141.054:Class.CleanOppTrackerLineItemTest.testDeleteLineItems: line 10, column 9: Insert: SOBJECT:Opportunity_Trackers__c 20091214200141.054:Class.CleanOppTrackerLineItemTest.testDeleteLineItems: line 10, column 9: DML Operation executed in 135 ms 20091214200141.054:External entry point: returning from end of method static testMethod void testDeleteLineItems() in 137 ms Cumulative resource usage: Resource usage for namespace: (default) Number of SOQL queries: 0 out of 100 Number of query rows: 0 out of 500 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 100 Number of DML rows: 1 out of 500 Number of script statements: 4 out of 200000 Maximum heap size: 0 out of 1000000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 10 Number of record type describes: 0 out of 10 Number of child relationships describes: 0 out of 10 Number of picklist describes: 0 out of 10 Number of future calls: 0 out of 10 Number of find similar calls: 0 out of 10 Number of System.runAs() invocations: 0 out of 20 Total email recipients queued to be sent : 0 Stack frame variables and sizes: Frame0 *** Ending Test CleanOppTrackerLineItemTest.static testMethod void testDeleteLineItems()

 

 How do i test to see if it worked?

 

 

 

 

DFDF

You are good to go.

 

Test Class	CleanOppTrackerLineItemTest
Tests Run 1
Test Failures 0
Code Coverage Total % 100
Total Time (ms) 138.0
Test Successes

 

Code Coverage Total % 100. You have hit your target.

 

 

You cant actually see the records you inserted. They only exist for the duration of your classes execution. 

 

Go ahead and right click on your trigger in eclipse and select force.com - > Deploy to server. You should now be able to deploy your new trigger to a production enviroment.

 

BTW Mike, I love the work you do with PHP and salesforce. Keep it up!

msimondsmsimonds

Thanks DF, I see that, but still not running my trigger because I am still adding the code to add line items to this opportunity_tracker.

 

I have to add a record to the child of this > opportunity_tracker_products__c and then delete the ID I had in the first creation.

 

 

Do you think that you could help me out with that.  I am still writing the code, but should be done soon

msimondsmsimonds

so I have this code to add line items:

 

 

@isTest private class CleanOppTrackerLineItemTest { static testMethod void testDeleteLineItems() { Date myDate = Date.today(); Date freezedate = myDate.addMonths(12); Opportunity_Trackers__c tracker = new Opportunity_Trackers__c(Name = 'Simonds Toliet', Distributor_Name__c = '0014000000FJu5EAAT', Distributor_FAE__c = 'Mike Simonds', Distributor_AM__c = 'William Yam', Market_Segment__c = 'Cell Phones', End_Customer_Name__c = 'Cisco', Distribution_Project_Status__c = '0-Conceptual', Freeze_Date__c = freezedate, Average_Systems_Per_Year__c = 10,Production_Life_months__c = 60 ); insert tracker; Opportunity_Tracker_Products__c[] newLimeItems = new Opportunity_Tracker_Products__c[] { new Opportunity_Tracker_Products__c (Opportunity_Tracker__c = tracker.id, Product__c = 'DS1831D+', Part_Status__c = 'Open', Quantity_Per_Board__c = 5,Maxim_DC_Price__c = 4.5, Resale_Price__c = 3.5 ), new Opportunity_Tracker_Products__c (Opportunity_Tracker__c = tracker.id, Product__c = 'DS1831CS+', Part_Status__c = 'Open', Quantity_Per_Board__c = 10,Maxim_DC_Price__c = 5.5, Resale_Price__c = 4.5 ) } insert newLineItems; } }

 

 and when I compile it, I get

Error: Compile Error: unexpected token: 'insert' at line 17 column 8

 

any ideas what I am doing wrong!!
 

 

 

 

msimondsmsimonds

This compiles, I forgot the ';' at the end of the curly brace, my mistake:

 

 

@isTest private class CleanOppTrackerLineItemTest { static testMethod void testDeleteLineItems() { Date myDate = Date.today(); Date freezedate = myDate.addMonths(12); Opportunity_Trackers__c tracker = new Opportunity_Trackers__c(Name = 'Simonds Toliet', Distributor_Name__c = '0014000000FJu5EAAT', Distributor_FAE__c = 'Mike Simonds', Distributor_AM__c = 'William Yam', Market_Segment__c = 'Cell Phones', End_Customer_Name__c = 'Cisco', Distribution_Project_Status__c = '0-Conceptual', Freeze_Date__c = freezedate, Average_Systems_Per_Year__c = 10,Production_Life_months__c = 60 ); insert tracker; Opportunity_Tracker_Products__c[] newLimeItems = new Opportunity_Tracker_Products__c[] { new Opportunity_Tracker_Products__c (Opportunity_Tracker__c = tracker.id, Product__c = 'DS1831D+', Part_Status__c = 'Open', Quantity_Per_Board__c = 5,Maxim_DC_Price__c = 4.5, Resale_Price__c = 3.5 ), new Opportunity_Tracker_Products__c (Opportunity_Tracker__c = tracker.id, Product__c = 'DS1831CS+', Part_Status__c = 'Open', Quantity_Per_Board__c = 10,Maxim_DC_Price__c = 5.5, Resale_Price__c = 4.5 ) }; insert newLimeItems; } }

 

 so I need help with the last step of this test

 

How do I delete the opporunity tracker in the first part. If that gets deleted, my trigger will work

 

Thanks for all the help

 

msimondsmsimonds

okay so I was able to get it to work and deployed to my production environment, but how to I take it make it inactive until I need it

 

msimondsmsimonds

okay need help again.

 

I ran the test and the trigger that I want to deploy in Eclipse is failing because it says 6 lines not tested, 0% coverage.

 

BUT i ran the test in my sandbox, not eclipse and it says it passed with 83%.

 

I don't get it.

 

How do I run a test in eclipse on my test APEX class

DFDF

Not sure I fully understand. You want to be doing your testing for the trigger you plan to deploy live in your production enviroment. Log into it from eclipse the same way you would your sandbox.

 

To make your trigger inactive,  open your project explorer in eclipse, go to the triggers folder and find your trigger. There should be two files that share your triggers name. One has a suffix of .trigger the other is .trigger-meta. Opne the meta file, change the word Active to inactive, save it, and deploy it to your server. That will make your trigger inactive.

 

Make sure your test class is in your production ecplise enviroment as well. It has to meet the test coverage on production in order to dpeloy. 

 

Run the test and check the logs. You should see where your test class is failing if it is. Also eclipse can be a bit confusing with saves. It may look like your file is error free but in reality you still have to hit ctrl+s before any errors will show. Also I find it useful to log into the front end of salesforce and find your trigger/classes there and see what has actually been imported.  

 

To actually run the test...just right click the test class in eclipse and select force.com->Run Tests

 

 

 

 

PragadheeshwariPragadheeshwari

Hai

 

  Do you want any help.Then i can.........

ben lenben len
Do you need to track someone location with their phone number (https://www.dream-recorder.com/how-to-track-someone-location-with-phone-number/)? Maybe you’re concerned about your child and want to make sure they’re safe, or maybe you think your partner is cheating on you and you want to catch them in the act. Whatever the reason, there are several ways that you can track someone’s location using their phone number.