[SalesForce] Using ApexPages.currentPage().getParameters().get(‘id’) with a custom visualforce page

I've been having some problems with replacing a Standard controller view with a Visualforce page following this: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_tabs.htm

The problem is, it's breaking a lot of my apex classes. For example, anything that uses:

ApexPages.currentPage().getParameters().get('id') 

Which will throw this error:

Id value is not valid for the Property__c standard controller 

Is there another way to get record ids using visualforce pages? We use these frequently

Here is one example of a code that is breaking:

public class ControllerCreateProposalView {
    public Id lId;
    public String convertedId;

    public ControllerCreateProposalView(ApexPages.StandardController stdController){
        lId = ApexPages.CurrentPage().getParameters().get('id');
        System.Debug('#######leadId:' + lId);
    }


    public PageReference convert(){


        Property__c l = [SELECT Id, name, Primary_Contact__c, Primary_Contact__r.id,Store_Number__c, Last_Sale_Date__c, Square_Footage__c, Last_Sale_Price__c, Anchor_GLA__c, CAP_Rate__c, Year_Built__c, Lot__c, Year_Renovated__c, Occupancy__c, Zoning__c, External_ID_APN__c, Number_of_Buildings__c, Number_of_Retail_Units__c, Loan_Balance__c, Maturity_Date__c, Interest_Rate__c, Term__c, Original_Lease_Term__c, Options__c, Term_Remaining_on_Lease__c, Gross_Leasable_Area__c, Type_of_Ownership__c, Parking__c, Parking_Ratio__c, Lease_Type__c, Date_Reported__c, Loan_Amount__c, Loan_Type_bcc__c, LTV__c, Lender__c, Lender_Type__c, Amortization__c, Recourse__c, Current_Interest_Rate__c, Payment__c, Prepayment__c, Proposal_date__c FROM Property__c WHERE Id=:lId LIMIT 1];
        Proposal__c c=new Proposal__c(Name=l.Name, Property__c=l.Id, Client__c=l.Primary_Contact__c, Square_Footage__c=l.Square_Footage__c, CAP_Rate__c=l.CAP_Rate__c, Lot__c=l.Lot__c, Loan_Balance__c=l.Loan_Balance__c, Maturity_Date__c=l.Maturity_Date__c, Term__c=l.Term__c, Original_Lease_Term__c=l.Original_Lease_Term__c, Options__c=l.Options__c, Term_Remaining_on_Lease__c=l.Term_Remaining_on_Lease__c, Anchor_GLA__c=l.Anchor_GLA__c, Occupancy__c=l.Occupancy__c, Number_of_Buildings__c=l.Number_of_Buildings__c, Number_of_Retail_Units__c=l.Number_of_Retail_Units__c, Gross_Leasable_Area__c=l.Gross_Leasable_Area__c, Type_of_Ownership__c=l.Type_of_Ownership__c, Parking__c=l.Parking__c,Parking_Ratio__c=l.Parking_Ratio__c, Year_Built__c=l.Year_Built__c, Lease_Type__c=l.Lease_Type__c, Date_Reported__c=l.Date_Reported__c, Loan_Amount__c=l.Loan_Amount__c, Loan_Type__c=l.Loan_Type_bcc__c, LTV__c=l.LTV__c, Lender__c=l.Lender__c, Interest_Rate__c=l.Interest_Rate__c, Lender_Type__c=l.Lender_Type__c, Amortization__c=l.Amortization__c, Recourse__c=l.Recourse__c, Current_Interest_Rate__c=l.Current_Interest_Rate__c, Payment__c=l.Payment__c, Prepayment__c=l.Prepayment__c);
        insert c;
       l.Sales_Status__c = 'Proposal';
       l.Proposal_Date__c=Date.today();
       update l;
        convertedId = c.Id;
        String cID=l.Primary_Contact__r.id;
        System.Debug('<>PROPOSAL<> :' + l );
        System.Debug('<>CEYEDEE<><>cID<><>CEYEDEE<> :' + cID );


              //update contact stage
        List<Contact> contacts=[SELECT Id, Sales_Status__c FROM Contact WHERE id=:cID];
System.Debug('<>LIST<><>contacts<><>LIST<> :' + contacts );
        if(contacts.size()>0){
        for(Contact i: contacts)
        {
if(i.Sales_Status__c=='Unconfirmed'||i.Sales_Status__c==NULL){
        i.Sales_Status__c='Proposal';
        update i;
        }

}
}





        String sServerName = ApexPages.currentPage().getHeaders().get('Host');
        sServerName = 'https://'+sServerName+'/';
        System.Debug('#######sServerName :' + sServerName );
        String editName='/e?retURL=%2F'+convertedId;
        PageReference retPage = new PageReference(sServerName + convertedId+editName); 
        System.Debug('#######retPage :' + retPage );
        retPage.setRedirect(true);


        return retPage;
    } 
    public PageReference back(){
            String sServerName = ApexPages.currentPage().getHeaders().get('Host');
        sServerName = 'https://'+sServerName+'/';
        System.Debug('#######sServerName :' + sServerName );
        PageReference retPage = new PageReference(sServerName + lId); 
        System.Debug('#######retPage :' + retPage );
        retPage.setRedirect(true);

        return retPage;
    }      
}

