Make fields mandatory when status is complete












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.










share|improve this question









New contributor




koduri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Why don't you use a validation rule for this? Much simpler solution.
    – Folkert
    2 days ago
















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.










share|improve this question









New contributor




koduri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Why don't you use a validation rule for this? Much simpler solution.
    – Folkert
    2 days ago














1












1








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.










share|improve this question









New contributor




koduri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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.







visualforce controller pagemessages






share|improve this question









New contributor




koduri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




koduri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









cloudZigZag

692316




692316






New contributor




koduri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









koduri

92




92




New contributor




koduri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





koduri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






koduri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Why don't you use a validation rule for this? Much simpler solution.
    – Folkert
    2 days ago


















  • Why don't you use a validation rule for this? Much simpler solution.
    – Folkert
    2 days ago
















Why don't you use a validation rule for this? Much simpler solution.
– Folkert
2 days ago




Why don't you use a validation rule for this? Much simpler solution.
– Folkert
2 days ago










3 Answers
3






active

oldest

votes


















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")





share|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;
    }





    share|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;
      }





      share|improve this answer





















      • Thank you pranay jaiswal for the reply,but unfortunately its not working.
        – koduri
        2 days ago










      • @koduri have you added <Apex:pageMessages/> tag in VF?
        – Pranay Jaiswal
        2 days ago










      • I added this tag in vf page..still it is not working Pranay Jaiswal
        – koduri
        2 days ago










      • can you edit your code and provide VF snippets as well?
        – Pranay Jaiswal
        2 days ago











      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "459"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      koduri is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f244742%2fmake-fields-mandatory-when-status-is-complete%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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")





      share|improve this answer




























        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")





        share|improve this answer


























          1












          1








          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")





          share|improve this answer














          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")






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          cloudZigZag

          692316




          692316

























              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;
              }





              share|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;
                }





                share|improve this answer


























                  1












                  1








                  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;
                  }





                  share|improve this answer














                  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;
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 days ago

























                  answered 2 days ago









                  Santanu Boral

                  30.4k52152




                  30.4k52152























                      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;
                      }





                      share|improve this answer





















                      • Thank you pranay jaiswal for the reply,but unfortunately its not working.
                        – koduri
                        2 days ago










                      • @koduri have you added <Apex:pageMessages/> tag in VF?
                        – Pranay Jaiswal
                        2 days ago










                      • I added this tag in vf page..still it is not working Pranay Jaiswal
                        – koduri
                        2 days ago










                      • can you edit your code and provide VF snippets as well?
                        – Pranay Jaiswal
                        2 days ago
















                      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;
                      }





                      share|improve this answer





















                      • Thank you pranay jaiswal for the reply,but unfortunately its not working.
                        – koduri
                        2 days ago










                      • @koduri have you added <Apex:pageMessages/> tag in VF?
                        – Pranay Jaiswal
                        2 days ago










                      • I added this tag in vf page..still it is not working Pranay Jaiswal
                        – koduri
                        2 days ago










                      • can you edit your code and provide VF snippets as well?
                        – Pranay Jaiswal
                        2 days ago














                      0












                      0








                      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;
                      }





                      share|improve this answer












                      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;
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 2 days ago









                      Pranay Jaiswal

                      13.3k32351




                      13.3k32351












                      • Thank you pranay jaiswal for the reply,but unfortunately its not working.
                        – koduri
                        2 days ago










                      • @koduri have you added <Apex:pageMessages/> tag in VF?
                        – Pranay Jaiswal
                        2 days ago










                      • I added this tag in vf page..still it is not working Pranay Jaiswal
                        – koduri
                        2 days ago










                      • can you edit your code and provide VF snippets as well?
                        – Pranay Jaiswal
                        2 days ago


















                      • Thank you pranay jaiswal for the reply,but unfortunately its not working.
                        – koduri
                        2 days ago










                      • @koduri have you added <Apex:pageMessages/> tag in VF?
                        – Pranay Jaiswal
                        2 days ago










                      • I added this tag in vf page..still it is not working Pranay Jaiswal
                        – koduri
                        2 days ago










                      • can you edit your code and provide VF snippets as well?
                        – Pranay Jaiswal
                        2 days ago
















                      Thank you pranay jaiswal for the reply,but unfortunately its not working.
                      – koduri
                      2 days ago




                      Thank you pranay jaiswal for the reply,but unfortunately its not working.
                      – koduri
                      2 days ago












                      @koduri have you added <Apex:pageMessages/> tag in VF?
                      – Pranay Jaiswal
                      2 days ago




                      @koduri have you added <Apex:pageMessages/> tag in VF?
                      – Pranay Jaiswal
                      2 days ago












                      I added this tag in vf page..still it is not working Pranay Jaiswal
                      – koduri
                      2 days ago




                      I added this tag in vf page..still it is not working Pranay Jaiswal
                      – koduri
                      2 days ago












                      can you edit your code and provide VF snippets as well?
                      – Pranay Jaiswal
                      2 days ago




                      can you edit your code and provide VF snippets as well?
                      – Pranay Jaiswal
                      2 days ago










                      koduri is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      koduri is a new contributor. Be nice, and check out our Code of Conduct.













                      koduri is a new contributor. Be nice, and check out our Code of Conduct.












                      koduri is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to Salesforce Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f244742%2fmake-fields-mandatory-when-status-is-complete%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy