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
matt.bylermatt.byler 

Validating fields in an object when clicking a button

I do not know if this is possible or not but decided to post and see if anyone has done this before. What I am trying to do is when a user clicks on a button it validates that certain fields are not blank. If there are blank fields it should disply an error message quite similar to validation rules. Let me know if there is any way I can do this.

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

SObject myObj = something;

 

Controller

if (myObj.aField == null)
{
  ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'aField requires a value.');
}
//You can do this for any number of fields.

 Page

<apex:pageMessages />

All Answers

Damien_Damien_

SObject myObj = something;

 

Controller

if (myObj.aField == null)
{
  ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'aField requires a value.');
}
//You can do this for any number of fields.

 Page

<apex:pageMessages />
This was selected as the best answer
Starz26Starz26

Check out Bob's blog posts here related to this. If it is not what you require, please provide more detail.

 

 

Part 1 (sObject Fields)

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

 

Part 2 (Non sObject Fields)

 

http://bobbuzzard.blogspot.com/search?q=Field+Level+Error+Messages+with+Visualforce