edit
I'm adding this controller extension that the page is using along with the standard controller, in case that makes a difference.

public class PropertyExtensionController{
    Property__c property;

    public PropertyExtensionController(ApexPages.StandardController controller)
    {
        property = (Property__c)controller.getRecord();
    }

    public PageReference save()
    {
        update property;
        return null;
    }
}

Best Answer

The usual way to get the ID is by way of StandardController.getId(). You use it as follows:

public ControllerCreateProposalView(ApexPages.StandardController stdController){
    lId = stdController.getId();
}

Overall, I don't see any reason why your code should be failing. Assuming your page is using the Property__c standard controller, the ID value is correct. If it is not using the Property__c standard controller, then your error is in your page, not the code you've provided.


One thing you should always be doing is trying to cut all the fluff out of your code. Here's how you could have optimized your code:

public class ControllerCreateProposalView {

    Id lId;
    ApexPages.StandardController stdController;

    public ControllerCreateProposalView(ApexPages.StandardController stdController){
        lId = stdController.getId();
        this.stdController = stdController;
    }

    public PageReference convert(){
        Property__c l = [SELECT Name, Primary_Contact__c, Square_Footage__c, Anchor_GLA__c, CAP_Rate__c, Year_Built__c, Lot__c, Occupancy__c, Number_of_Buildings__c, Number_of_Retail_Units__c, Loan_Balance__c, Maturity_Date__c, 
                                Interest_Rate__c, Term__c, Original_Lease_Term__c, Options__c, Term_Remaining_on_Lease__c, Gross_Leasable_Area__c, Type_of_Ownership__c, Parking__c, Parking_Ratio__c, Lease_Type__c, 
                                Date_Reported__c, Loan_Amount__c, Loan_Type_bcc__c, LTV__c, Lender__c, Lender_Type__c, Amortization__c, Recourse__c, Current_Interest_Rate__c, Payment__c, Prepayment__c
                        FROM Property__c
                        WHERE Id = :lId];
        Proposal__c c = new Proposal__c(Name=l.Name, Property__c=l.Id, Client__c=l.Primary_Contact__c, Square_Footage__c=l.Square_Footage__c, CAP_Rate__c=l.CAP_Rate__c, Lot__c=l.Lot__c, Loan_Balance__c=l.Loan_Balance__c,
                                        Maturity_Date__c=l.Maturity_Date__c, Term__c=l.Term__c, Original_Lease_Term__c=l.Original_Lease_Term__c, Options__c=l.Options__c, Term_Remaining_on_Lease__c=l.Term_Remaining_on_Lease__c,
                                        Anchor_GLA__c=l.Anchor_GLA__c, Occupancy__c=l.Occupancy__c, Number_of_Buildings__c=l.Number_of_Buildings__c, Number_of_Retail_Units__c=l.Number_of_Retail_Units__c,
                                        Gross_Leasable_Area__c=l.Gross_Leasable_Area__c, Type_of_Ownership__c=l.Type_of_Ownership__c, Parking__c=l.Parking__c,Parking_Ratio__c=l.Parking_Ratio__c, Year_Built__c=l.Year_Built__c,
                                        Lease_Type__c=l.Lease_Type__c, Date_Reported__c=l.Date_Reported__c, Loan_Amount__c=l.Loan_Amount__c, Loan_Type__c=l.Loan_Type_bcc__c, LTV__c=l.LTV__c, Lender__c=l.Lender__c,
                                        Interest_Rate__c=l.Interest_Rate__c, Lender_Type__c=l.Lender_Type__c, Amortization__c=l.Amortization__c, Recourse__c=l.Recourse__c, Current_Interest_Rate__c=l.Current_Interest_Rate__c,
                                        Payment__c=l.Payment__c, Prepayment__c=l.Prepayment__c);
        insert c;

        update new Property__c(Id=lId, Sales_Status__c = 'Proposal', Proposal_Date__c=Date.today());

        Id cID = l.Primary_Contact__c;

        //update contact stage
        if([SELECT COUNT() FROM Contact WHERE Id = :cID AND Sales_Status__c IN (null, 'unconfirmed')] == 1) {
            update new Contact(Id=cID, Sales_Status__c='Proposal');
        }

        //  Redirect to new view
        return new ApexPages.StandardController(c).view();
    }

    public PageReference back(){
        return stdController.cancel();
    }
}

Some notes:

  • You don't need to query fields you plan on updating.
  • You don't need to query fields you don't plan on using.
  • You don't need to check if a list is empty before iterating over it.
  • You shouldn't need that many debug statements.
  • You don't need to query the Id field by default, unless no other fields are specified.
  • You should try to query specific records to avoid excess Apex Code (see line after "update contact stage").