0

I have a VERY simple apex trigger. Since I'm new to the dev side of salesforce I am struggling with writing an apex class and test for this trigger. The trigger works in my sandbox but I have to validate it before I can deploy it to prod.

trigger trgMnCopyPrimaryContact on Opportunity (before update) {

for (Opportunity o : Trigger.new) {

OpportunityContactRole contactRole =
[select Contact.Name from OpportunityContactRole where IsPrimary = true and OpportunityId = :o.id];

if (contactRole != null) {
o.Purchaser__c= contactRole.Contact.Name;
}
}
}

UPDATE : Moving the code from comments into Question

 @isTest 
 public class trgMnCopyPrimaryContact 
  { static testMethod void trgMnCopyPrimaryContact() 
   { //Create and insert opp  
     Opportunity opp = new Opportunity(Name='BeställareTEST opp', StageName='stage', Probability = 10, CloseDate=system.today());     
    insert opp; 
  } 
  } 
  • 1
    When you have the test done you should then refactor to move the SOQL query outside of the for loop (bulkification). – Keith C Feb 11 '14 at 9:26
  • As you can see from above I haven't been able to write the test case. The above is solely the trigger which functions. I'm completely new to the SFdev. Was able to get the trigger going, but went blank when it came to writing the apex. This is the only thing I could master which doesn't give me anything: @isTest public class trgMnCopyPrimaryContact { static testMethod void trgMnCopyPrimaryContact() { //Create and insert opp Opportunity opp = new Opportunity(Name='BeställareTEST opp', StageName='stage', Probability = 10, CloseDate=system.today()); insert opp; } } – Dennis Feb 11 '14 at 9:34
  • @isTest public class trgMnCopyPrimaryContact { static testMethod void trgMnCopyPrimaryContact() { //Create and insert opp Opportunity opp = new Opportunity(Name='BeställareTEST opp', StageName='stage', Probability = 10, CloseDate=system.today()); insert opp; } } – Dennis Feb 11 '14 at 9:36
  • @Dennis you can update the question with the code for test class. Its easier reading on the question than the comments – Prady Feb 11 '14 at 9:57
4

Here is a wiki that you should read to learn about writing test classes. http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

You would also need to bulkify your trigger as this could hit the governor limits http://wiki.developerforce.com/page/Best_Practice%3A_Bulkify_Your_Code

You can also see an example of test class here http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm

As for your test class

In your test class you need to run this trigger you have written. As your trigger is fired on update of opportunity and it uses data from Contact role which in turn uses contact.

You need to first create these records in your test class.

Account a = new Account;

a.Name = 'testing in test method';

insert a;



Contact newContact = new Contact(Lastname='testLastName', AccountId = a.Id, LeadSource='Cold call');

insert newContact;



Opportunity o = new Opportunity(AccountId = a. Id, stagename='Meeting', ...);

insert o;



OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = o.Id, ContactId = newContact.Id, ...);

insert ocr;

Now we have the test records ready. Lets fire the trigger. Trigger gets fired during a DML operation. In your case its on update.

So do an update on the opportunity record

  o.stagename = 'Closed/Won';  
  update o;

These should have fired your trigger. Now you can check if the opportunity has been updated with name from contactrole using the system.assert statements.

assert(Boolean, Object) Asserts that the specified condition is true. If it is not, a fatal error is returned that causes code execution to halt.

assertEquals(Object, Object, Object) Asserts that the first two arguments are the same. If they are not, a fatal error is returned that causes code execution to halt.

assertNotEquals(Object, Object, Object) Asserts that the first two arguments are different. If they are the same, a fatal error is returned that causes code execution to halt.

  • As right as you probably are Prady, I just can't get it to work. So let me ask in another way and maybe you'll find that my error is found in my trigger: SITUATION: On an opportunity in Salesforce you are able to select a primary contact role. I want that selected name to appear in a custom field called "Purchaser". The reason being that I am using the field as a merge field in a mail merge word file. As I am not able to insert the PrimaryContactName as a merge field, I created the trigger that would copy the PrimaryContactName into my CustomerField ("Purchaser"). The trigger works but... – Dennis Feb 11 '14 at 12:06
  • So either my trigger is wrong, I just don't understand the testing part (may very well be so) or there is a completely different solution to my issue. – Dennis Feb 11 '14 at 12:09
  • Is there something in my answer you are not understanding. You should read through the first link on how to create a test class. If you have question shoot it out, would be more than happy to help you out Could you elaborate on "cant get it to work" We could help you resolving it – Prady Feb 11 '14 at 12:27
  • Ok from the code you have posted for test class. All you have done is creating a opportunity record. You need to create a contact record and contactRole record like i have provided in the sample code. After creation of these you would need to update opportunity to fire the trigger like i have done in the second code snippet i have provided. Once you have updated the opportunity you can query for the opportunity and confirm if the field has been updated using system.assertEquals – Prady Feb 12 '14 at 4:46

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.