• Monish
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

HI, I just cant seem to get any coverage on my trigger that is run when a custom object gets update.

A visual force page uses the following code to update a lead via a trigger attached to a custom object that the page creates.

 

I have one class and one trigger, I have put the test method in the class. This feels like the right thing to do but for the life of me (it's my first script) I cant get the test to run. The code is basic and does work in sandbox...

 

 

public with sharing class ManagePartnerLeadClass { public final Lead l; Lead_Feedback__c lf; public string comment {get; set;} private ApexPages.StandardController lController; public ManagePartnerLeadClass(ApexPages.StandardController controller) { lController = controller; this.l = (Lead)controller.getRecord(); } public PageReference CreateLeadApproval() { try { lf = new Lead_Feedback__c(); lf.LeadID__c = l.ID; lf.Lead_Approval_Status__c = 'Approved'; lf.Comments__c = comment; insert lf; } catch(DmlException ex) { ApexPages.addMessages(ex); } return new PageReference('http://www.salesforcewebform.com/thankyou.html'); } static testMethod void myUnitTest() { //Create Lead for the test Lead l = new Lead( Email='test@gwp.test', LastName='Test', Company='Test' ); insert l; // add a known lead //New instance of the standard page controller for this lead ApexPages.StandardController con = new ApexPages.StandardController(l); //New instance of the extension class ManagePartnerLeadClass mpl = new ManagePartnerLeadClass(con); //Set loose values mpl.comment = 'test comment'; //Test methods PageReference endPage = mpl.CreateLeadApproval(); //Confirm output System.assertEquals(endPage.getUrl(),'http://www.salesforcewebform.com/thankyou.html'); //Test feedback insert Lead_Feedback__c tlf = new Lead_Feedback__c(LeadID__c = l.ID, Lead_Approval_Status__c = 'Approved',Comments__c='Trigger test'); insert tlf; //Test to see if trigger was hit String leadId = tlf.LeadID__c; Lead trl = [select Id from Lead where Id =:leadId]; System.assertEquals(leadId, trl.Id); } }

 The trigger

 

trigger Update_Lead_Feedback on Lead_Feedback__c (after insert) { //also , after update required? // trigger automatically syncs Lead Approval to the Lead record. Lead l; for (Lead_Feedback__c lf : Trigger.new) { String leadId = lf.LeadID__c; if (leadId !=null && leadId !='') { l = [select Id from Lead where Id=:leadId]; l.Lead_Approval__c = lf.Lead_Approval_Status__c; l.Lead_Approval_Feedback__c = lf.Comments__c; update l; } } }

 

 

 

 Big thanks, I'm sitting on a risky project after having toted Salesforce as THE platform we should be using... now I'm beginning to worry as development is down to a snails pace, any suggestions to improve the code? Perhaps it's my whole approach???

 

 

  • December 06, 2009
  • Like
  • 0