Activity Forums Salesforce® Discussions What is wrong in my Salesforce Trigger Test Class which updates IsUnreadbyOwner field to False?

  • What is wrong in my Salesforce Trigger Test Class which updates IsUnreadbyOwner field to False?

    Posted by Achintya on August 21, 2019 at 11:01 am

    I have written a simple Salesforce Trigger. I want to update the IsUnreadbyOwner field to False once a lead becomes unqualified.

    My Trigger is:

    trigger UnqualifiedLead on Lead (after update) {
        for(Lead lead: Trigger.new)
        {
            if (lead.Status == ‘Unqualified’)
            {
                lead.IsUnreadByOwner = False;
            }
        }
    }

    My Test class is:

    @isTest
    private class UnqualifiedLeadTest {
        static testMethod void myUnitTest() {
            // Setup the lead record
            Lead lead = new Lead();
            lead.LastName = ‘last’;
            lead.FirstName = ‘First’;
            lead.Company = ‘Company’;
            lead.Status = ‘Unqualified’;
            lead.IsUnreadByOwner = True;
            insert lead;
        }
    }

     

    Laveena replied 5 years, 3 months ago 2 Members · 1 Reply
  • 1 Reply
  • Laveena

    Member
    August 21, 2019 at 11:06 am

    Hi Achintya,

    In your test class you are only inserting the record yet your trigger is only setup to capture update events. You will either need to insert the lead then update to execute your trigger or add "on insert" to your trigger so that it runs when a lead is inserted and updated.

    Also, you are using an after event when you should be using a before event trigger for this type of update.

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos