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
Devendra SawantDevendra Sawant 

Standard Field - Name

 

Hi,

How can i make standard field- Name Text(80) as Unique?

It is taking duplicate values and i want to avoid the same.

Cheers,
Devendra S

Best Answer chosen by Admin (Salesforce Developers) 
LakshmanLakshman

Try modifying your code as follows:

 

trigger duplicateGC on GL_Code__c (before insert,before update) {
List<GL_Code__c> lstGC = new List<GL_Code__c>();
if(Trigger.isInsert)
{
    for(GL_Code__c g:Trigger.New)
    {
        lstGC=[select id from GL_Code__c where Name =: g.Name];
        if(lstGC.size()>0)
        {
           for(GL_Code__c g1:lstGC)
               g.Name.addError('Duplicate Record');
              
        }
       
    }
}
else if(Trigger.IsUPdate)
{
for (GL_Code__c no : Trigger.new)
           {
              GL_Code__c old = Trigger.oldMap.get(no.id);
              if (no.Name != old.Name)
              {
                 lstGC = [select id from GL_Code__c where Name =: no.Name];
        
                 if (lstGC.size() > 0)
                 {
                    for(GL_Code__c noOld : lstGC)
                    {

                        no.Name.addError('Duplicate Record');

                     }
                 }
                 
              }
           }
           }
}

 

 

Let me know if you are facing any issues.

 

Regards,

Lakshman

All Answers

Rahul SharmaRahul Sharma

You would need to write a trigger for that object, which won't allow you to create a duplicate record with same Name

Devendra SawantDevendra Sawant

I am doing the same..

 

This is my code,

 

trigger duplicateGC on GL_Code__c (before insert,before update) {
List<GL_Code__c> lstGC = new List<GL_Code__c>();
if(Trigger.isInsert)
{
    for(GL_Code__c g:Trigger.New)
    {
        lstGC=[select id from GL_Code__c where Name =: g.Name];
        if(lstGC.size()>0)
        {
           for(GL_Code__c g1:lstGC)
               g1.addError('Duplicate Record');
              
        }
       
    }
}
else if(Trigger.IsUPdate)
{
for (GL_Code__c no : Trigger.new)
           {
              GL_Code__c old = Trigger.oldMap.get(no.id);
              if (no.Name != old.Name)
              {
                 lstGC = [select id from GL_Code__c where Name =: no.Name];
        
                 if (lstGC.size() > 0)
                 {
                    for(GL_Code__c noOld : lstGC)
                    {}
                 }
                 
              }
           }
           }
          
           if(lstGC.size() > 0)
       update lstGC;   

}

 

How can i display error message and avoid inserting a record if it is duplicate record?

 

 

Cheers,

Devendra S

LakshmanLakshman

Try modifying your code as follows:

 

trigger duplicateGC on GL_Code__c (before insert,before update) {
List<GL_Code__c> lstGC = new List<GL_Code__c>();
if(Trigger.isInsert)
{
    for(GL_Code__c g:Trigger.New)
    {
        lstGC=[select id from GL_Code__c where Name =: g.Name];
        if(lstGC.size()>0)
        {
           for(GL_Code__c g1:lstGC)
               g.Name.addError('Duplicate Record');
              
        }
       
    }
}
else if(Trigger.IsUPdate)
{
for (GL_Code__c no : Trigger.new)
           {
              GL_Code__c old = Trigger.oldMap.get(no.id);
              if (no.Name != old.Name)
              {
                 lstGC = [select id from GL_Code__c where Name =: no.Name];
        
                 if (lstGC.size() > 0)
                 {
                    for(GL_Code__c noOld : lstGC)
                    {

                        no.Name.addError('Duplicate Record');

                     }
                 }
                 
              }
           }
           }
}

 

 

Let me know if you are facing any issues.

 

Regards,

Lakshman

This was selected as the best answer
Rahul SharmaRahul Sharma

Had you tried your code, I'm sure it will fail in case of bulk Update/Insert.

For bulk update try below code:

 

trigger AvoidDuplicateAccounts on Account (before insert, before update) 
{
 Set<String> setAccountName = new Set<String>(); 
 for(Account objAccount: [Select Name from Account])
  setAccountName.add(objAccount.Name);
 for(Account objAccount: Trigger.new)
 {
  if(!setAccountName.contains(objAccount.Name))
  {
   //   To handle bulk insert
setAccountName.add(objAccount.Name); } else { objAccount.Name.addError('Account with same name Exists'); } } }

 

rohitsfdcrohitsfdc

you should use Maps or Sets for that, as Map cant have duplicate records.

the above code will hit governer limits if you insert large number of records.

 

Devendra SawantDevendra Sawant

 

Thanks to all.

 

Cheers,

Devendra S