+ Start a Discussion
Abraham DurojaiyeAbraham Durojaiye 

Need an Opportunity object trigger


I have a new record type called Competitive Opportunities in the Opportunities object. I am looking to create a trigger that allows only the users in a specific Role (Global Users) or with a specific Profile (Global Permissions) to be able to access and complete the record.

For users in other profiles, an error message should pop up indicating that they do not have the correct permissions to access the record. 

Also, any records of the Competitive Opportunities type should only be visible in a search to the Global Users or Global Permissions role or profile.

Please advise.
Daniel BallingerDaniel Ballinger
With regards to restricting access to the record type by Profile, you would probably be better off using the built in support for this. See How is record type access specified?

With regards to roles, again, it might be easier to create permission sets that mirror the roles to use the built in support.

In a trigger you could check the UserInfo.getUserRoleId() to get the users roles. Compare this value for each record being inserted with the allowed roles and then use sObject.addError() to reject invalid records.