0

I have a VF page on VF page.There are few validation rules on Opportunity object,Which are throwing as exceptions after clicking on save.Instead i want to display error on VF page.

VF page:

<apex:page standardController="Opportunity" extensions="CreateOppExtension"  >
  <apex:sectionHeader title="Opportunity Edit" subtitle="New Opportunity Edit"/>

    <apex:form id="theForm" >
        <apex:pageBlock title="Opportunity Edit" mode="save">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save" />
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
                       <apex:outputPanel styleClass="red" layout="block" >

                    <apex:inputField value="{!Opportunity.Name}"/>
                     <apex:outputText value="{!Owner}" label="Oppourtunity Owner" />
                    <apex:inputField value="{!Opportunity.Opportunity_Type__c}" required="true"/>
                    <apex:inputField value="{!Opportunity.Process_Quadrant__c}"/>
                    <apex:inputField value="{!Opportunity.Start_Date__c}" />
                    <br/> 
                    <apex:inputField value="{!Opportunity.CloseDate}"/>
                    <apex:inputField value="{!Opportunity.Customer_Group_ERP__c}" required="true"/>
                    <br/>
                    <apex:inputField value="{!Opportunity.Device__c}"/>
                    <apex:inputField value="{!Opportunity.PM_I_Business_Field__c}" required="true"/>
                    <apex:inputField value="{!Opportunity.Node_Size__c}"/>
                    <apex:inputField value="{!Opportunity.Main_Group_GPH1__c}" required="true" />
                    <apex:inputField value="{!Opportunity.TopProject__c}"/>
                    <apex:inputField value="{!Opportunity.Product__c}"/>
                    <br/> 
                    <apex:inputField value="{!Opportunity.USP__c}"/>
                </apex:pageBlockSection>

                <apex:pageBlockSection title="References" columns="2" collapsible="false">
                 <font color="#000000">
                     <apex:facet name="header">
                          <span style="color:black">References </span>
                     </apex:facet>
                 </font>  
                    <apex:inputField label="Account Name" value="{!opportunity.AccountId}" required="true"/>
                    <apex:inputField value="{!Opportunity.R_D_Project__c}"/>
                    <apex:inputField value="{!Opportunity.Master_Account__c}" />
                    <apex:inputField value="{!Opportunity.Project_ID__c}"/>
                    <br/>
                    <apex:inputField value="{!Opportunity.NBD_ID__c}"/>                        
                 </apex:pageBlockSection>

                <apex:pageBlockSection title="Sales Cycle" columns="2" collapsible="false">
                 <font color="#000000">
                     <apex:facet name="header">
                          <span style="color:black">Sales Cycle</span>
                     </apex:facet>
                 </font>  
                    <apex:inputField value="{!Opportunity.StageName}"/>
                    <apex:inputField value="{!Opportunity.Monthly_Progress__c}" required="true"/>
                    <apex:inputField value="{!Opportunity.Status__c}" />        
                    <apex:inputField value="{!Opportunity.Counter_Measures__c}"/>
                    <apex:inputField value="{!Opportunity.Reason_PM_I__c}"/>
                    <apex:inputField value="{!Opportunity.Price_EUR_Unit__c}"/>
                    <apex:inputField value="{!Opportunity.Sub_Reason__c}"/>
                    <apex:inputField value="{!Opportunity.Dispense_Volume_mUnit__c}"/>
                    <apex:inputField value="{!Opportunity.Probability}"/>
                    <apex:inputField value="{!Opportunity.Layers__c}"/>
                    <apex:inputField value="{!Opportunity.Expected_Rev_rHVM__c}"/>
                    <apex:inputField value="{!Opportunity.Unit__c}"/> 
                    <apex:inputField value="{!Opportunity.Exp_Sales_Vol_p_a_EUR__c}"/>  
                    <apex:inputField value="{!Opportunity.R_D_Headcount__c}"/>
                    <apex:inputField value="{!Opportunity.PMI_In_Forecast__c}"/>
                </apex:pageBlockSection>

Controller:

public class CreateOppExtension 
{
    Opportunity opp{set;get;}
    List<Opportunity> opp1 {set;get;}
    String Owner {get;set;}
    public id AccountId;



    public CreateOppExtension(ApexPages.StandardController stdController)
      {   
      //  GstdController= stdController;
        opp = (Opportunity)stdController.getRecord();
         opp.Notes__c = 'Testing is done in this Profile,It should be visible for only PM-I record type.';

         opp.AccountId =apexpages.currentpage().getparameters().get('AccountId');
         opp.Master_Account__c=apexpages.currentpage().getparameters().get('AccountId');

        }


    public PageReference save() {
        insert opp;

         Pagereference ref= new PageReference('/'+opp.Id);
                            return ref;

    }

    public String getowner()
     {
         Owner= UserInfo.getName();
         return owner;
     }

}

Validation rules:

Counter measures- IF( AND( Counter_Measures__c="", RecordTypeId='012b0000000M8s5', ISPICKVAL(Monthly_Progress__c, "Red") ) , true, false)

Reason-

RecordType.Id == '012b0000000M8s5' && NOT(ISPICKVAL( Status__c ,'Open')) && ISPICKVAL(Reason_PM_I__c,'') && NOT(CONTAINS($Profile.Name, "Administrator"))

Please find the exceptions errors. enter image description here

enter image description here

Please help how to acheive to display the error on page,based on conditions of validation rules.Thanks in advance

3 Answers 3

0

You can use <apex:pageMessages/>

More here ->

0

You can do with help of <apex:messages />

place <apex:messages /> in visual force Page.

In Apex controller pass the error message, example Like below .

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value'));
3
  • In Controller i have added public PageReference save() { if(opp.Monthly_Progress__c == 'Red' && opp.RecordTypeId =='012b0000000M8s5' ) { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Enter Counter Measure when Monthly Progress = Red.')); } insert opp; Pagereference ref= new PageReference('/'+opp.Id);
    – User 7864
    Commented Aug 9, 2017 at 11:00
  • <apex:messages /> in VF page It is not working
    – User 7864
    Commented Aug 9, 2017 at 11:00
  • Please updated the question what u have tried. usually it is working
    – NITHESH K
    Commented Aug 9, 2017 at 13:01
0

@Diljaan Shaik Hey you can use javascript function to show error message on the input field. You can view this link for better idea Adding Error Message to Input Text using javascript/jquery in Visualforce

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .