1

I have a status field on contact,when status is complete,remarks should get mandatory..I wrote if condition for this in controller,but it is not taking remarks field as mandatory..how to write this? please help

My code is: Vf page:

<apex:inputField value="{!con1.Status__c}"  />
 <apex:inputField value="{!con1.Any_Remarks__c}" />
   <apex:commandButton value="save" action="{!saveActivity}"/>

Controller:

public PageReference saveActivity(){
   if(con1.Status__c=='Complete' && con1.Any_Remarks__c==''){
        apexpages.addMessage(new ApexPages.message(apexpages.Severity.ERROR,'Remarks is mandatory'));
    }
 insert con1;
}

Thanks & regards, usha.

  • Why don't you use a validation rule for this? Much simpler solution. – Folkert Dec 27 '18 at 11:45
1

Return type is missing in if condition. add return null keyword after the addmessage line.

 if(con1.Status__c=='Complete' && String.isBlank(con1.Any_Remarks__c)){
    apexpages.addMessage(new ApexPages.message(apexpages.Severity.ERROR,'Remarks is mandatory'));
    return null;
   }

And on Visualforce page you have to use

< apex:pageMessages /> tag.

Another way you can achieved by Validation rule, or go to that field Any_Remarks__c and click on edit and check the required checkbox, no need to write apex code.

Validation rule like this

AND(Isblank(Any_Remarks__c), TEXT(Status__c)="Complete")

if Status__c is picklist field then your validation rule like this

AND(Isblank(Any_Remarks__c),ISPICKVAL(Status__c, "Complete")
| improve this answer | |
1

Since you want to show the message in VFP so declare <apex:pageMessages id="appMessages"/> and use rerender to display that.

Otherwise,apexpages.addMessage() will collect the message but couldn't able to display that.

<apex:pageMessages id="appMessages"/>
<apex:inputField value="{!con1.Status__c}"  />
<apex:inputField value="{!con1.Any_Remarks__c}" />
<apex:commandButton value="save" action="{!saveActivity}" reRender="appMessages"/> 

Controller

Use a logic to verify apexpages.hasMessages() before performing DML statement, so that you can add other error messages through apexpages.addMessage()

public PageReference saveActivity()
{
       if(con1.Status__c=='Complete' && con1.Any_Remarks__c=='')
       {
            apexpages.addMessage(new ApexPages.message(apexpages.Severity.ERROR,'Remarks is mandatory'));
       }

       //add more validation rule if that is required.
       if(!apexpages.hasMessages())
       {
            insert con1;
       }

    return null;
}
| improve this answer | |
0

You have to return null after checking for validation. This will make your page-messages show up on VF page. As per your current code you are checking and still inserting record.

public PageReference saveActivity(){

   if(con1.Status__c=='Complete' && String.isBlank(con1.Any_Remarks__c)){
        apexpages.addMessage(new ApexPages.message(apexpages.Severity.ERROR,'Remarks is mandatory'));
        return null;
   }
    insert con1;
}
| improve this answer | |
  • Thank you pranay jaiswal for the reply,but unfortunately its not working. – koduri Dec 27 '18 at 11:56
  • @koduri have you added <Apex:pageMessages/> tag in VF? – Pranay Jaiswal Dec 27 '18 at 13:29
  • I added this tag in vf page..still it is not working Pranay Jaiswal – koduri Dec 28 '18 at 4:17
  • can you edit your code and provide VF snippets as well? – Pranay Jaiswal Dec 28 '18 at 4:18

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.