function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steven Wellman 23Steven Wellman 23 

I'm having problems writing a test class for this trigger

Here's my Trigger:
trigger SoldCustomer on Opportunity (after update) {
    for (Opportunity opp : Trigger.new) {
        if (opp.Account.Type != 'Customer' && opp.StageName == 'Sold' && opp.Account.Type != null) {
            opp.Account.Type = 'Customer';
        }
    }
}

Here's my Test Class:
@isTest
private class SoldCustomerTest {

    @isTest static void CreateOpp() {
        // Create Account
        Account acc = new Account();

        acc.Name = 'Created Account';
        acc.Type = 'Prospect';
        String accId = acc.Id;

        Opportunity opp = new Opportunity();
        opp.StageName = 'Discovery';
        opp.CloseDate = date.parse('12/27/2019');
        opp.Name = 'Sold Customer Test Opp';
        opp.AccountId = accId;
    }

    @isTest static void UpdateOpp() {
        List<Opportunity> records = [
        SELECT Id FROM Opportunity WHERE Name = 'Sold Customer Test Opp'
        ];

        for (Opportunity record : records) {
            record.StageName = 'Sold';
        }

        update records;
    }
}
When I run the test class, it shows no coverage for the trigger. This is my 2nd trigger and I'm really excited about asking my first question. Hopefully, someone can help. Thanks in advance!
Pradeep SinghPradeep Singh
Hi,
Use Insert operation on account and opportunity records to insert and then make changes and update the records.  
Abdul KhatriAbdul Khatri
Here you go

Trigger
trigger SoldCustomer on Opportunity (after update) {
    
    List<Opportunity> oppList = [SELECT Id, StageName, Account.Type FROM Opportunity WHERE Id IN :trigger.new];
    List<Account> accountToUpdate = new List<Account>();
    
    for (Opportunity opp : oppList) {

        if (opp.Account.Type != 'Customer' && opp.StageName == 'Sold' && opp.Account.Type != null) {
            
            Account acctToUpdate = new Account (Id = opp.AccountId);
            acctToUpdate.Type = 'Customer';
            
            accountToUpdate.add(acctToUpdate);
        }
    }
    
    if(accountToUpdate.size() > 0)
        update accountToUpdate;
}

Test Class
@isTest
public class SoldCustomerTest {
    
    @isTest static void TestOpp() {
        // Create Account
        Account acc = new Account();

        acc.Name = 'Created Account';
        acc.Type = 'Prospect';
        insert acc;

        Opportunity opp = new Opportunity();
        opp.StageName = 'Discovery';
        opp.CloseDate = date.parse('12/27/2019');
        opp.Name = 'Sold Customer Test Opp';
        opp.AccountId = acc.Id;
        insert opp;
        
        opp.StageName = 'Sold';
        update opp;
        
        Account account = [Select Type FROM Account WHERE Id = :acc.Id];
        system.assert(account.Type == 'Customer');
    }

}

Abdul KhatriAbdul Khatri
Was the solution helpful?
Steven Wellman 28Steven Wellman 28
@abdul, the assertion fails. I've updated the Test Class to:
@isTest
private class SoldCustomerTest {

    @isTest static void CreateOpp() {
        // Create Account
        Account acc = new Account();

        // Set required values for account
        acc.Name        = 'Created Account';
        acc.Type        = 'Prospect';
        String accId    = acc.Id;
        insert acc;

        // Create opp and set values
        Opportunity opp = new Opportunity();
        opp.StageName   = 'Discovery';
        opp.CloseDate   = date.parse('12/27/2019');
        opp.Name        = 'Sold Customer Test Opp';
        opp.AccountId   = accId;
        insert opp;

        // Update opp to sold
        opp.StageName   = 'Sold';
        update opp;
    }

    @isTest static void UpdateOpp() {
        
        // Create Account
        List<Opportunity> records = [SELECT Id
                                     FROM   Opportunity
                                     WHERE  Name = 'Sold Customer Test Opp'];

        for (Opportunity record : records) {
            record.StageName = 'Sold';
        }

        update records;
    }
}

Now I get 67% coverage.
 
Abdul KhatriAbdul Khatri
Did you update the trigger too?
Steven Wellman 11Steven Wellman 11
@Abdul, that worked. Thank you!!