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
Hiteshkumar Chaudhari 2Hiteshkumar Chaudhari 2 

how to throw error messege after evaluating a condition in apex code

how to throw error messege after evaluating a condition in apex code


i want to throw error before executing an opeartion 

  if(b1.Amount__c != Decimal.valueof(wamount))
{
    // how to throw error msg in a visualforce page
}
Best Answer chosen by Hiteshkumar Chaudhari 2
sfdcMonkey.comsfdcMonkey.com
i think you have any mistake in your code
here is basic visualforce page and controller code try this as create dummy page and controller
<apex:page controller="errorMessage">
    <apex:pageMessages />
    test page
</apex:page>
public class errorMessage {
  
    public errorMessage(){
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'your page is load.'));
    }
}
output -:
User-added image
when your component is load you got this error message
let me inform if it helps you
thanks


 

All Answers

Hiteshkumar Chaudhari 2Hiteshkumar Chaudhari 2
no dear it dosnt give any sort of message in my case

here's my code : 
public PageReference withdrawn()
    {
         BankApplication__c b1 = [Select id,Amount__c from BankApplication__c where BankApplication__c.Name =: wname]; 
          
        if(b1.Amount__c >= Decimal.valueof(wamount))
          {
              b1.Amount__c  = b1.Amount__c - Decimal.valueof(wamount);
              update b1;
          
          }
          else
          {
               b1.addError('error message');
          }
    
Hiteshkumar Chaudhari 2Hiteshkumar Chaudhari 2
sorry piyueh but i didnt find any effect of above code .
sfdcMonkey.comsfdcMonkey.com
i think you have any mistake in your code
here is basic visualforce page and controller code try this as create dummy page and controller
<apex:page controller="errorMessage">
    <apex:pageMessages />
    test page
</apex:page>
public class errorMessage {
  
    public errorMessage(){
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'your page is load.'));
    }
}
output -:
User-added image
when your component is load you got this error message
let me inform if it helps you
thanks


 
This was selected as the best answer
Hiteshkumar Chaudhari 2Hiteshkumar Chaudhari 2
Yes now its working Thanks for your great support Piyush Soni.
 
sfdcMonkey.comsfdcMonkey.com
Pleasure :) & what is status of your below queue 
https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XgwDQAS

Let me inform if you have any issue with it 

Thanks