You need to sign in to do that
Don't have an account?

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
You need to sign in to do that
Don't have an account?
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
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
You would need to write a trigger for that object, which won't allow you to create a duplicate record with same Name
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
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
Had you tried your code, I'm sure it will fail in case of bulk Update/Insert.
For bulk update try below code:
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.
Thanks to all.
Cheers,
Devendra S