function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
parkerAPTparkerAPT 

Reference native PageReferences / Actions like (Contact.New) in Apex

Is there a way to get a handle on the PageReference that corresponds to the New / Edit / Delete actions for SObjects.

 

In Visualforce, you can get the URL by using the URLFOR function:

 

 

<apex:commandButton value="New"action="{!URLFOR($Action.Gathering__c.New, null, [cid=theContact.id])}"/>

 This will properly take me to the New Gathering__c page.

 

Can I get at this in Apex?  Something like:

 

PageReference pageRef = ApexPages.Action.Gathering__c.New;

pageRef.getParameters().put(cid, theContact.id);ApexPages.Action ac = ApexPages.Action.Gathering__c.New;

 Neither of these work like the Page.Custom_Apex_Page

 

Is the only solution to override the New / Edit Views and make a custom Apex Page / Controller?

 

Thanks 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

parkerAPT wrote:

Is there a way to get a handle on the PageReference that corresponds to the New / Edit / Delete actions for SObjects.

 

In Visualforce, you can get the URL by using the URLFOR function:

 

 

<apex:commandButton value="New"action="{!URLFOR($Action.Gathering__c.New, null, [cid=theContact.id])}"/>

 This will properly take me to the New Gathering__c page.

 

Can I get at this in Apex? 

 

 

 


 

 

 

Yes. Use the standardcontroller like such:

 

Contact c = new Contact();
ApexPages.StandardController con = new ApexPages.StandardController(c);
PageReference newPage = con.edit();

 

 

Message Edited by mtbclimber on 05-26-2009 09:17 AM

All Answers

mtbclimbermtbclimber

parkerAPT wrote:

Is there a way to get a handle on the PageReference that corresponds to the New / Edit / Delete actions for SObjects.

 

In Visualforce, you can get the URL by using the URLFOR function:

 

 

<apex:commandButton value="New"action="{!URLFOR($Action.Gathering__c.New, null, [cid=theContact.id])}"/>

 This will properly take me to the New Gathering__c page.

 

Can I get at this in Apex? 

 

 

 


 

 

 

Yes. Use the standardcontroller like such:

 

Contact c = new Contact();
ApexPages.StandardController con = new ApexPages.StandardController(c);
PageReference newPage = con.edit();

 

 

Message Edited by mtbclimber on 05-26-2009 09:17 AM
This was selected as the best answer
XactiumBenXactiumBen

What happens if you are working on, for example, an Accounts custom visualforce page then wanted to redirect to the Contact New page?

 

The only way I can see of doing this would be to use describe calls to get the key prefix (or query a Contact and take the first 3 characters from the id) then add '/e' at the end:

 

Schema.DescribeSObjectResult r = Contact.sObjectType.getDescribe(); PageReference ref = new PageReference('/' + r.getKeyPrefix() + '/e');

 

Would this be the only way to do it?

mtbclimbermtbclimber

Apologies. The above only works when the contact has an Id. We're investigating this as a feature enhancement. The view and edit actions will work if the object has an Id.

 

 

pierpipierpi
Hi Andrew,
Is there already an idea opened for this?  (I tried searching for one but I did not find it).  It would be nice to have this feature so that it would be possible to pre populate field values with run-time obtained values when creating a new record (without using the URL or static defaults).

Thanks
Pierpaolo 
ShikibuShikibu

By way of completeness, if you are overriding, for instance, the edit button, but want to conditionally display the standard page, you need to set the nooverride parameter.

 

 

public PageReference routePageAction() {
if (condition-for-displaying-vf-page) {
return null;
}

PageReference standardEditPage = new ApexPages.StandardController(theCase).edit();
standardEditPage.getParameters().put('nooverride', '1');
String retUrl = ApexPages.currentPage().getParameters().get('retURL');
standardEditPage.getParameters().put('retURL', retUrl);

standardEditPage.setRedirect(True);
return standardEditPage;
}

 

 

 

 

Message Edited by Shikibu on 10-22-2009 10:22 PM
Message Edited by Shikibu on 10-22-2009 10:23 PM