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
Salesforce2015Salesforce2015 

Simple 3 lines Trigger Test

Hi Experts,

Below trigger working sucessfully in single Account Standard Object. But i need test trigger for deployment from Sandbox to Production.

trigger ExecutiveOwner on Account(before insert, before update){
    Account Owner = [Select Executive_Owner__c from Account Where Account.Strategic__c= null or Account.Strategic__c= 'No' limit 1];
        Owner.Executive_Owner__c = null;
}

Below is fields Info on Account:
Strategic__c
Executive_Owner__c

Please anyone help me.
Thanks in Advance.

Thanks,
Manu
Best Answer chosen by Salesforce2015
robdobbyrobdobby
Hi!  I suggest rewriting your trigger.  You don't need the SOQL:
// since we cannot guarantee the order that triggers are executed
// its a best practice to have a single trigger on each object
// named for the object
trigger Account on Account(before insert, before update) {

	if (trigger.isBefore) {
		for (Account a : trigger.new) {
			if (a.Strategic__c == null || a.Strategic__c.equals('No')) {
				a.Executive_Owner__c = null;
			}
		}
	}
}


All you need to do in a unit test to get coverage is insert an Account record:
 
@isTest
private class Test_AccountTrigger {

    static testMethod void testAccountTrigger() {
		    	    	
    	Account acct1 = new Account(Name = 'Test Account', Strategic__c='Yes', Executive_Owner__c=UserInfo.getUserId());
    	Account acct2 = new Account(Name = 'Test Account', Strategic__c='No',  Executive_Owner__c=UserInfo.getUserId());
    	insert new List <Account> {acct1, acct2};
   	}
}

If you were serious about verifying your trigger you could verify the unit test:
 
@isTest
private class Test_AccountTrigger {

    static testMethod void testAccountTrigger() {
		    	    	
    	Account acct1 = new Account(Name = 'Test Account', Strategic__c='Yes', Executive_Owner__c=UserInfo.getUserId());
    	Account acct2 = new Account(Name = 'Test Account', Strategic__c='No',  Executive_Owner__c=UserInfo.getUserId());
    	insert new List <Account> {acct1, acct2};
    	
    	acct1 = [select Executive_Owner__c from Account where Id = :acct1.Id];
    	system.assertEquals(UserInfo.getUserId(), acct1.Executive_Owner__c);
    	
    	acct2 = [select Executive_Owner__c from Account where Id = :acct2.Id];
    	system.assertEquals(null, acct2.Executive_Owner__c);
   	}
}




Rob Kemper - OpFocus