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

Getting the error while executing the code
trigger AccountDuplicateTrigger on Account (before insert, before update)
{
for(Account a:Trigger.new)
{
List<Account> acc="Select id from Account where Name=:a.Name";
if(acc.size()>0)
{
acc.Name.addError(‘You Cannot Create the Duplicate Account’);
}
}
}
It should display error when account name is duplicate please help me
thx
All Answers
Try this:
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks
Instead of writing trigger, you can create Duplicate rule for this. Follow these steps:
Setup -> Data.com Administration -> Duplicate Management -> Duplicate Rules.
Create your rule, and prevent inserting duplicating accounts.
For more info, refer this link:
https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_create.htm&language=en_US (https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_create.htm&language=en_US)
Let me know, if you need any other help.
If this posts help you, mark it as best answer, so will help others in future.
Thanks,
Neetu
I need to implement in trigger only.Please help me
Thanks,
Sindhu
List<Account> acc=[Select id from Account where Name=:a.Name];
thx
Thanks,
Sindhu
thx
Both are fine but what is the difference
Thanks,
Sindhu
a.Name.addError('You Cannot Create the Duplicate Account');
instead of
acc.Name.addError(‘You Cannot Create the Duplicate Account’);
since a is the record bound to the trigger not acc.
Be sure to choose best answer.
Thx.
Thanks,
Sindhu.
Please help me
It's a common practice to ask a new question, but that said, you can try this:
Thx
Thanks,
Sindhu.
Might be you need to write trigger, but in this trigger, you are using SOQL in for loop which exploits salesforce best practices and you may get exceptions in case of bulk handling. (101 SOQL). I suggest you to use the below code to avoid exceptions: You can use this code, if only you need to handle bulk data.
Thanks,
Neetu
Please help me out
Thanks,
Sindhu
The code which I provided is not using SOQL or query inside for loop. You can use this code, this is following all the best practices of programming. If this helps, mark it as best answer.
Thanks,
Neetu
Map<Id, Decimal> acctIdToAmount = new Map<Id, Decimal>();
List<Account> accountsToUpdate = new List<Account>();
Map<ID,RecordType> typeMap = New Map<ID,RecordType>([Select ID, DeveloperName From RecordType Where sObjectType = 'CONTACT']);
for (CONTACT CON : trigger.new) {
// If the Record Type = Intake Form
if (typeMap.get(CON.RecordTypeId).DeveloperName == 'C2B' || typeMap.get(CON.RecordTypeId).DeveloperName == 'D2B') {
// Get the Contact Amount into a temp variable
Decimal sum = CON.PAID_AMOUNT__c == null ? 0 : CON.PAID_AMOUNT__c;
// Sum it up with the already collected Amount
if(acctIdToAmount.containsKey(CON.AccountId))
sum += acctIdToAmount.get(CON.AccountId);
// Make a map with AccountId as Key and Amount as value
acctIdToAmount.put(CON.AccountId, sum);
}
else if(typeMap.get(CON.RecordTypeId).DeveloperName == 'E2B') {
Decimal sum = 0;
if(acctIdToAmount.containsKey(CON.AccountId))
sum = acctIdToAmount.get(CON.AccountId);
sum -= CON.PAID_AMOUNT__c == null ? 0 : CON.PAID_AMOUNT__c;
acctIdToAmount.put(CON.AccountId, sum);
}
}
for(Id accId : acctIdToAmount.keyset()) {
Account acc;
if(acctIdToAmount.get(accId) >= 0)
acc = new Account(Id = accId, TOTAL_AMOUNT__c = acctIdToAmount.get(accId));
else
acc = new Account(Id = accId, PENDING_AMOUNT__c = acctIdToAmount.get(accId));
accountsToUpdate.add(acc);
}
if(!accountsToUpdate.isEmpty())
update accountsToUpdate;
}
and the error m facing is:duplicate value found: <unknown> duplicates value on record with id: <unknown>
can anyone help resolve dis issue.....