Skip to content

Getting Started with Custom Buttons in Salesforce


Salesforce Admin Tips
Since a few weeks a recurrent question is asked me: How to create Custom Buttons in Salesforce?

As an administrator, providing solutions for the rest of the company to use should revolve around making them as user-friendly as possible. Why? Because this greatly improves productivity and mitigates human error. Thankfully, Salesforce’s flexibility means you are able to customize just about anything. #Winning

Custom Buttons in Salesforce

In this blog post, I will show you how you can create a button on the Salesforce object, ‘Opportunity’, for closing an Opportunity with the deal amount fed in. Bear with me, it’s not as complicated as it may initially sound (I promise.)

To follow along with me, all you need is a Salesforce developer account. If you don’t already have one, just sign up for your free account.

First thing we are going to do is create a button in Salesforce for Opportunity. In order to do that, navigate to Setup > Opportunities > Buttons, Links, and Actions. From there, click on “New Button or Link”. You should see a page identical to the screen shot below:

Custom Buttons in Salesforce

Give your button a label. This is the label users are going to see, therefore it should be a user friendly name (and nothing you wouldn’t want them to see!). The button name should automatically be generated for you but you can change this if you wish however, this is also the API name so be cautious that it cannot contain any spaces.

You should always give your button a description because it makes life that little bit easier for any future Salesforce Admins or Developers that may pick up your work where you have left off. We are going to pick the option “Detail Page Button” for Display Type. Pick the option “Execute Javascript” for Behavior. Leave “OnClick Javascript” for Content Source.

Now here’s the fun part – writing the code to execute once the user has clicked your button. Copy and paste the code below into the big text box:

{!REQUIRESCRIPT(“/soap/ajax/29.0/connection.js”)}   var opp = new sforce.SObject(‘Opportunity’); = “{!Opportunity.Id}”; var amount = prompt(“Deal Value”, “”); if(amount !== null){                   opp.Amount = amount; } opp.StageName = “Closed Won”; result = sforce.connection.update([opp]); location.reload(true);   

Hit Save.

Don’t get prematurely excited – we’re not done yet. We still need to add the button to the page layout that we choose.

Pick the page layout that should contain this button and drag the button on the page like displayed in the screen shot below:

Add Custom Button in Salesforce

You should find your button by the friendly name you have given it in the buttons section and you simply need to drag it to the section labeled “Custom Buttons”.

Once you have saved your layout, you should find your button next to Salesforce’s native buttons like Edit and Delete as displayed in the screen shot below. Mine is labeled (rather imaginatively) “Close Opportunity”.

blog3 (2)

Go ahead and test it out. On clicking your new button, you should find that you are prompted to enter a deal value like displayed below.

Enter Deal Value Custom Buttons in Salesforce

Once you have entered a deal value and clicked “OK” and you should find the page is refereshed and the opportunity is now closed Won with the correct deal value in the field “Amount”. 

The great thing about our button is that I have used Javascript to perform this action which makes our button incredibly flexible. You can alter the code and perform what logic your workflow requires. Now we’ve got our hip ‘n’ happening button going on, you are on your way to creating even greater Custom Buttons in Salesforce. Enjoy!


Soroosh Avazkhani

Junior Developer at Ebsta. Obsessed with finding and sharing all the best Salesforce Admin hacks to help you achieve Salesforce greatness!

Give your Sales Reps access to the best data

salesforce-email-data-quality Long gone are the days where you had to rely on your Reps to add emails or data into Salesforce. Ebsta does it automatically. Reps now have access to 100% data accuracy without having to do a thing.