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 

Open a Case (Using Button) from Contact Record with Prepopulated fields

I have the following javascript code below for a contact detail page button. It creates the case as planned, but I need it to do additional items.
1. Pull the contact details from the contact record where the button is pressed
a. Contact Details are Contact Name, Email, Phone, Fax, Mobile
2. Pull Account Detail fields on the Contact Record
b. Account Name and AOID
3. Open the case in a new tab so I can continue editing it for more manual work.

Any help is appreciated. Providing code is ideal, as this is not my strength, and we need this solved in the next week! Thank you!
 
{!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
 
SKolakanSKolakan
Laura,

you are almost there. Here is the modified code
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

try{
	var connection = sforce.connection;
	var caseToUpdate = new sforce.SObject("Case");
	caseToUpdate.Status = "New";
	caseToUpdate.Subject = "Benefit Confirmation Statement";
	caseToUpdate.Description = "Benefit Elections edited.";
	//caseToUpdate.Service__c = "Health and Welfare";
	//caseToUpdate.Action__c = "Enrollment";
	//caseToUpdate.Case_Contact_Type__c = "WSE";
	caseToUpdate.Origin = "Web";
	caseToUpdate.Priority = "Low";
	//caseToUpdate.Type_c = "Eligibility";
	caseToUpdate.ContactId = '{!Contact.Id}'; //You can access all contact fields on the page like this

	var result = sforce.connection.create([caseToUpdate]);
	
	if(result[0].success == "true"){
		var newCaseURL = '/'+result[0].id + '/e';  //Form URL
		window.open(newCaseURL ,target = "_blank"); //Open the created case in new tab
	}
	else{
		alert("An Error has Occurred. Error: " +result[0].errors.message);
	}
}
catch(e){
	alert("An error has Occurred. Error: " + e );
}

Let me know how it works.
Laura StanleyHubbardLaura StanleyHubbard
That's so close...and thank you! However, it opens a case outside of console, which is what our team works in. It also didn't prepopulate my custom picklist fields. Any thoughts as to why?
SKolakanSKolakan

it opens a case outside of console, which is what our team works in --- I did not understand what it means.
It also didn't prepopulate my custom picklist fields -- Check if the above code has those fields.I commented some of the fields to keep it simple. You can uncomment them as you need. 

P.S. Please mark one as best answer if it solved your problem so that others can benefit from it.
Laura StanleyHubbardLaura StanleyHubbard
The Custom Buttons are listed and denoted with a '_c' at the end of the field name.

The case open here: See the tabs across the top. The case opened in this view as a single tab. Then, the user would need to click 'back to console' to return to the workspace.
User-added image

We need the case to open as a primary tab here:
User-added image