+ Start a Discussion
sandip87sandip87 

Code coverage

Hi I am testing my triggers and getting problems to write test method for triggers as i am new to test methods. Here is my trigger. the problem that i facing is due to three different objects are used in my trigger. Please review this code for trigger that i wrote. Thanks in advance.

Trigger Code:

 

 trigger RiskConvInsert on JunctionRiskRiskSearch__c (after insert)
  {
          List<JunctionRiskSearchConvJunc__c> juncRRSCJ = new List<JunctionRiskSearchConvJunc__c>();
          for(JunctionRiskRiskSearch__c jrsc: Trigger.New)
          {
              List<Risk_Conversation__c> riskConv = [Select Id from Risk_Conversation__c where Risk_Search__c =: jrsc.Risk_Searches__c];
              for(Risk_Conversation__c riskConvRec: riskConv)
              {
                  JunctionRiskSearchConvJunc__c junRec = new JunctionRiskSearchConvJunc__c();
                  junRec.Risk_Conversations__c = riskConvRec.Id;
                  junRec.JunctionRiskSearch__c = jrsc.Id;
                  juncRRSCJ.add(junRec);
              }
              insert juncRRSCJ;
          }
      }

Dhaval PanchalDhaval Panchal
What issue you are facing, Please show your test class here.
sravan36sravan36

Hi Sandeep,

 

When you are writing test methods make sure you need to prepare test data for your trigger/class in your test class. As per your trigger you need to prepare test data for your all objects those are used in trigger. Refer below link for clear idea.

 

http://shivasoft.in/blog/salesforce/step-by-step-salesforce-tutorial-%E2%80%93-creating-trigger-and-test-cases-%E2%80%93-6-of-6/

 

 

Better to use (seeallData=true).

 

Regards,

Sravan.

Chris760Chris760

@sandip99, for some reason, test classes are often poorly explained.  To make sure you clearly understand... all a test class is, is the steps you go through one by one as a user in the salesforce org, in order to eventually cause your code to run (including the If/Else logic in your code if any exists).

 

As you probably know, APEX is like DOS... in that you can do everything in APEX that you can do in the "point and click" salesforce user interface (as if it were Windows).  So a test class is you writing out DOS-style, all the individual steps you would take in order to cause all the lines in your code to run.  That's all!

 

So you'd have to write (in APEX) each step:

 

Create Account with a, b, c, d, and e fields populated.

 

Create Contact with a, b, c, d, and e, fields populated.

 

Create Opportunity with h, j, k, l, m, n, o and p fields populated.

 

Edit the Opportunity you just made with new values for fields h, j, and k.

 

Etc. Etc. Etc.

 

In your case, I don't know if JunctionRiskRiskSearch__c is a detail record of a master record.  If it was looking up to an Opportunity, then you'd have to make an Account so that you can make an Opportunity (because Opportunities are detail records of Accounts) and then you'd have to specify that opportunity ID in the lookup of the JunctionRiskRiskSearch__c record you make in order to finally make your code run.  If it's just a lone object with no master-detail relationships to anything, then you can write out the test class a lot faster.

 

Regardless, you're literally just writing a shopping list of APEX commands that simulate mouse clicks and field inputs that the user would normally make in the salesforce user interface, which would cause your code to run.  That's all.  Just make sure that you follow the format for test classes and you'll be golden. :)