+ Start a Discussion
Antonio_hotelbedsAntonio_hotelbeds 

capture errors and showing a different one

Hi!

 

Im new in capturing errors and showing errors within salesforce and....I am a bit lost....

 

I have a trigger that is fired when converting a lead into account. This trigger will update one field of the account.

 

The problem is that in our organization we only let the sales managers convert into exisiting accounts, not into new ones. But some of them still try to convert into new ones and then, because of the trigger they get this error:

Error: UpdateNumbercontactsWhenConverting: execution of AfterUpdate caused by: System.FinalException: SObject row does not allow errors Trigger.UpdateNumbercontactsWhenConverting: line 14, column 4
 
I would like to show them a different error, easier to understand, like: ERROR:you cannot convert a lead into a new account.
 
Any ideas on how to do it?This is my code. I tried with the addError but i get

Save error: Method does not exist or incorrect signature: addError(String)

 

trigger UpdateNumbercontactsWhenConverting onLead (afterupdate) {

 

Set<Id> addAccounts = newSet<Id>();

 

for (Lead lead: Trigger.new){

 

if(lead.IsConverted){

addAccounts.add(lead.ConvertedAccountId);

}

}

if

(addAccounts.size()>0){

Map<Id,

Account> Updatevalue = new Map<Id,Account>([select Number_contacts__c fromAccountwhereId in: addAccounts]);

 

for (Accountacc:Updatevalue.values()){

 

if (acc.Number_contacts__c==NULL) {

addError(

'You cannot convert a lead into a new account only into an existing one');

}

acc.Number_contacts__c=acc.Number_contacts__c+1;

}

updateUpdatevalue.values();

}

}

 

Thanks!!

Ankit AroraAnkit Arora

Let me know if it works for you. Just change this :

 

addError(
'You cannot convert a lead into a new account only into an existing one');

To this : 

 

 trigger.new[0].addError(
'You cannot convert a lead into a new account only into an existing one');

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Antonio_hotelbedsAntonio_hotelbeds

Hi,

 

Unfortunately this doesnt work for me.

 

In the moment that the trigger is being fired I am in the conversion screen. Thats the main problem, I dont know how to show the error.

 

Thanks Ankit!