I would like to create a custom button on service contract to create an opportunity. I want to populate a lookup field between service contract and opportunity (So I will be able to use it for some formula fields later on) The steps are:

  1. Sales rep clicks the button
  2. Opportunity is created and auto-saved (no edit screen is displayed)
  3. Lookup field on service contract is populated with opporunity id

I'm not sure if it's possible just with javascript within the custom button, or adding some triggers. Any help will be great.

2 Answers 2


This is possible using the AJAX Toolkit, which you can invoke on button click.

Here is a create example

var account = new sforce.SObject("Account");
account.Name = "my new account";
var result = sforce.connection.create([account]);

After creating the Opportunity, you can update the Lookup on the Service Contract with the OpportunityId.

Equally, you can do it in a trigger (perhaps after insert), where it happens more seamlessly and unless there is reason to give the user some kind of immediate visual feedback via the button click, I would prefer the trigger over embedding Javascript on the button click.

P.S. There is also the option of doing your processing in an Apex class webservice method and invoking that on button click.

Use the synchronous update example from the AJAX Toolkit reference

 var account = new sforce.SObject("Account");
  account.Name = "myName";
  account.Phone = "2837484894";
  result = sforce.connection.create([account]);

  //update that account
  account.id = result[0].id;
  account.Phone = "12398238";
  result = sforce.connection.update([account]);
  • I prefer creating a custom button with javascript. the part that I'm missing is how to update the lookup field in the service contract after the opportunity is created. Can you elaborate on that?
    – Itay B
    Feb 14, 2013 at 13:02
  • @ItayB You need to reload the whole page with window.location.reload(); Feb 14, 2013 at 14:25
  • @ItayB I've added an update example, you just instantiate a new instance, set the Id and update the field you need to update. Feb 14, 2013 at 14:59

I think you can do it with javascript and webservice. The webservice will create a new opp and returns a string-callback. If it is OK - then reload the page to see a new created opp id, if not - alert an error message:


global without sharing class CreateOpportunity
    WebService static String createOpp(String contractId)
            // Now do you stuff eg. set all fiels etc.
            Oppotrunity opp = new Oppotrunity();
            insert opp;

            // Now updating your service contract object
            Contract__c con = [select id, lookupToOpportunity__c From Contract__c where id = :contractId];
            con.lookupToOpportunity__c = opp.id;
            update con;

            return 'OK';
        catch(Exception e){
            return e.getMessage();

Custom button:


// Now calling the webservice to create a new opportunity
var callback = "" + sforce.apex.execute("CreateOpportunity","createOpp", {contractId:"{!Contract__c.Id}"});

// If create and update was ok - just reload contract page to see the id of the new opportunity
if(callback == 'OK'){
    alert('Error: ' + callback);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .