Trigger for When new Account is inserted with Industry as ‘Banking ‘ then set phone

By | November 16, 2019

Hi, Here we have written trigger for Account object. Whenever new Account is inserted with Industry as ‘Banking’ then set the phone as ‘999’. For this trigger we written Test class…

Trigger

trigger phoneUpdate on Account (before insert) {
    for(Account a: Trigger.New){
        if(a.industry=='Banking'){
            a.phone='999';
        }
    }
}

Test Class for the Trigger.

@isTest
private class PhoneTest {
    testmethod static void testme(){
        Account acc=new Account();
        acc.Name='SalesforceCodes';
        acc.Industry='Banking';
        try{
        	insert acc;
        }catch(Exception e){
            
        }
        Account a=[select id,phone from Account where id=:acc.Id];
        System.assertEquals(a.phone,'999');
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *