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
Anusha VankayalapatiAnusha Vankayalapati 

ApexPages.addMessage can only be called from a Visualforce page ()

Hi Team,
I have received the below exception mail from my org. Can anyone please let me know why this error is thrown.
execution of AfterUpdate caused by: System.FinalException: ApexPages.addMessage can only be called from a Visualforce page ()
 
Hemant_SoniHemant_Soni
Hi,
It seems Trigger and VF page both are using same apex class and in apex class ApexPages.addMessage is used for vf page message which will not work for trigger.

Quick Solution would be add this ApexPages.addMessage in try catch.

Thanks
Hemant 
Anusha VankayalapatiAnusha Vankayalapati
Hi @Hemant_Soni,

Thanks for your response. But I am sure that this is not used in VF page. This ' ApexPages.addMessage ' is used in an apex class which is used from trigger.  ApexPages.addMessage is kept in try catch block. Please let me know why this exceprion is thrown.

Thanks-in-advance.
Hemant_SoniHemant_Soni
Can you please share your apex class.