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 HarrisonSteve Harrison 

onclick javascript to update Status

I am in need of onclick javascript for a custom button on the order detail record that changes the Status picklist option to "Cancelled" 

Thanks in advance

Steve
Best Answer chosen by Steve Harrison
sfScottsfScott
There are a few steps to do this:  First you need to create a button (Setup | Customize | Orders | Buttons and Links | New)
I named it "Cancel Order" and set it as a "Detail Page Button" (so it's available for use ona  detail record).  
Behavior is "Execute JavaScript" and Content Source is "OnClick JavaScript".
I've pasted in the Javascript code I put in my formula box:
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
var o = new sforce.SObject("Order");
o.id ="{!Order.Id}";

function checkError(result)
{
  if (!result[0].getBoolean('success')) {
    throw("error");
  }
}
try 
{
  o.status = "Cancelled";
  result = sforce.connection.update([o]);
  checkError(result);
  alert("Order cancelled!");
}
catch (err)
{
  alert('An error occurred processing your request.  Please contact your administrator and reference order ID '+o.id);
}

Now edit the desired page layout and choose "Buttons" and place your custom button like so:
Editing the Page Layout

Now when you edit an Order record you should see the button and, if all goes well, get an alert that says "Order cancelled!"

Hope this helps! 
 

All Answers

sfScottsfScott
There are a few steps to do this:  First you need to create a button (Setup | Customize | Orders | Buttons and Links | New)
I named it "Cancel Order" and set it as a "Detail Page Button" (so it's available for use ona  detail record).  
Behavior is "Execute JavaScript" and Content Source is "OnClick JavaScript".
I've pasted in the Javascript code I put in my formula box:
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
var o = new sforce.SObject("Order");
o.id ="{!Order.Id}";

function checkError(result)
{
  if (!result[0].getBoolean('success')) {
    throw("error");
  }
}
try 
{
  o.status = "Cancelled";
  result = sforce.connection.update([o]);
  checkError(result);
  alert("Order cancelled!");
}
catch (err)
{
  alert('An error occurred processing your request.  Please contact your administrator and reference order ID '+o.id);
}

Now edit the desired page layout and choose "Buttons" and place your custom button like so:
Editing the Page Layout

Now when you edit an Order record you should see the button and, if all goes well, get an alert that says "Order cancelled!"

Hope this helps! 
 
This was selected as the best answer
Steve HarrisonSteve Harrison
Excellent! It worked as needed. Thanks. Steve