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
TimSTimS 

Need urgent help with a Custom Button to add related Account to custom object

Have a custom object (Customer Comment) to capture customer comments, containing lookup fields to both Contact and Account objects. Need to add a new comment from a Contact record, which standard "New" button populates the related Contact but not the related Account. So want to replace the standard "New" button on the custom object's related list on a contact record with a custom javascript button that will populate BOTH the custom object's related Contact and Account lookup fields with the related records.

Not a developer by trade, so using some code created by a former employee which did something similar with other obejcts, but am obviously missing something. The code below generates the error "Cannot read property 'Id' of undefined."

Here's the code...any assistance to this novice would be greatly appreciated...relaize it is probably something simple, but yet over my head and am running out of time.  HELP!!!!  Thanks in advance.....

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

//Used to trim the last three characters off of the Profile Id's in txtProfileId1, //txtProfileId2 and txtProfileId3 
function Left(str, n){ 
if (n <= 0) 
return ""; 
else if (n > String(str).length) 
return str; 
else 
return String(str).substring(0,n); 


var txtContact__c = "00NC0000006Epxb"; //SFDC ID of Contact__c custom field in Customer Comment object 
var txtAccount__c = "00NC0000006Epxg"; //SFID of Account__c custom field in Customer Comment object 
var txtObjPrefix = 'a2k'; //Three-letter prefix for Customer Comment custom object 
var txtContactId__c ="{!Contact.Id}"; //Related Contact SFDC Id 
var txtCustCommentName = "{!Customer_Comment__c.Name}"; //Customer Comment Name 
var txtAccount = "{!Customer_Comment__c.Account__c}"; //Related Account 
var txtAccountID = ""; //Account Id 

var txtAccountIdResult = sforce.connection.query("SELECT Id FROM Account WHERE Name = '" + txtAccount + "'"); 
txtAccountId = Left(txtAccountIdResult.getArray("records")[0].Id, 15); 

//Open a new Customer Comment record to add a new comment to a Contact that also populates the related Account lookup 
parent.location.href = "/" + txtObjPrefix + "/e?CF" + txtContact__c + "=" + txtCustCommentName + "&CF" + txtContact__c + "_lkid=" + txtContactId + "&CF" + txtAccount__c + "=" + txtAccount + "&CF" + txtAccount__c + "_lkid=" + txtAccountId + "&retURL=%2F" + txtContactId;