You need to sign in to do that
Don't have an account?
satyam
How to right a trigger to check duplicate record -refer the mail for requirement
Hi,
I have a requirement is that
I have a field name as 'Job Name' where i filles engineer in one record now if user again creating the record where job name is engineer then its should show error massage that one record with same job name is allready exist.
So any idea how i can write a trigger for this.
Thanks in Advance:))))
Hi Satyam.
Please visit the link below . there is Example to check the stop Duplciation Email in the Lead ... You have the same requirement . . YOu need to stop duplicate Job Name on You are object . . . so . .
In place of
Lead ---> Your object ...
Email Fields --> JobName fields .. .
Some modification . .
Done . .
Thansk
SFDC_Evolve
Please Mark it as resoved if this solves the issue . . :)
Please find the sode for this
trigger uniqueJobName on Account (before insert,before update)
{
Map<String, Account> AccountMap = new Map<String, Account>();
for (Account Account : System.Trigger.new)
{
if ((Account.Job_Name__c != null) && (System.Trigger.isInsert || (Account.Job_Name__c != System.Trigger.oldMap.get(Account.Id).Job_Name__c)))
{
if (AccountMap.containsKey(Account.Job_Name__c))
{
Account.Job_Name__c.addError('Another new Account has the ' + 'same Job Name.');
}
else
{
AccountMap.put(Account.Job_Name__c, Account);
}
}
}
for (Account Account : [SELECT Job_Name__c FROM Account WHERE Job_Name__c IN :AccountMap.KeySet()])
{
Account newAccount = AccountMap.get(Account.Job_Name__c);
newAccount.Job_Name__c.addError('A Account with this Job Name ' + 'already exists.');
}
}
regards
MS
Hi,
Thanks for help its working pretty fine.
Can u give me some hint to write test case.
Thanks:))))
@isTest
private class Test_uniqueJobName
{
public static testMethod void testAccount()
{
Account acc1=new Account(Name='test', Job_Name__c='Sfdc Developer');
Insert acc1;
Account acc2=new Account(Name='test', Job_Name__c='Sfdc Developer');
Insert acc2;
}
}