I have a Javascript button that creates an Opportunity from a Case. One of the fields in the new Opportunity is a Contact lookup field that needs to be pre-populated with the Contact field on the Case. All works fine unless the Contact name contains an apostrophe, then the Javascript throws an error that I'm missing a ). Does anyone know how to get the text of the Contact field from the Case? I tried using JSENCODE, but that end us returning the Contact ID.


var query = "SELECT FirstName,LastName FROM Contact WHERE Id = '{!Case.ContactId}' limit 1";

var CName = sforce.connection.query(query);

ConName = CName.getArray("ConName");
var myCon1 = ConName[0].FirstName;

window.open('/006/e?retURL={!Case.Id}&accid={!Account.Id}&RecordType=012a0000001Fsxa&opp11=Quote&00Na000000BODU0=Small Quote&opp3={!Case.Account} - Quote&opp9={!TODAY()+30}&CF00Na000000BAz6W='+myCon1+' '+myCon.LastName+'&ent=Opportunity');
  • 1
    John you will have to query the contact name since this is a related object's field that you are trying to access from the case object. Look for examples on how to query in javascript button and I am sure you will find tons of references eg:success.salesforce.com/answers?id=90630000000htWHAAY
    – Rao
    Oct 12, 2017 at 20:15
  • Thanks. I edited my code and it gives me an error: "Cannot read property of "FirstName" undefined. Any idea what I did wrong? Oct 13, 2017 at 13:49
  • Do console.log('###'+ConName); under ConName= line, save the code and refresh your browser. Right click on your browser preferably chrome and see under console (2nd tab usually) what you see and paste the result in your original question
    – Rao
    Oct 13, 2017 at 14:14
  • if you can post the query result and make sure data is coming back for the query, I can look from there why you are getting undefined for the query. also declare ConName, use Var ConName = CName.getArray("ConName"); it is a good practice to be strict mode compliant
    – Rao
    Oct 13, 2017 at 16:34
  • connection.js:594 Refused to set unsafe header "User-Agent" send @ connection.js:594 sforce.SoapTransport.send @ connection.js:1013 sforce.Connection._invoke @ connection.js:1803 sforce.Connection.invoke @ connection.js:1742 sforce.Connection.query @ connection.js:1396 (anonymous) @ VM4854:6 (anonymous) @ 500a000001Kw7yF:470 loadScripts @ main.js:269 loadScripts @ main.js:269 loadScripts @ main.js:269 element.invokeAction @ 500a000001Kw7yF:466 window.invokeOnClickJS_00ba0000001EZXc @ 500a000001Kw7yF:474 onclick @ 500a000001Kw7yF:407 Oct 13, 2017 at 17:20

1 Answer 1


This is where the issue is with your code

ConName = CName.getArray("ConName");

You are asking the system to getArray by name "ConName". when you query using sforce.connection you get the result bask in the following format

$$$$${done:'true', queryLocator:null, records:{type:'Contact', Id:null, FirstName:'test', LastName:'dasfsdfsd', }, size:'1', }

In the above response the only array you see is "records" so your CName,getArray("ConName") is coming back undefined because there is no array by name ConName.


Change this to

 var ConName = CName.getArray("records");
  • @john most welcome
    – Rao
    Oct 13, 2017 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .