function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vutukuru balajivutukuru balaji 

how to restrict trigger firing using hierchy custom setting

hi I write a trigger which blocks insertion of duplicate records in account object.but i want for particular user to the trigger should not be fired.i know that we restrict it by using hierchy custom setting but how to do these please can any explain about it.
My trigger code is:

trigger duplicaterecord on Account (before insert) {

 set<string>names=new set<string>();
    List<Account>accs=[select id,name from account];
    for(Account a:accs)
    {
        names.add(a.name);
    }
    for(account b:trigger.new)
    {
        if(names.contains(b.name))
        {
            b.addError('Duplicate Record');
        }
    }
}
Best Answer chosen by vutukuru balaji
Shrikant BagalShrikant Bagal
Correct Code:

trigger duplicaterecord on Account (before insert) {
TriggerFlow__c triggerflow = TriggerFlow__c.getInstance(userinfo.getUserId());  // its profile Id or User Id. I am using logged user Id.
if(triggerflow.IsActive__c){
 set<string>names=new set<string>();
    List<Account>accs=[select id,name from account];
    for(Account a:accs)
    {
        names.add(a.name);
    }
    for(account b:trigger.new)
    {
        if(names.contains(b.name))
        {
            b.addError('Duplicate Record');
        }
    }
}
}

All Answers

Shrikant BagalShrikant Bagal
You have to create hierchy custom setting suppose it is "TriggerFlow" with field "IsActive"
And create record Org level record as "IsActive=true"
And Create records with "IsActive=false" for whom you don't want to fire a trigger.

then your trigger will be 
trigger duplicaterecord on Account (before insert) {
TriggerFlow__c triggerflow = TriggerFlow__c.getInstance(userinfo.getUserId());  // its profile Id or User Id. I am using logged user Id.
if(triggerflow.IsActive){
 set<string>names=new set<string>();
    List<Account>accs=[select id,name from account];
    for(Account a:accs)
    {
        names.add(a.name);
    }
    for(account b:trigger.new)
    {
        if(names.contains(b.name))
        {
            b.addError('Duplicate Record');
        }
    }
}
}
vutukuru balajivutukuru balaji
thanks shrikanth it's working but a small mistake in that it is
in If condition used it if(triggerflow.isActive__c) insted of if(triggerflow.isActive)
Shrikant BagalShrikant Bagal
sorry, Right !!!!!!!!!!!
Shrikant BagalShrikant Bagal
Correct Code:

trigger duplicaterecord on Account (before insert) {
TriggerFlow__c triggerflow = TriggerFlow__c.getInstance(userinfo.getUserId());  // its profile Id or User Id. I am using logged user Id.
if(triggerflow.IsActive__c){
 set<string>names=new set<string>();
    List<Account>accs=[select id,name from account];
    for(Account a:accs)
    {
        names.add(a.name);
    }
    for(account b:trigger.new)
    {
        if(names.contains(b.name))
        {
            b.addError('Duplicate Record');
        }
    }
}
}
This was selected as the best answer
Shrikant BagalShrikant Bagal
If its resolved Please mark as Solved and Select best answser. It will helps others.
Shrikant BagalShrikant Bagal
Please mark as best answer so it will help to other who will serve same problem.
​Thanks! 
Shrikant BagalShrikant Bagal
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
​Thanks!