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
como_estascomo_estas 

apex:facet button for a new record / sobject?

Here's one that I think should be easy?

 

I have an <apex:pageBlock> and I am using an <apex:facet name="header"> to make my own custom stuff in the header.

 

I just want a button in there to create a new object.  Is there an easy way to do this?  Or do I seriously need to get the object prefix for this?

 

That would be weak.

sfdcfoxsfdcfox

Don't use header unless you want it only at the top. Normally apex:pageBlockButtons are used for things like apex:commandButton elements. That besides, you can return a page reference:

 

public apexpages.pagereference createnewrecord() {
  return new apexpages.standardcontroller(new account()).edit();
}

Replace account() with your desired object. The command button would be:

 

<apex:commandButton value="New XYZ" action="{!createnewrecord}" />

 

como_estascomo_estas

I did want it only at the top, but then when I put it in both places, I liked it better.

 

That said, what you said makes perfect sense.  For some reason, though, the page is only refreshing and not redirecting to the page reference being returned by the method.  Here's the code:

 

	public PageReference newAttendee() {
		return new ApexPages.Standardcontroller(new ContactScheduleItem__c()).edit();
	}
	

			<apex:pageBlockButtons >
				<apex:commandButton value="New Attendee" action="{!newAttendee}"/>
			</apex:pageBlockButtons>

 I even tried turning it into a variable and calling the setRedirect(true); method on it.  Any clues?

sfdcfoxsfdcfox

Do you have a page override on the ContactScheduleItem__c.new button? If so, you need to add the "nooverride" parameter, thusly:

 

apexpages.pagereference ref = new apexpages.standardcontroller(new contactscheduleitem__c()).edit();
ref.getparameters().put('nooverride','1');
return ref;

 

como_estascomo_estas

Hey I really appreciate your help.  It seems that no matter what object I try here, It just opens up an edit page for the type of object on which this button is found.

 

The "New Attendee" button appears on a custom VF page that is an edit or a view page for a custom object OrgEvent__c

 

When I try to return a page reference for a standard controller edit page for any other object, it simply opens an edit page for the object i happen to have loaded in the current scope.

 

It must be an issue with standard controllers?  Perhaps I can't create another one while there is one in scope already?  Sounds stupid, but even if I try 

return new apexpages.standardcontroller(new contact()).edit();

 it does the same thing.  It opens an edit page for the OrgEvent__c object that i'm viewing.