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
Angel118Angel118 

Close Case

Hi,
       I need to directly Close a Case directly via a Custom-Button, without taking the user to the Close Case Page Layout.

Is it Possible ? if yes, Please guide me for the same..

Thanks in Advance,
Angel..
sfdcfoxsfdcfox
Are you in Enterprise Edition?

Code:
{!RequireScript("/soap/ajax/10.0/connection.js")}
var CaseToClose = new sforce.SObject("Case")
CaseToClose["Id"] = "{!Case.Id}"
CaseToClose["Status"] = "Closed - Pending Customer Approval"
closeResults = sforce.connection.update([CaseToClose])
if(closeResults[0].getBoolean("success"))
  window.top.location.href = window.top.location.href // Refresh Page
else
  alert("Unable to close this case at this time:\n\n"+closeResults[0].getArray("errors")[0]["Message"]) // This syntax only shows the first error message!
If you're in Professional Edition, try this instead:

Code:
window.top.location.href="/{!Case.Id}/s?retURL={!Case.Id}&cas7=Closed+-+Pending+Customer+Approval&save=1
Note that this is less code than Enterprise Edition only because there's no error checking and is completely hardcoded, and even may fail in the future.

Hope this helps!

~ sfdcfox ~

Angel118Angel118
Thanks sfdcfox,
       That worked fine for me...

Angel..
David DanzigDavid Danzig
Does anyone have code for a case "Close" button for a custom object similar to cases?

Thanks,
David.
sfdcfoxsfdcfox
Sure do, just change a few lines from above, and you've got it. Try this:

Code:
{!RequireScript("/soap/ajax/10.0/connection.js")}
CustomObject__c = new sforce.SObject("CustomObject__c")
CustomObject__c["Id"] = "{!CustomObject__c.Id}"
CustomObject__c["Status__c"] = "Closed"
// You could put more lines like the one above to update multiple fields.
// E.g. CustomObject__c["CustomCheckbox__c"] = true
try {
results = sforce.connection.update([CustomObject__c])
if(!results[0].getBoolean("success"))
{ // This is a permissions issue.
alert("Could not close this record.\n\nReason: "+results[0].getArray("errors")[0].message+"\n\nIf you continue to receive this message, please contact your administrator.") }
} catch(e)
{ // This is an API fault, not necessarily permissions-based.
alert("Could not close this record.\n\nReason: "+(e.faultCode?e.faultCode:e.message?e.message:e)+"\n\nIf you continue to receive this message, please contact your administrator.")
}
window.top.location.href=window.top.location.href
As you can see, this is fairly quick and painless no matter what field you're trying to update on any object. Just substitute the object's name and the field names in red (above), and you can make any update.

~ sfdcfox ~

Edit: Updated the error checking for robustness.
 

Message Edited by sfdcfox on 10-18-2007 06:09 PM

Message Edited by sfdcfox on 10-18-2007 06:10 PM

David DanzigDavid Danzig
I tried to use this and include a line to add a time stamp.

None of the following lines worked:

CustomObject__c["Date_Time_Closed__c"] = sforce.function(NOW(0))

CustomObject__c["Date_Time_Closed__c"] = NOW()

CustomObject__c["Date_Time_Closed__c"] = "NOW()"

What would work?

Thanks,
David.
sfdcfoxsfdcfox
Code:
CustomObject__c["Date_Time_Closed__c"] = new Date("{!NOW()}")
That would be the officially correct way to capture the date. Anything else and you're asking for trouble.

~ sfdcfox ~