Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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]);
share|improve this answer
    
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 '13 at 13:02
    
@ItayB You need to reload the whole page with window.location.reload(); –  mast0r Feb 14 '13 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. –  techtrekker Feb 14 '13 at 14:59
add comment

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:

Webservice:

global without sharing class CreateOpportunity
{
    WebService static String createOpp(String contractId)
    {
        try{
            // 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:

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

// 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'){
    window.location.reload();
}
else{
    alert('Error: ' + callback);
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.