+ Start a Discussion
KevinLaurenceKevinLaurence 

Error in Function?

I'm trying to use the Ajax Toolkit to update a contact record when the contact stops working for their employer. Users click on a custom detail page button which then automatically changes selected fields on the current contact record and refreshes the screen. Here's the code:

Code:
{!requireScript("/soap/ajax/11.0/connection.js")}

// Prompt user for salesforce password
var username = "{!User.Username}";
var password = "";

var x = 550, y = 150;
var w = window.open("","PswdWin","width=" + x + ", height=" + y);
w.moveTo((screen.availWidth / 2) - (x / 2), (screen.availHeight / 2) - (y / 2));
w.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n');
w.document.write(' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n');
w.document.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n');
w.document.write('<head>\n');
w.document.write(' <meta http-equiv="content-type" content="text/html; charset=utf-8" />\n');
w.document.write(' <title>Salesforce User Password Form</title>\n');
w.document.write('</head>\n');
w.document.write('<body style="text-align: center;">\n');
w.document.write(' <form name="pswd" >\n');
w.document.write(' <p>Please enter your Salesforce password:</p>\n');
w.document.write(' <input type="password" name="password" size="28" />\n');
w.document.write(' <br /><br />\n');
w.document.write(' <input type="submit" name="submitbutton" value="Submit" onClick="window.opener.document.leftcompany(password.value); window.close();" />\n');
w.document.pswd.password.focus();
w.document.write(' </form>\n');
w.document.write('</body>\n');
w.document.write('</html>');
w.document.close();

function leftcompany(password)
{

// Login as user
sforce.connection.login("{!User.Username}", password);

// Create a new instance of the Contact object
MasterObject = new sforce.SObject("Contact");

// Set the Id of the new record to that of the currently viewed Contact
MasterObject["Id"] = "{!Contact.Id}";

// Amend the relevant fields given that we do not know where the Contact now works
MasterObject["Title"] = "Left Company";
MasterObject["Email"] = " ";
MasterObject["Phone"] = " ";
MasterObject["Fax"] = " ";
MasterObject["MobilePhone"] = " ";
MasterObject["Phone_Location_Code__c"] = " ";
MasterObject["Internal_Phone_Number__c"] = " ";
MasterObject["Do_Not_Post__c"] = 1;
MasterObject["HasOptedOutOfEmail"] = 1;
MasterObject["DoNotCall"] = 1;
MasterObject["HasOptedOutOfFax"] = 1;
MasterObject["Post_Preferred__c"] = 0;
MasterObject["Email_Preferred__c"] = 0;
MasterObject["Phone_Preferred__c"] = 0;
MasterObject["Fax_Preferred__c"] = 0;
MasterObject["Left_Company_User__c"] = "{!User.FirstName}" + " " + "{!User.LastName}";

// Update the relevant fields on the Contact record in Salesforce
sforce.connection.update([MasterObject]);

// Refresh the screen
window.top.location.href = window.top.location.href;
}

The error message I'm getting is "window.opener.document.leftcompany is not a function", but the function looks correctly defined to me.

Does anyone understand this error message, or can anyone tell me where I'm going wrong?

Thanks.