Make fields mandatory when status is complete

Multi tool use
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
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.
add a comment |
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
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
1 hour ago
add a comment |
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
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
visualforce controller
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.
edited 1 hour ago


sanket kumar
2,4182322
2,4182322
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 1 hour ago


koduri
52
52
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
1 hour ago
add a comment |
Why don't you use a validation rule for this? Much simpler solution.
– Folkert
1 hour ago
Why don't you use a validation rule for this? Much simpler solution.
– Folkert
1 hour ago
Why don't you use a validation rule for this? Much simpler solution.
– Folkert
1 hour ago
add a comment |
3 Answers
3
active
oldest
votes
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;
}
add a comment |
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")
add a comment |
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;
}
Thank you pranay jaiswal for the reply,but unfortunately its not working.
– koduri
1 hour ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
edited 1 hour ago
answered 1 hour ago


Santanu Boral
30.4k52152
30.4k52152
add a comment |
add a comment |
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")
add a comment |
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")
add a comment |
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")
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")
edited 51 mins ago
answered 1 hour ago


cloudZigZag
675315
675315
add a comment |
add a comment |
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;
}
Thank you pranay jaiswal for the reply,but unfortunately its not working.
– koduri
1 hour ago
add a comment |
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;
}
Thank you pranay jaiswal for the reply,but unfortunately its not working.
– koduri
1 hour ago
add a comment |
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;
}
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;
}
answered 1 hour ago


Pranay Jaiswal
13.2k32351
13.2k32351
Thank you pranay jaiswal for the reply,but unfortunately its not working.
– koduri
1 hour ago
add a comment |
Thank you pranay jaiswal for the reply,but unfortunately its not working.
– koduri
1 hour ago
Thank you pranay jaiswal for the reply,but unfortunately its not working.
– koduri
1 hour ago
Thank you pranay jaiswal for the reply,but unfortunately its not working.
– koduri
1 hour ago
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
mmgGW54bqrwrPzR1jGem Y,jc,lphceN,BD52WhRYM8GTPQq,1xPM7qc tFEj69Jr,gCE 4maO3a x,G ubLbmwyRtl,8ejbdV,WTJzsZ
Why don't you use a validation rule for this? Much simpler solution.
– Folkert
1 hour ago