+ Start a Discussion
Devendra SawantDevendra Sawant 

Avoid Duplicate Role

 

Hi,

 

How to avoid user from creating duplicate roles ?

 

Regards,

Devendra S

 

 

r1985r1985

Hi,

 

U can use the following trigger for checking duplicate role.

 

trigger rolecheck on User(before insert)

{

List<User> ulist=[select UserRoleId from User where isActive=true];

Map<id,User> m=new Map<id,User>();

for(User u1:ulist)

{

m.put(u1.UserRoleId,u1);

}


for(User u:Trigger.new)

{


if(m.get(u.UserRoleId)!=null)

{
u.addError('Error Message');

}

{

}

}
}

 

Thanks,

Malar

Rahul SharmaRahul Sharma

Duplicate role?

Please elaborate the requirement.

Devendra SawantDevendra Sawant
Hi, I am creating Role using visualforce page. If new created role matches with the available roles. Then it should generate an error message. Because then duplicate role would be created. I want to avoid this duplicate role. Thanks and Regards, Devendra S
Rahul SharmaRahul Sharma

I guess below steps will help in your development.

 

-Query all the roles and take it in a map<String, sObject> and put the each role in map's keyset.

-Before inserting new Role from vf page, check if the role already exists (show page message that role exists.)

and if Role doesn't exists, Create it.