2

Summary of what i am doing - I have a standard Object - ServiceContract, its child (detail) standard object ContractLineItem, and a custom object (Transaction_Line_Item__c) which is a detail to ContractLineItem. So basically: ServiceContract (GrandDaddy) -> ContractLineItem (Parent) -> Transaction_Line_Item__c (Child)

Important Fields on Transaction_Line_Item__c object -

  • Service_Contract__c (Lookup to grandaddy, REQUIRED at field level)
  • Contract_Line_Item__c (Lookup to parent, REQUIRED at field level)
  • Start_Date__c, End_Date__c, Points__c (REQUIRED at field level)
  • Point_Status__c (Picklist, Validation rule exists to make sure it cannot be left blank, validation rule also specifies for error to pop up at field level)

Goal is to create a custom "New Transaction Line Item" button which auto-populates the two lookups. While i can accomplish that, i am having hard time displaying proper errors incase of some field left blank.

Here's the code for VF page, and its controller which will show error for that Point_Status__c field ONLY if other required fields are filled out. If one or more of the other required fields are left blank, it shows the error for them, but not for Point_Status__c field.

If i use Apex:pageMessages.Add in the controller (where insert is performed) and use Apex:pageMessages in VF page, all errors show at the top as well as at the field level. I don't want them at the top.

VF Page code -

<apex:page standardController="Transaction_Line_Item__c" recordSetVar="Transaction_Line_Item" extensions="SS_CreateTransactionLineItemController" action="{!getSCandCLI}"> <style>   body .bPageBlock .pbBody .grey .pbSubheader{
    background-color:#AFD98C;   } </style>   <apex:form >   <apex:sectionHeader title="Transaction Line Item Edit" subtitle="New Transaction Line Item"/>
      <apex:pageBlock title="Transaction Line Item Edit" >

          <apex:pageBlockButtons >
              <apex:commandButton action="{!save}" value="Save"/>
              <apex:commandButton action="{!cancel}" value="Cancel"/>
          </apex:pageBlockButtons>
          <apex:outputPanel styleClass="grey" layout="block">
          <apex:pageBlockSection columns="2" title="Information" collapsible="false" >
               <apex:inputField value="{!TransactionRecord.Service_Contract__c}"/>
               <apex:inputField value="{!TransactionRecord.Start_Date__c}"/>
               <apex:inputField value="{!TransactionRecord.Contract_Line_Item__c}"/>
               <apex:inputField value="{!TransactionRecord.End_Date__c}"/>
               <apex:inputField value="{!TransactionRecord.Type__c}"/>
               <apex:pageBlockSectionItem />
               <apex:inputField value="{!TransactionRecord.Points__c}"/>
               <apex:pageBlockSectionItem />
               <apex:inputField value="{!TransactionRecord.Point_Status__c}"/>
               <apex:pageBlockSectionItem />
               <apex:inputField value="{!TransactionRecord.Case__c}"/>
          </apex:pageBlockSection>
          </apex:outputPanel>
      </apex:pageBlock>
         </apex:form>        </apex:page>

Code for controller -

/*
    Class to create transactionLineItems for ContractLineItems
*/
public class SS_CreateTransactionLineItemController{
    public Id contractLineItemId{get;set;}
    public Id ServiceContractId{get;set;}
    public Transaction_Line_Item__c TransactionRecord{get;set;}
    public SS_CreateTransactionLineItemController(ApexPages.StandardSetController controller) {
        contractLineItemId = ApexPages.currentPage().getParameters().get('id');
    }

    /*
     * Method to fetch related ContractLineItemId and ServiceContractId
     */
    public void getSCandCLI(){
        TransactionRecord = new Transaction_Line_Item__c();
        ServiceContractId = [select Id,ServiceContractId from ContractLineItem WHERE Id=:contractLineItemId].ServiceContractId;
        TransactionRecord.Contract_Line_Item__c = contractLineItemId;
        TransactionRecord.Service_Contract__c = ServiceContractId;
    }

    /*
     * Method to save the transactionLineItem record and return it to related ContractLineItem page.
     */
    public PageReference save(){
        try{
            database.insert(TransactionRecord);
        }catch(DMLException ex){
            return null;
        }
        PageReference pg = new PageReference('/'+contractLineItemId);
        pg.setRedirect(true);
        return pg;
    }
}

Result if I leave Point Status blank and fill out everything, and press "Save" .. everything looks fine Only Point Status left blank

But if i leave any of the other required field blanks (along with point_Status field still blank), it doesn't show the error about point status field, as shown below-

When Some required field is also left blank, it doesn't show error for Point Status

Any suggestions would be helpful.

2

As part of visualforce page form posting, it first checks form data integrity (required fields, data type etc.). Only once it is all qualified, form is posted and underlying controller is invoked.

In this case, Start date is a required field and is stopped at the form posting level itself, and hence none of the other rules (validation rules, apex code) is invoked and you see only 1 error related to "Start Date"

  • OK. That clarifies it. But then how do i address this situation? I want all errors to be displayed .... validation as well as required field errors. – user2957592 May 18 '15 at 22:24
  • one straightforward thing that you can check is to mark the status control as required at page level <apex:inputField value="{!TransactionRecord.Point_Status__c}" required="true"/> – Anshul May 19 '15 at 0:27

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.