function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nagasnagas 

error message in a trigger not displaying

hi 

 

i ahve written a small trigger which will block the deletion of a "territory" if it finds a matching record in another object "userterritory". The trigger is working fine with that but the error message which it supposed to display is not displaying. Its displaying some other message standard from salesforce which is not related to this context.

 

 

trigger dontdel on Territory (before delete) {

 Territory ter = new Territory();
 List<userterritory> uter = [select territoryid from userterritory where territoryid=:Trigger.old[0].id];

 if(uter.size()>0)

     Trigger.old[0].id.addError('You cannot delete this territory since you have assigned users to this');
  
  

}

 its working fine its not allowing to delete the territory but the error message which it is displaying is

 

 

Cannot Delete the Territory
You cannot delete the territory because at least one territory rolls up to it. 

Click here to return to the previous page.

 

this message should be displayed when we trying to delete a parent terrirory with childs.

 

 

Pradeep_NavatarPradeep_Navatar

Try out this sample code given below :

 

                                                trigger dontdelacc on Account(before delete)

                                                {

                                                 Account acc = new Account();

                                                 system.debug('############ '  + Trigger.old[0].id);

                                                 List<Contact> con = [select AccountId from Contact where AccountId=:Trigger.old[0].id];

                                                 if(con.size()>0)

                                                {

                                                    Trigger.old[0].id.addError('You cannot delete this Account since you have assigned account to this');

                                                }

 

                                                }

 

Hope this helps.

nagasnagas

hi pradeep the trigger is working on Account and contacts, But coming to Terrirtory I am having the problem dont know whats going on.

_Prasu__Prasu_

Did you tried following? You are adding the error at field level. Try adding it to object level.

 

Trigger.old[0].addError('You cannot delete this territory since you have assigned users to this');
nagasnagas

i tried that too no use.something is overriding the message, i checked there are no validation rules or nay other triggers on territory dont know whats happening.