0

I am using JSENCODE in a custom button to populate a Contact lookup field on a new Opportunity record from a custom Javascript button. However, the field gets populated with the 15 character ID rather than the Contact's name. I've tried Contact.Name and Contact__r.Name, and both returns errors. Any idea how I can get the Contact's Name instead of the ID?

{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")}
if (typeof(srcUp) == 'function')
{
srcUp('/006/e?retURL={!Case.Id}&accid={!Account.Id}&RecordType=012a0000001Fsxa&opp11=Quote&00Na000000BODU0=Small Quote&opp3={!Case.Account} - Quote&opp9={!TODAY()+30}&CF00Na000000BAz6W={!JSENCODE(TEXT(Case.Contact.Name))}&ent=Opportunity');
}
else
{
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={!JSENCODE(TEXT(Case.Contact.Name))}&ent=Opportunity');
}
5

If you want to merge in the name, you need to merge in the Name field.

{!JSENCODE(Case.Contact.Name)}

Make sure you add this field to your query.

  • When I try that, it tells me Case.Contact.Name does not exist. – John Neilan Oct 11 '17 at 23:08
  • 1
    @JohnNeilan This is why you should provide complete context when you ask the question... – Adrian Larson Oct 11 '17 at 23:10
  • Sorry, posted too quickly – John Neilan Oct 11 '17 at 23:13
  • Do you know if it's possible to get the name of a lookup field rather than the ID using JSENCODE? – John Neilan Oct 12 '17 at 13:59
  • It has nothing to do with that function. It's the JS button context which matters... – Adrian Larson Oct 12 '17 at 14:08
0

For anyone curious, the code below fixes the issue I was trying to solve:

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

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

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

ConName = CName.getArray("records");
console.log('###'+ConName);
var myCon1 = ConName[0].FirstName;
var myCon2 = ConName[0].LastName;

if (typeof(srcUp) == 'function')
{
srcUp('/006/e?retURL={!Case.Id}&accid={!Account.Id}&RecordType=012a0000001Fsxa&opp11=Quote&00Na000000BODU0=Small Quote&opp3={!Case.Account} - Quote&opp9={!TODAY()+30}&CF00Na000000BAz6W={!JSENCODE(Case.Contact_Text__c)}&ent=Opportunity');
}
else
{
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+' '+myCon2+'&ent=Opportunity');
}

I queried the Name of the record I was trying to pull in as text.

  • 1
    Please add some explanation rather than just a code dump. Also, if implementing a formula field was the solution, your answer doesn't really help others in the same quandary unless you also share the formula. – Adrian Larson Oct 13 '17 at 23:42
  • I didn't use a formula field – John Neilan Oct 20 '17 at 14:33
  • 1
    Please edit your post to add explanation. Right now this post is pretty much just a code dump, making it rather low quality. – Adrian Larson Oct 20 '17 at 14:35
-2

No need to use JSENCODE function or query the name value.

Just use double quote (") instead of single quote (') in URL like below, it should work.

if (typeof(srcUp) == 'function')
{
    srcUp("/006/e?retURL={!Case.Id}&accid={!Account.Id}&RecordType=012a0000001Fsxa&opp11=Quote&00Na000000BODU0=Small Quote&opp3={!Case.Account} - Quote&opp9={!TODAY()+30}&CF00Na000000BAz6W={!JSENCODE(TEXT(Case.Contact.Name))}&ent=Opportunity");
}else
{
    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={!JSENCODE(TEXT(Case.Contact.Name))}&ent=Opportunity");
}
  • Actually, you need JSENCODE to prevent from all kinds of JS attacks. Not because of the quotes. – Sebastian Kessel Dec 3 '18 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.