0

We have a trigger on attachment, can we control the trigger firing on the basis of users who share same profile. Like through custom setting or anything.

  • 4
    Yes, UserInfo.getProfileId() method will give you the logged in user profile. Accordingly, you can implement conditions in your trigger code. – Devendra Aug 3 '16 at 16:47
5

You can create a Profile whitelist fairly simply. Use the Lazy Load pattern, and look at Trigger Handler patterns to find one that suits you.

public with sharing class MyObjectService
{
    public static Set<Id> whitelistedProfileIds
    {
        get
        {
            if (whilelistedProfileIds == null)
                whitelistedProfileIds = new Map<Id, Profile>([
                    SELECT Id FROM Profile WHERE Name IN ('<value1>', '<value2>')
                ]).keySet();
            return whitelistedProfileIds;
        }
        private set;
    }
    public static Boolean isRunningUserWhilelisted()
    {
        return isWhitelisted(UserInfo.getProfileId());
    }
    public static Boolean isWhitelisted(Id profileId)
    {
        return whitelistedProfileIds.contains(profileId);
    }
}

If you adopt a Trigger Handler pattern, it is really simple to bail when you hit a specific condition.

public with sharing class MyObjectTriggerHandler
{
    final List<MyObject__c> newRecords;
    final Map<Id, MyObject__c> oldMap;
    public MyObjectTriggerHandler(List<MyObject__c> newRecords, Map<Id, MyObject__c> oldMap)
    {
        this.newRecords = newRecords;
        this.oldMap = oldMap;
    }

    public void beforeInsert()
    {
        // logic that should always run
        if (isRunningUserWhitelisted())
        {
            // logic that should only run for specific profiles
        }
    }
    public void afterInsert()
    {
        // similar pattern
    }

    // other trigger events
}
| improve this answer | |
0
  1. You can create a Custom Setting of list type and add Names of profiles which you want to be skipped. This custom settings can be easily configured by System Admin without updating code in future.
  2. Then in trigger you can fetch values of that custom setting, to check if logged in user have that profile.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.