My VF Page Code:

<apex:page standardController="CM__c" extensions="ContractButtonsController" recordSetVar="contracts" action="{!openpage}">


My Controller Code:

public class ContractButtonsController {

    public Opportunity opp{get; set;}
    public Account acc{get; set;}
    public List<CM_Contract__c> getParentContract;
    public string parentContractName{get; set;}
    public String parentContractId{get; set;}
    public id opportunityid;

    public Gen_CM_ContractButtonsController(ApexPages.StandardController controller) {
    public Gen_CM_ContractButtonsController(ApexPages.StandardSetController controller) {
        opportunityid = apexpages.currentpage().getparameters().get('id');
        if(opportunityid!= null) {
            //Query to get account id of a opportunity
            opp = [select id, account.id, account.name, name from opportunity where id =: opportunityid limit 1];

        getParentContract = new List<CM_Contract__c>();
        if(opportunityid!= null) {
            //Query to get Parent contract name and parent contract id for the account
            getParentContract = [select name, id, Opportunity__r.Account.Id from CM_Contract__c where Opportunity__c =: opportunityid and account__r.id =: opp.account.id and Opportunity__r.Master_Opportunity__c =: opp.name  limit 1];

            parentCOntractName = getParentContract[0].name;
            System.debug('Name' +parentContractName);
            parentContractId = getParentContract[0].id;



    public pagereference openpage() {
        PageReference newPage;
        if( '{!opp.RecordType}' == 'Single Account Opportunity' || '{!opp.RecordType}' == 'Multi Account Opportunity' ) {

            newPage = new PageReference('/a2S/e?{!CM_Contract__c.ContractStartDate__c}=&{!CM_Contract__c.CustomerSignatureDate__c}=&{!CM_Contract__c.ExternalLink__c}=&{!CM_Contract__c.ImplementationDate__c}=&{!CM_Contract__c.ContractEffectiveDate__c}=&{!CM_Contract__c.External_Contract_ID__c}=&{!CM_Contract__c.Documents_Attached_Saved__c}=&{!CM_Contract__c.Approved_Revision_Number__c}=&{!CM_Contract__c.Archive_Reference__c}=&03db0000000LbQI=&{!$Setup.CS_ContractFieldIdConfiguration__c.CustomerSignature_Field_Id__c}=&{!$Setup.CS_ContractFieldIdConfiguration__c.CustomerSignature_Field_Id__c}_lkid=&{!$Setup.CS_ContractFieldIdConfiguration__c.Account_Field_Id__c}={!acc.Name}&{!$Setup.CS_ContractFieldIdConfiguration__c.Account_Field_Id__c}_lkid={!opp.AccountId}&{!$Setup.CS_ContractFieldIdConfiguration__c.Opportunity_Field_Id__c}={!opp.Name}&{!$Setup.CS_ContractFieldIdConfiguration__c.Opportunity_Field_Id__c}_lkid={!opp.Id}&{!$Setup.CS_ContractFieldIdConfiguration__c.ContractNegotiator_Field_Id__c}=&{!$Setup.CS_ContractFieldIdConfiguration__c.ContractNegotiator_Field_Id__c}_lkid=&CF0Nb0000009qGMV=&CF0Nb0000009qGMV_lkid=&ent={!$Setup.CS_ContractFieldIdConfiguration__c.Opportunity_Object_Id__c}&nooverride=1&opportunityId={!opp.Id}&RecordType=012b0000000M0Yx&retURL={!opp.Id}&saveURL={!opp.Id}');        


       else {

       return newPage;       

I do not understand why this code isn't working and the redirection to the required page is not happening on click of list button. Any help?

  • what are you trying to do here? If Opportunity is a specific recordType, redirect to a special page but if not, display an empty page with no markup?
    – cropredy
    Mar 10, 2016 at 3:06

1 Answer 1


You are returning null in your else block. Try adding some pageMessages...

<apex:page attributes="populated">
    <apex:pageMessages />

Then in your else block, something along the lines of:

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Cannot redirect'));

I would recommend putting the message in a Custom Label if you make this a permanent component of your solution.

Also, you can't use merge fields in Apex. Change your if condition to something more like:

Set<String> recordTypeDeveloperNames = new Set<String>
    'Single_Account_Opportunity', 'Multi_Account_Opportunity'
if (recordTypeDeveloperNames.contains(opportunity.RecordType.DeveloperName))
    // build reference

Make sure you add this field to your query.

  • Is this the only reason that the code isn't working?
    – PranavJain
    Mar 9, 2016 at 18:07
  • actually my code is whatever I've posted here.
    – PranavJain
    Mar 9, 2016 at 18:23
  • Based on the condition in if statement I've to navigate the user to the mentioned page reference else redirect to some other url.
    – PranavJain
    Mar 9, 2016 at 18:25

You must log in to answer this question.

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