+ Start a Discussion
sreenivasAppssreenivasApps 

need test class for following trigger ?

have to create new user with required information . so while creating how to give user profileId , user licenseId all those things ...

 

 

trigger opprolechangeusertrig on User (after update) {
user u = trigger.new[0];
user u2 = trigger.old[0];
list<Id> lst = new list<Id>();

if(u2.UserRoleId!= u.UserRoleId)
{
lst.add(u.Id);
userrolechangetrigcls.usercls(lst);
}
}

venkateshyadav1243venkateshyadav1243

Hi

 

try like this

 

 

@isTest
private class Test_MyControllerExtension {
public static testmethod void test()
{
Profile p = [select id from profile where name='Some Profile'];
 
  User  testUser = new User(alias = 'u1', email='u1@testorg.com',
      emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
      localesidkey='en_US', profileid = p.Id, country='United States',
      timezonesidkey='America/Los_Angeles', username='u1@testorg.com');
 
    insert testUser;
}
}

 

 

regards

venkatesh

sreenivasAppssreenivasApps

if(u2.UserRoleId!= u.UserRoleId)

 

how to check this condition . here we are not mension the userroleId . 

Naidu PothiniNaidu Pothini
@isTest
private class Test_MyControllerExtension 
{
	public static testmethod void test()
	{
		Profile p = [select id from profile where name='Some Profile'];
		List<UserRole>  rList = [SELECT Id FROM UserRole LIMIT 2];

		User  testUser = new User(alias = 'u1', email='u1@testorg.com',emailencodingkey='UTF-8', lastname='Testing',
								  languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, country='United States',
								  timezonesidkey='America/Los_Angeles', username='u1@testorg.com', UserRoleId = rList[0].Id);
		insert testUser;

		testUser.UserRoleId = rList[1].Id;
		update testUser;
	}
}

 try this.

 

I suggest you to change your trigger code to something like this.

 

trigger opprolechangeusertrig on User (after update)
{
	List<Id> lst = new List<Id>();

	for(Integer i =0; i < Trigger.new.size(); i++)
	{
		if(Trigger.new[i].UserRoleId != Trigger.old[i].UserRoleId)
		{
			lst.add(u.Id);
		}
	}
	userrolechangetrigcls.usercls(lst);
}