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
Anna SuganthiAnna Suganthi 

how to write test case for updating Account status?

Hi all,

Am a beginner to Salesforce development and Apex . I have a scenario where i need to create a trigger to update the account status to "Ëngaged" whenever the contact with Event subject "Initial Meeting" changes to "Sales Qualified". I have written the trigger for it. But now, stuck with the test class. Here is my trigger, 
trigger contactSetEngagedAccount on contact (after update){
    
    Map<ID, Contact> contactsToCheck = new Map<ID, Contact>();
    
    for (Contact updatedContact : System.Trigger.new){
       
        Contact oldContact = System.Trigger.oldMap.get(updatedContact.Id);
        
        //If the contact status is updated to "Sales Qualified" then update the Map      
        if(oldContact.contact_status__c != updatedContact.contact_status__c && updatedContact.Contact_Status__c.compareTo('Sales Qualified') == 0){
            
            contactsToCheck.put(updatedContact.Id,updatedContact);
        }
            // Check for events with Initial Meeting
            // 
            
        
        List<Event> eventList=([select WhoId, subject From Event where WhoId IN :contactsToCheck.keySet() and (subject = 'Initial Meeting')]);
        
        Set<ID> contactIds = new Set<ID>();
        for(Event eventId:eventList) {
            
            contactIds.add(eventId.WhoId);
        }
        
       // get the list of accounts          
       List<Account> accountList = 
            [select id, Status__c from Account 
             where Status__c IN ('Prospect', '') 
             and Id in (select AccountId from Contact where Id IN :contactIds)];
        
        // change the account status to Engaged
        for (Account oneAccount : accountList ) {
            oneAccount.Status__c = 'Engaged';               
        }
        
        // Update accounts
        update accountList;
        
    }
}

Can anyone please help me with the test class. It will be very helpful!!! TIA.
Raj VakatiRaj Vakati
try this code
 
@isTest
private class contactSetEngagedAccountTest {
    static testMethod void testOneCon(){
         Test.startTest(); 
        Account a = new Account(name='test acc',phone='9494146144',Status__c ='Prospect');
        insert a;
        Account a2 = new Account(name='test acc 2',phone='9494146144',Status__c ='Prospect');
        insert a2;
        Contact con = new Contact(accountid=a2.id,lastname='test con',email='lnarasimha823@gmail.com');
		con.contact_status__c ='Sales Qualified';
		
        insert con;
        Contact con2 = new Contact(accountid=a.id,lastname='test con2',email='lnarasimha823@gmail.com');
        con2.contact_status__c ='Not Qualified';
		
       
            insert con2;
			 Event e = new Event();
        e.WhatId=con2.id;
        e.StartDateTime=system.today();
        e.EndDateTime=system.today()+5;
		e.subject = 'Initial Meeting';
		
		insert e ; 
		
					con2.contact_status__c ='Sales Qualified';

            con.AccountId = a.id;
            update con;
      
        
        
        Test.stopTest();
        
    }
}

 
Anna SuganthiAnna Suganthi
Hi Raj, 

Thanks for the reply! 
Will try and let you know.
Raj VakatiRaj Vakati
Please make sure you are adding correct picklist values 
Anna SuganthiAnna Suganthi
Hi Raj, I tried this way, but the assertion is getting failed. Any idea?
// case 0 update contact status
        
        contact2.contact_status__c ='Sales Qualified';
        contact2.AccountId = account1.Id;
        update contact2;
        Contact queryContact = [select contact_status__C from Contact where Id = :contact2.id];
        System.assert([SELECT count() FROM Account WHERE Id = :account1.Id and status__c = 'Engaged'] == 1);

Also, my trigger is not fired when i run this test case. my code coverage is still None. it is not referring to my test case. :( 
 
Anna SuganthiAnna Suganthi
Hi Raj,

Figured out and everything is working fine and yes your code works!!!

Thanks a ton! :) :)