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
Laura StanleyHubbardLaura StanleyHubbard 

Javascript Triggering error for Button Added on Contact Detail Page

I've added a custom button to the Contact Detail view, and I'm startig to test it's functionality. I want it to open a case and have the status default to closed.I get the error, when click on contact page, "An error has occurred. Error: Id not specific in an update call." My code is below:


{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

try{
var caseToUpdate = new sforce.SObject("Case");

caseToUpdate.Id = "{!Case.Id}";
caseToUpdate.Status = "Closed";


var result =
sforce.connection.update([caseToUpdate]);

if(result[0].success == "true"){
location.reload();
}
else{
alert("An Error has Occurred. Error: " +
result[0].errors.message
);
}
}
catch(e){
alert(
"An Error has Occurred. Error: " +
e
);
}
David ZhuDavid Zhu
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

try{
var caseToUpdate = new sforce.SObject("Case");

caseToUpdate.Status = "Closed";

var result = sforce.connection.create([caseToUpdate]);

if(result[0].success == "true"){
location.reload();
}
else{
alert("An Error has Occurred. Error: " +
result[0].errors.message
);
}
}
catch(e){
alert(
"An Error has Occurred. Error: " +
e
);
}
Laura StanleyHubbardLaura StanleyHubbard
This runs without error but there's no new case on the contact record.
David ZhuDavid Zhu
Since it is a Closed case, you won't see it in open case list view. try select my case or create closed case list view.
Practically, you need to provide a subject for this case or other information. ie.

caseToUpdate.Subject = "Lauran's case";
Laura StanleyHubbardLaura StanleyHubbard
David:
I now have new updated code that reflects new business requirements. However, when I run, I get the following error: Expected Identifier. Some additional information might help: All fields except subject and description are picklist fields. 3 are custom/dependent fields. Also, I need this to ensure that the case is opened and revealed in another tab and not just created in the system to go find. There are addiional steps that have to happen manually or with a macro post this button click. Suggestions on error and viewing the case on click?

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
try{
var caseToUpdate = new sforce.SObject("Case");
caseToUpdate.Status = "Open";
caseToUpdate.Subject = "Benefit Confirmation Statement";
caseToUpdate.Description = "Benefit Elections edited. Confirmation Statement created and sent to WSE via email";
caseToUpdate.{!Case.Service__c} = "Health and Welfare";
caseToUpdate.{!Case.Action__c} = "Enrollment";
caseToUpdate.{!Case.Case_Contact_Type__c} = "WSE";
caseToUpdate.Origin = "Internal";
caseToUpdate.Priority = "Low";
caseToUpdate.Type_c = "Eligibility";
var result = sforce.connection.create([caseToUpdate]);
if(result[0].success == "true"){
location.reload();
}
else{
alert("An Error has Occurred. Error: " +
result[0].errors.message
);
}
}
catch(e){
alert(
"An Error has Occurred. Error: " +
e
);
}
David ZhuDavid Zhu
Custom field should be referenced the same way as standard field.
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
try{
var caseToUpdate = new sforce.SObject("Case");
caseToUpdate.Status = "Open";
caseToUpdate.Subject = "Benefit Confirmation Statement";
caseToUpdate.Description = "Benefit Elections edited. Confirmation Statement created and sent to WSE via email";
caseToUpdate.Service__c = "Health and Welfare";
caseToUpdate.Action__c = "Enrollment";
caseToUpdate.Case_Contact_Type__c = "WSE";
caseToUpdate.Origin = "Internal";
caseToUpdate.Priority = "Low";
caseToUpdate.Type_c = "Eligibility";
var result = sforce.connection.create([caseToUpdate]);
if(result[0].success == "true"){
location.reload();
}
else{
alert("An Error has Occurred. Error: " +
result[0].errors.message
);
}
}
catch(e){
alert(
"An Error has Occurred. Error: " +
e
);
}

 
Laura StanleyHubbardLaura StanleyHubbard
David: I had an error on one of my fields. I removed that and it works for now. I still need to meet the following paramters:

1. Pull over the contact name, contact email (when available), phone, mobile, and fax, and the account name tied to that contact when this case is created.
2. I also need the new case to display. Is javascript the way to go here?
David ZhuDavid Zhu
You can create a custom button on Contact object using Javascript and put it on the layout.
In the code, you can pass the contact information to new case. the example below is passing contact name to case subject field.

caseToUpdate.Subject = "{!Contact.Name}";

To redirect to the new case page, use result[0].id to get the new case id.

if(result[0].success == "true")
{
     //location.reload();
     location.href="/" + result[0].id;
}