You need to sign in to do that
Don't have an account?
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
Yes. Use the standardcontroller like such:
Contact c = new Contact();
ApexPages.StandardController con = new ApexPages.StandardController(c);
PageReference newPage = con.edit();
All Answers
Yes. Use the standardcontroller like such:
Contact c = new Contact();
ApexPages.StandardController con = new ApexPages.StandardController(c);
PageReference newPage = con.edit();
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?
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.
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;
}