+ Start a Discussion
LudivineLudivine 

How to write an adderror method in a trigger?

Hi,
When users are changing the Account Owner, I have made a trigger to change the Owner of the realated Contract as well.
When some mandatory fields are empty in the related contract, users see a red message with the name of the field to fill but it is abit frightning.
I want to add a adderror() method in my trigger to show them the following message : 'Please complete all mandatory field on contract page'
Can you help me add this in my trigger?
Many thanks.

trigger TriggerUpdateContractOwnerFromAcount on Account (after Update) {
for(Account Acc :Trigger.new){

List<Contract> C = [select id, ownerid from Contract where Account.id = :Acc.Id ];

for (Contract Ctrct : C)

If(Ctrct.Ownerid != Acc.Ownerid){
{
Ctrct.Ownerid= Acc.Ownerid;
}
update C;
}
}
}
Best Answer chosen by Ludivine
lakslaks
Hi,

You can do that by adding a boolean (Checkbox) field to the object inorder to determine whether validation rule needs to be bypassed or not.

Set it to 'True' in your trigger.
And in the Validation rule include logic to check the value in the field. If it's 'True' do not perform the Validation.

Lakshmi.

All Answers

Ramu_SFDCRamu_SFDC
Firstly to add an error using adderror method, you need to write a before error trigger. Below post shows an example of how you can add an error using adderror method.

https://developer.salesforce.com/forums?id=906F00000008zcBIAQ
LudivineLudivine
Thanks,
I have changed a bit my trigger but it doesn't change anything on the windows.
This error is displayed on the windows that is called to changethe name of Account Owner :

My trigger :
trigger TriggerUpdateContractOwnerFromAcount on Account (Before Update) {
for(Account Acc :Trigger.new){

List<Contract> C = [select id, ownerid from Contract where Account.id = :Acc.Id ];

for (Contract Ctrct : C)
if(Ctrct.EndDate == null){
Acc.Owner.Adderror('Please complete mandatory fields on Contract before Changing the Account Owner');

If(Ctrct.Ownerid != Acc.Ownerid){
Ctrct.Ownerid= Acc.Ownerid;
}
//add error message if mandatory fields are empty in related contract
//adderror();
update C;
}
}
}
The error :
Error: Apex trigger TriggerUpdateContractOwnerFromAcount caused an unexpected exception, contact your administrator: TriggerUpdateContractOwnerFromAcount: execution of BeforeUpdate

caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contract.EndDate: Trigger.TriggerUpdateContractOwnerFromAcount: line 7, column 1


lakslaks
Hi Syntaxis,

The exception that you are getting above is because you have used the field EndDate of the object Contract for the check if(Ctrct.EndDate == null), however you have not queried for the field EndDate in List<Contract> C = [select id, ownerid from Contract where Account.id = :Acc.Id ];

Once you include EndDate in the query - List<Contract> C = [select id, ownerid,  EndDate from Contract where Account.id = :Acc.Id ]; it should be fine.

Hope this helps.

Lakshmi.
LudivineLudivine
Hi,

I was wondering finally if it was possible to ignore the validation rules on Contract page and Update the contact Owner?
It would be fantastic in fact and maybe easier to write?
lakslaks
Hi,

You can do that by adding a boolean (Checkbox) field to the object inorder to determine whether validation rule needs to be bypassed or not.

Set it to 'True' in your trigger.
And in the Validation rule include logic to check the value in the field. If it's 'True' do not perform the Validation.

Lakshmi.

This was selected as the best answer
lakslaks
Please do not forget to mark the 'Best answer' if your issue has been addressed correctly.
LudivineLudivine
Done! Many thanks for your help Iaks
lakslaks
Happy to be of help :-)