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 Java 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.


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}";


   result = sforce.connection.create(newRecords);
   alert ('Case has been created.');
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.



Janet GuoJanet Guo

When you say opp types, do you mean opp record types? If you don't have Apex available, you will need to hard-code the record type IDs into your If-statements. A caveat is that things can break depending on how your production environment is set-up (that is why Apex is the preferred way to do this, you can get record types dynamically). Were the record types all created in production before the sandbox was refreshed? If they were, then you should be good because the record type IDs will remain the same. However, if the record types were created in the sandbox but not in production first, you may find things breaking when you deploy to production. Just remember to change the IDs when you deploy to production.