In a visualforce page I want to render help text dynamically.

Suppose if some field is blank and the user tries to save the record I want to show that help text.

But I want this to happen only once, the next time when user click the save and that field is empty I don't want the message to render.


You'll need to use a custom controller or a controller extension to do this, and then just use a simple flag to check these fields on save.

// In the controller
private Boolean CheckDone = false;

public PageReference DoSave()
  Boolean AllGood = true;

    // check the fields in question
    if(SomeRecord.Field__c == '')
      ApexPages.AddPageMessage(new ApexPages.PageMessage(ApexPages.Severity.Info, 'You haven't entered a value for the field Field__c.');
      AllGood = false;
    else if(SomeRecord.OtherField__c == '')
      // etc.

  if(CheckDone || AllGood)
    // Save the record
    upsert SomeRecord;
    CheckDone = false;
    return SomePageReferenceOrNull;

  CheckDone = true;
  return null;

In the page you just need to be sure to include a messages tag and to use your custom Save action for the button being used:


    <apex:commandButton action="{!DoSave}" value="Save"/>

Note I've written this code in-browser, so can't be sure that it'll compile as is, but I hope it demonstrates the technique effectively.

  • i think you have interpreted question wrongly.i have pageblocksectionitem inthis i have used helptext attribute.i want to show this helptext on click of save button.i have taken a falg to control this (as i want to show helptext once only when user click save button) but i am not able to make it show.what script i need to write for that? – Pankaj Mar 28 '13 at 13:31
  • 2
    Ah I see. I think your best bet would be to use jQuery to find the relevant IDs for the help text and then to modify their display attribute, but it would be an unsupported and somewhat hacky solution that would break if Salesforce every changed the markup used. I'd strongly advise against doing that and use something similar to my answer which just relies on standard functionality. You could even grab the help text from describe information to put it into the page messages. – Matt Lacey Mar 29 '13 at 0:35
  • You could use a validation rule, but normally a validation rule would be persistent. It would display every time the record was blank unless you could incorporate an If statement that had an "OR count > 1" with the count being linked to your save button for that pageblocksectionitem. I've not tried it, but it's possible something like that might work for your purposes. Don't know if that helps, but it might point you in a direction that will. – crmprogdev Mar 29 '13 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.