You need to sign in to do that
Don't have an account?
Need some help in trigger when creating lead
Hi
I need help.
When ever I create a lead I want to search unique org number in lead and account database? here is my code but i am getting error as Apex trigger blockDuplicates_tgr1 caused an unexpected exception, contact your administrator: blockDuplicates_tgr1: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.blockDuplicates_tgr1: line 36, column 22
trigger blockDuplicates_tgr1 on Lead bulk(after insert, after update)
{
/*
* begin by building a map which stores the (unique) list of leads
* being inserted/updated, using email address as the key.
*/
Map<String, Lead> leadMap = new Map<String, Lead>();
for (Lead lead : System.Trigger.new)
{
if (lead.Organisation_Number_Norway__c != null)
{
/* for inserts OR
* updates where the email address is changing
* check to see if the email is a duplicate of another in
* this batch, if unique, add this lead to the leadMap
*/
if ( System.Trigger.isInsert ||
(System.Trigger.isUpdate &&
lead.Organisation_Number_Norway__c != System.Trigger.oldMap.get(lead.Id).Organisation_Number_Norway__c))
{
if (leadMap.containsKey(lead.Organisation_Number_Norway__c))
{
lead.Organisation_Number_Norway__c.addError('Another Lead or Account has the same Organisation Number.');
}
else
{
leadMap.put(lead.Organisation_Number_Norway__c, lead);
}
}
}
String leadOrgNR = lead.Organisation_Number_Norway__c;
Account acc = new Account ();
acc = [Select Organisation_Number_Norway__c from Account
WHERE Account.Organisation_Number_Norway__c =: leadOrgNR limit 1];
if (acc.Organisation_Number_Norway__c.equals(lead.Organisation_Number_Norway__c))
{
lead.Organisation_Number_Norway__c.addError('Another Lead or Account has the same Organisation Number.');
}
else
{
// leadMap.put(lead.Organisation_Number_Norway__c, lead);
}
}
}
The issue is with the following line of code I believe:
Looking at your trigger, this looks like it is attempting to ensure that there is no account with the same organisation number norway as the current lead. However, this will throw an exception if there is not exactly one record returned by the SOQL query.
You'll need to query the SOQL into a list and check if that is empty or not, e.g.
All Answers
The issue is with the following line of code I believe:
Looking at your trigger, this looks like it is attempting to ensure that there is no account with the same organisation number norway as the current lead. However, this will throw an exception if there is not exactly one record returned by the SOQL query.
You'll need to query the SOQL into a list and check if that is empty or not, e.g.
Hi Bob, many thanks buddy. It's working fine now for me.
many thanks.
regards,
Pradip