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
rajesh k 10rajesh k 10 

How display Error message based on check box otherwise allow save update record?

Hi,
      trigger AccountDuplicateTrg on Account (before insert,before update)
{
Map<string,Account> accMap = new Map<string,Account>();
set<string> accSet = new set<string>();
    for(Account acc : Trigger.new)
        accSet.add(acc.name);
    for(Account a : [select id,name from Account where name in : accSet])
        accMap.put(a.name,a);
    for(Account acc1 : trigger.new)
        {
        if(accMap.containskey(acc1.name))
           if(Trigger.isInsert){
             acc1.addError('There is another account in the system with this exact name and is a likely duplicate');
           }
           else if(Trigger.isUpdate){
               acc1.addError('There is another Account with the same name as this one. Please navigate to the Account tab');
           
           }
        }
  }

How to apply below senario above trigger updation time

I created a check box named "displayerror". Now in trigger if this check box is true, then throw the error. If check box is false then save the record and have the default value for this check box as true.So when user gets this error he can know the existence of a duplicate account and then in order to save the account he can uncheck the check box which inturn will save the record.

please help me...

 
KaranrajKaranraj
trigger AccountDuplicateTrg on Account (before insert,before update)
{

    for(Account acc : Trigger.new){
       if(acc.disaplayerror__c == True)
       acc.addError('There is another account in the system with this exact name and is a likely duplicate');
    }
}

The above will throw error message in account object when 'disaplyerror' checkbox field is enabled
Shaijan ThomasShaijan Thomas
trigger AccountDuplicateTrg on Account (before insert,before update)
{
Map<string,Account> accMap = new Map<string,Account>();
set<string> accSet = new set<string>();
    for(Account acc : Trigger.new)
        accSet.add(acc.name);
    for(Account a : [select id,name from Account where name in : accSet])
        accMap.put(a.name,a);
    for(Account acc1 : trigger.new)
        {
        if(<DisplyError(Checkboxfield)>) Add this Line
        if(accMap.containskey(acc1.name))
           if(Trigger.isInsert){
             acc1.addError('There is another account in the system with this exact name and is a likely duplicate');
           }
           else if(Trigger.isUpdate){
               acc1.addError('There is another Account with the same name as this one. Please navigate to the Account tab');
           
           }
        }
  }

if(<DisplyError(Checkboxfield)>) Add this Line
Try this
Shaijan
Bhanu MaheshBhanu Mahesh
Hi rajesh,

Try this (Assuming API Name for checkbox is displayerror__c)

else if(Trigger.isUpdate){
              if(acc1.displayerror__c){    
                    acc1.addError('There is another Account with the same name as this one. Please navigate to the Account tab');
              }
             else{
                    acc1.displayerror__c = true;
            }
 }

Regards,
Bhanu Mahesh
rajesh k 10rajesh k 10
Hi,
        When i will edit existing record and click save that time system will check there is any same account name in database and with displayerror__c is true it will display error .other wise allow save functionality without error how?.
Bhanu MaheshBhanu Mahesh
Hi Rajesh,
Try this

trigger AccountDuplicateTrg on Account (before insert,before update)
{
Map<string,Account> accMap = new Map<string,Account>();
set<string> accSet = new set<string>();
    for(Account acc : Trigger.new)
        accSet.add(acc.name);
    for(Account a : [select id,name from Account where name in : accSet])
        accMap.put(a.name,a);
    for(Account acc1 : trigger.new)
        {
        if(accMap.containskey(acc1.name)){
           if(Trigger.isInsert){
             acc1.addError('There is another account in the system with this exact name and is a likely duplicate');
           }
           else if(Trigger.isUpdate){
              if(acc1.displayerror__c){    
                    acc1.addError('There is another Account with the same name as this one. Please navigate to the Account tab');
              }
             else{
                    acc1.displayerror__c = true;
                }
            }

        }
    }
  }

Regards,
Bhanu Mahesh
rajesh k 10rajesh k 10
Hi Bhanu Mahesh,
                          I tried this code but not working already duplicate exist in my database and check box also "True" this sistuation its not throwing an error.but still its allowing save.

Note:Bydefault checkbox is true



 
KaranrajKaranraj
Try the below code
 
trigger AccountDuplicateTrg on Account (before insert, before update){

    Map<String, Account> acctMap = new Map<String, Account>();
    for (Account acct : System.Trigger.new){
	   
        if ((acct.Name !=null) &&
                (System.Trigger.isInsert ||
                (acct.Name != 
                    System.Trigger.oldMap.get(acct.Id).Name))){
            if (acctMap.containsKey(acct.Name) && acct.displayError__c == true){
                acct.Name.addError('Duplicate');
            }else if (acct.displayError__c == true) {
                acctMap.put(acct.Name, acct);
            }
       }
    }    
    for (Account acct : [SELECT Id,Name FROM Account
                      WHERE Name IN :acctMap.KeySet()]){
        Account newAcct = acctMap.get(Account.Name);
        acct.Name.addError('Duplicate');
    }
}



 
shikha ahujashikha ahuja
hey please help me ,
for contactupsert :
On Click "Save" button only update those records which has checkbox is checked otherwise skip.
created apex class and vf page trick code