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 

Help with a tweak to my custom button code?

Hi all,

 

We use the following code behind a custom button that's displayed on our opp detail page to automatically create a case.  We're using Professional Edition, so we don't have Apex available to us for this to happen automatically when the opp stage is changed to closed won.

 

You can see below that we're using an IF statement to only create the case if the opp stage is closed won, and to alert the user if otherwise.

 

{!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.Type = "{!Opportunity.Type}";
   c.Subject = "{!Opportunity.Name}";
   c.Origin = "Converted Opportunity";
   c.Description = "{!Opportunity.Requirement__c}";
   c.ContactId = "{!Opportunity.Opportunity_ContactId__c}";

   newRecords.push(c);

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

 

I need to complicate this further with another IF statement, but not sure how to do so ... here's what we need:

 

We still need the case only to be created if the opp stage = closed won, and to alert the user if otherwise.

 

We have 6 opp types (we'll call them types 1 to 6), and in addition to the above IF statement, we also need the button to create the case if opp type = 1, 2, 3 or 4, but to alert 'A case cannot be created for opportunity types 5 or 6.' (and not create the case) if the opp type = 5 or 6.

 

Can anyone help me with a tweak to my button code to achieve this?

 

Thanks in advance.

 

Steve

Best Answer chosen by Admin (Salesforce Developers) 
_Prasu__Prasu_

Try

 

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

if("{!Opportunity.Type}" != "5" && "{!Opportunity.Type}" != "6")

{

 if (status == "Closed Won")
 {
   var c = new sforce.SObject("Case");
   c.AccountId = "{!Opportunity.AccountId}";
   c.Type = "{!Opportunity.Type}";
   c.Subject = "{!Opportunity.Name}";
   c.Origin = "Converted Opportunity";
   c.Description = "{!Opportunity.Requirement__c}";
   c.ContactId = "{!Opportunity.Opportunity_ContactId__c}";

   newRecords.push(c);

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

}

else

     alert ('A case cannot be created for opportunity types 5 or 6.');

All Answers

_Prasu__Prasu_

Try

 

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

if("{!Opportunity.Type}" != "5" && "{!Opportunity.Type}" != "6")

{

 if (status == "Closed Won")
 {
   var c = new sforce.SObject("Case");
   c.AccountId = "{!Opportunity.AccountId}";
   c.Type = "{!Opportunity.Type}";
   c.Subject = "{!Opportunity.Name}";
   c.Origin = "Converted Opportunity";
   c.Description = "{!Opportunity.Requirement__c}";
   c.ContactId = "{!Opportunity.Opportunity_ContactId__c}";

   newRecords.push(c);

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

}

else

     alert ('A case cannot be created for opportunity types 5 or 6.');

This was selected as the best answer
Steve HuseSteve Huse

Thanks Prasanna - Perfect!

 

Many thanks for your help.

 

BW


Steve