
TRIGGER
trigger Account on Account (after update){
Id userId = userinfo.getUserId();
Id RecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'Person Account' LIMIT 1].Id;
system.debug('PA RecordTypeId =' + ' ' + RecordTypeId);
List<user> mcUser = [SELECT id from User WHERE username = 'mc.kapqa@kaplan.com.kapqa' ];
system.debug('MC Users ='+ ' ' + mcUser);
if (RecordTypeId == RecordTypeId && userId == mcUser[0].id){
accountServices asp = new accountServices(trigger.newmap, trigger.oldmap);
asp.process();
system.debug ('The trigger fired');
}
}
TEST CLASS
@isTest
public class AccountTest{
static testMethod void validateAccount() {
Test.startTest();
Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
System.debug('What is the profile id ' + profile1);
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
date tDate = date.today();
date uDate = Date.today().addDays(30);
User u = new User(
UserRoleId = portalRole.Id,
ProfileId = profile1.Id,
Username = 'testtermsconditions1234423@kaplan.com',
Alias = 'batman',
Email='testtermsconditions1234423@kaplan.com',
EmailEncodingKey='UTF-8',
Firstname='Bruce',
Lastname='Wayne',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
TimeZoneSidKey='America/Chicago');
insert u;
System.debug ('Here is the user id ' + u.id);
User u1 = [SELECT id from User WHERE username = 'mc.kapqa@kaplan.com.kapqa' LIMIT 1];
system.debug('MC Users ='+ ' ' + u1);
System.runas(u1) {
//Create account
Account a = new Account(
Name = 'Test Account',
OwnerId = u.Id,
Program_Of_Interest__c = 'LSAT',
Expected_test_date__c = tDate,
Expected_Prep_Start_Date__c = tdate);
insert a;
System.debug('Here is the account id ' + a.id);
Test.stopTest();
}
}
}
Please try below code. I hope that will help you.
NOTE:- As per salesforce best pratice we should nt use seeAllData= true in test class. But in your Trigger you are using hard cord user name that is why i used that.For more information in test classes you can see below blog:-http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.htmlPlease let us know if this will help you.Thanks,Amit Chaudhary@isTest(seeAllData=true)
public class AccountTest
{
static testMethod void validateAccount()
{
Test.startTest();
User u1 = [SELECT id from User WHERE username = 'mc.kapqa@kaplan.com.kapqa' LIMIT 1];
List<RecordType> lstRc =[SELECT Id FROM RecordType WHERE Name = 'Person Account' LIMIT 1];
system.debug('MC Users ='+ ' ' + u1);
System.runas(u1)
{
//Create account
Account a = new Account(
Name = 'Test Account',
OwnerId = u.Id,
if(lstRc.size() >0 )
{
recordTypeId = lstRc[0].id;
}
Program_Of_Interest__c = 'LSAT',
Expected_test_date__c = tDate,
Expected_Prep_Start_Date__c = tdate);
insert a;
System.debug('Here is the account id ' + a.id);
a.Name='Test Update';
update a;
Test.stopTest();
}
}
}