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
Steve HuseSteve Huse 

Custom button to create case from won opportunity

Hi all,

 

I've struggled with this for a while and welcome any advice.

 

I want to place a custom button in opportunities (detail page) that, providing the opp stage is 'Closed Won' creates a case, copying a number of opp fields across into the case fields as it does so.

 

I currently have two custom buttons on the page that I'm testing ... both using Onclick Javascript.

 

HERE'S THE CODE BEHIND THE FIRST ONE:

if ("{!Opportunity.StageName}" == 'Closed Won')

{location.href = "/500/e?retURL=%2F500%2Fo

&cas4={!Account.Name}

&cas3={!Opportunity.Opportunity_Contact__c}

&cas5={!Opportunity.Type}

&cas11={!'Converted Opportunity'}

&cas14={!Opportunity.Name}

&cas15={!Opportunity.Requirement__c}

&retURL=/{!Opportunity.Id}&saveURL=/{!Opportunity.Id}";}

else

{alert('Opportunity must be won before a case can be created.'); }

 

What works:

  • The 'new case' page is launched and the fields are populated.

What doesn't work:

  • It doesn't always copy the full content of each field across ... i.e. an opportunity account name of 'ABC Construction Ltd' only populates as 'ABC' in the account name field on the case screen.
  • It requires the user to click the Save button after the fields have been populated.

 

HERE'S THE CODE BEHIND THE SECOND ONE:

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
var status = "{!Opportunity.StageName}";
var newRecords = [];

if (status == "Closed Won")
{
var c = new sforce.SObject("Case");
c.AccountId = "{!Opportunity.AccountId}";
c.Contact = "{!Opportunity.Opportunity_Contact__c}";   <-------- Problem arises when other fields are included.
//add any other related fields you like

newRecords.push(c);

result = sforce.connection.create(newRecords);
}
else
{
alert ('Opportunity must be won before a case can be created.');
}

 

What works:

  • Providing the line in red is left out, the button works fine.

What doesn't work:

  • As soon as I try to include any additional fields to carry across from the opp to the case, the button no longer works ... no case is created at all.

 

Can anyone tell me which code is best, or provide some guidance as to how to tweak either of the existing codes so that they work in the way I'd like them to?

 

Sorry for the long post!

 

Regards


Steve