0

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;
    }
}
9
  • 3
    You're using the wrong ID value: that error is telling you that the key prefix for the value you provided does not match the key prefix for the Property__c object. Usually, this means you're trying to assign the wrong value somewhere. Unfortunately, what exactly is wrong is hard to tell, because you didn't include any useful code here. Can you provide a Short, Self Contained, Correct (Compilable), Example so we can triage your code?
    – sfdcfox
    Mar 24, 2016 at 17:36
  • I assume it doesn't work anymore because the URL is now in the format https://xxxxx.visual.force.com/apex/PropertyTestTabs?id=xxxxxxxx Instead of https://xxxxx.salesforce.com/xxxxxxx Not sure how to rewrite this part to get the record id
    – MRG
    Mar 24, 2016 at 17:47
  • To clarify, I know that the bottom of the class, which builds retpage will have to be reprogrammed, but before I do this, I need to get the recordid in the first place
    – MRG
    Mar 24, 2016 at 17:50
  • why are you using variables named l for custom object property__c? Did this code evolve from something about leads and the lead ID is being passed in the URL?
    – cropredy
    Mar 24, 2016 at 17:55
  • yes, it did. We have one of these for a number of custom objects and it works basically the same in each of them. We just left the letters the same for subsequent ones.
    – MRG
    Mar 24, 2016 at 17:57

2 Answers 2

4

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").
12
  • Although I'm using the standard controller, I am using an extension. I will add this above.
    – MRG
    Mar 24, 2016 at 19:07
  • Actually, it looks like the problem have nothing to do with my apex code. It has to do with a button on the Property__c object. This button is supposed to send the user to another page which runs the apex class. It's a detail page button, which is supposed to redirect to this URL: /apex/CreateProposalView?id={!Property__c.Id} Added a screenshot here: tiikoni.com/tis/view/?id=7dee2ed
    – MRG
    Mar 24, 2016 at 20:35
  • That sounds more reasonable. The URL appears to be correct at first glance, but I suspect something is going wrong. What do one of the URLs look like after you click on the button?
    – sfdcfox
    Mar 24, 2016 at 20:40
  • @MRG It's missing the ID value. That means there's something wrong with your button...
    – sfdcfox
    Mar 24, 2016 at 21:06
0

I encountered the same problem with a custom Detail Page Button on the object that was passing an ID in the query string. For some reason, there is an invisible character being inserted at the end of the ID. I wrote some code to strip out the extra character:

// Remove extraneous character which being inserted for some reason.
Pattern nonAlphanumeric = Pattern.compile('[^a-zA-Z0-9]');
Id parentId = nonAlphanumeric.matcher(ApexPages.currentPage().getParameters().get('Id')).replaceAll('');

You must log in to answer this question.

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