+ Start a Discussion
Antonio_hotelbedsAntonio_hotelbeds 

Showing errors in Salesforce

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!!

 

BTW, I am in the conversion screen and I would like to show the error in that screen.

Devendra NataniDevendra Natani

Hi,

 

Instead of that 

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

 

Please use this to show error at top level

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

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

 

or to show error at field level

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

 

Chamil MadusankaChamil Madusanka

Hi,

 

Please refer following links.

 

http://bobbuzzard.blogspot.com/2011/04/field-level-error-messages-with.html

 

http://bobbuzzard.blogspot.com/2011/04/field-level-error-messages-with_29.html

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

Antonio_hotelbedsAntonio_hotelbeds

Hi,

 

I think my case is a bit special. I want to show the error in the conversion screen thats why when I use the statements you said they dont work. IUsing:

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

I get the error:

Error: UpdateNumbercontactsWhenConverting: execution of AfterUpdate caused by: System.FinalException: SObject row does not allow errors Trigger.UpdateNumbercontactsWhenConverting: line 14, column 4
 
This conversion screen is some king of follow up....Maybe I cuold access to this follow up and show the error?
 
 

Thanks a lot!

Devendra NataniDevendra Natani

please try

 

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

Chamil MadusankaChamil Madusanka

Hi,

 

You cannot addErrors validation afterUpdate. You have to change it as beforeupdate.

 

trigger UpdateNumbercontactsWhenConverting onLead (beforeupdate) {

 

}

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

Chamil's Blog

Antonio_hotelbedsAntonio_hotelbeds

Hi,

 

This is my code now:

 

trigger

UpdateNumbercontactsWhenConverting onLead (beforeupdate) {

 

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 (Account acc:Updatevalue.values()){

 

if (acc.Number_contacts__c==NULL) {

Trigger.

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

}

acc.Number_contacts__c=1;

//acc.Number_contacts__c+1;

}

//update Updatevalue.values();

}

}

 

 

 

Now it doesnt show any error and makes the conversion without updating the field Number_contacts_c

 

Any ideas?

 

thanks!

Devendra NataniDevendra Natani

Now your trigger is on before update. You are checking 

 

If(lead.IsConverted)

which will always return false, so addAccounts set will never be populated. 

 

 

 

Antonio_hotelbedsAntonio_hotelbeds

But I am checking the trigger.new what should contain the lead with the lead.isconverted = True, is that right?

 

How would you do it now to check that a lead is being converted?

 

Thanks