+ Start a Discussion
JohnC.JohnC. 

Custom Button to create a new record on a separate object with passed data

I am new to Visual Force and I have what I feel like shouldn't be that difficult of a task.  On the Event object I am trying to create a button that when a user clicks it will create a record on a custom object.  Anyone have any suggestions as to how to get this process off the ground? 

ministe2003ministe2003

Hi,

I think you can only do this with some apex coding.  Is the Event object a custom object you've made?

What you want to do is create a visualforce page with your button on, write a class in Apex and set this as the Controller and on the button's action prompt, write something like action={!createRecord} where createRecord() is a method in your class which creates your record and inserts in into the database.

 

Alternatively instead of creating a whole custom page you can just create a custom Button on your object and use some javascript to invoke the method.

SteveBowerSteveBower

Are you just trying to have a record be created as a background task (in which case I think the previous answer is correct), or are you trying to have a record be created and then be brought there to edit the new record? (in which case there is an easier answer.)  Best, Steve

JohnC.JohnC.

Steve,

Your second assumption is correct.  I am trying to take data in fields on a Event record and pass it to a new record on a custom object on button click.  What is the easiest way to accomplish that?  I am sifting through the developer guide for Apex and Visualforce but with little success.

SteveBowerSteveBower

Yes, you would think that something as basic as this would be well and easily documented, but no.

 

Essentially, the easiest way to do this is to create a button with a hacked-together URL which tells Salesforce to

A) Create a new record of a specific type

B) Bring me to the Edit page for that record

C) pre-fill in the following fields.

 

For example:

/00U/e?who_id={!Contact.Id}&retURL=/{!Contact.Id}&RecordType=012400000000zdf&cancelURL=/{!Contact.Id}&ent=Event

 

This is an "Execute Javascript" type button which says:

 

/00U      - 00U is the three letter "Prefix" for the object type that is being created (in this case another Event).  001 is Account, 003 is contact, etc.  If you go to a Tab for that record and look at the URL for that tab you should see something like:  'https://cs2.salesforce.com/003/o'    use the "003" (in this example)

 

/e      - says "Go to Edit this page".   /o = go to the "Overview" for this page, e.g. viewing it.    If nothing is specified, it goes to the overview.

 

Now you start filling in various things:

 

The retURL is the URL that you'll return to when the operation is complete.  In this example (Which is a button being launched from an Event), we're saying, go back to Contact.   "/003xxxxxxx' being the slash followed by the Contact Id.

 

We're doing the same for the CancelURL which, as you might guess, is where we go if the user cancels the operation.

(Note:  With this mechanism, the new record *IS* created, and *then* you are brought to edit it.  If the user cancels the Edit the new record was still created.

 

If the new item you are going to create has various RecordTypes, you can pass the hard-coded record type id as well.  However, if you leave it off, and there is no default, the user will (as normally happens) be redirected to the page where they have to select the record type.

 

 

To see other parameters do this:  (And no, I'm not kidding), in Salesforce, go to an Edit page for the type of record you want to create.  Use whatever browser tools you have to View the HTML source of the page.  Then, find the HTML ID of the field that you want to pre-set a value to.   Then pass those on the URL line.    Things like: 

&evt12=San%20Francisco   This is the Id of the "Location" field on the Edit page for Events. (in my setup at least)

 

Also, under the <form> tag in the generated source there are a variety of Hidden Html parameters which can be used.

 

 

There are other issues here as well, like using nooverride=1 if you want to, for example, go to the Default Edit page and not one that has been overridden with some other VF page.   And there are a variety of issues with pre-filling Lookup fields because the generated HTML has several HTML ID's associated with it like "evt2_lkid'.

 

But, hopefully this will be enough to get you going.   I'm *SURE* there are better writeups of all this stuff out there somewhere, I'm just not sure where.  (Although I'm not sure any if it is in *official* Salesforce documentation, but this is, in reality, how most everyone does this.)

 

Hope this helps, Steve.

 

 

Stacey HusseyStacey Hussey
This is an old post but I created a custom new event button for the first time and couldn't figure out why it wasn't working. Since I was using my code for a new TASK I just had to change the first three letters from OOT to OOU. A big thank you to this post!
Surgaya KhundrakpamSurgaya Khundrakpam

@SteveBower

If the recordType selection is mandatory  is there any way to create an object from Separate object page using custom buttom ?
Khaled YoussefKhaled Youssef
This article describes how to format the value of a SF_URLPop custom call attribute to screen pop a new record in Salesforce, pre-populated with caller data. This example uses a Salesforce New Contact, but you can use this same approach to screen pop and pre-populate literally any Salesforce record.

​https://help.mypurecloud.com/articles/format-salesforce-url-new-record-screen-pop-purecloud-salesforce/