[SalesForce] Make Lookup fields read only on a VF page

I have a VF page for creating a new record for a custom Object. It looks like this
VF page

There is a lookup to Service_Contract__c. I needed to autopopulate it (which was the whole reason behind creating VF page. After autopopulating it, i don't want user to change it. So i want to make it a read-only field at the page level (not the field level).

Any suggestions would be helpful. I am attaching my VF page code and the two things that i have tried.

VF page code –

<apex:page standardController="Transaction_Line_Item__c" recordSetVar="Transaction_Line_Item" extensions="SS_CreateTransactionLineItemController" action="{!getSCandCLI}" title="Transaction Line Item Edit: New Transaction Line Item">
  <apex:form >
  <apex:sectionHeader title="Transaction Line Item Edit" subtitle="New Transaction Line Item"/>
      <apex:pageBlock title="Transaction Line Item Edit" mode="edit">
          <apex:pageBlockButtons >
              <apex:commandButton action="{!save}" value="Save"/>
              <apex:commandButton action="{!SaveAndNew}" value="Save & New"/>
              <apex:commandButton action="{!cancel}" value="Cancel"/>
          </apex:pageBlockButtons>
          <apex:outputPanel rendered="{!displayError}">
            <div class="pbError">
                Error: Invalid Data. <br/>
                Review all error messages below to correct your data. <br/>
                {!errorMessage}
            </div>
          </apex:outputPanel>
          <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:pageBlock>

  </apex:form>    

</apex:page>

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 Boolean displayError{get;set;}
    public String errorMessage{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(Exception ex){
            displayError = true;
            if(ex.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION, Service')){
                List<String> splitString =  ex.getMessage().split('FIELD_CUSTOM_VALIDATION_EXCEPTION, ');
                splitString = splitString[1].split(': \\[');
                errorMessage = splitString[0];
            }
            return null;
        }
        PageReference pg = new PageReference('/'+contractLineItemId);
        pg.setRedirect(true);
        return pg;
    }

    /*
     * Method to save the transactionLineItem record and create another transactionLineItem record.
     */
    public PageReference SaveAndNew(){
        try{
            database.insert(TransactionRecord);
        }catch(Exception ex){
            displayError = true;
            if(ex.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION, Service')){
                List<String> splitString =  ex.getMessage().split('FIELD_CUSTOM_VALIDATION_EXCEPTION, ');
                splitString = splitString[1].split(': \\[');
                errorMessage = splitString[0];
            }
            return null;
        }
        PageReference pg = new PageReference(ApexPages.currentPage().getUrl());
        pg.setRedirect(true);
        return pg;
    }

}

If I add html-disabled=true, <apex:inputField value="{!TransactionRecord.Service_Contract__c}" html-disabled="true"/> , it makes it read-only in a way that no-body can type anything into it via keyboard. That lookup button is still next to it, and anyone can use it to change the Service Contract associated with this lookup.

If i change it to inputText, and use disabled="true" … <apex:inputText value="{!TransactionRecord.Service_Contract__c}" disabled="true"/> ….it makes it truly read-only, that lookup icon is gone and no-one can type anything in there either. But shows the actual ServiceContract ID (that, instead of name of the service contract.

Best Answer

Try instead using apex:outputField:

<apex:outputField value="{!TransactionRecord.Service_Contract__c}" />