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
BrenzoBrenzo 

Execute Javascript Button to Create Contact Record from Related List (on Account Record)

I am trying to create a onclick, executable javascript list button that will appear from the account record and enable the following when clicked:
  • Create a new contact record, using values that appear in custom fields on the Account record where the button appears
  • Update a check box field on the Account record
  • Display a confirmation message
Below is the button that I have so far:

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

var ContactObj = new sforce.SObject("Contact");

ContactObj.Account='{!Account.Id}';
ContactObj.FirstName='{!Account.AP_First_Name__c}';
ContactObj.LastName='{!Account.AP_Last_Name__c}';
ContactObj.Phone='{!Account.Accounts_Payable_Phone__c}';
ContactObj.Responsible_for_A_P__c='1';

var result = sforce.connection.create([ContactObj]);

if(result[0].getBoolean("success")){
window.location = "/" + result[0].id + "/e";
}else{
alert('Could not create record '+result);
}


So a couple problems...

First, I'm getting an error that 'Account' doesn't exist on the contact object, which obviously is not the case. What am I messing up here? I know it's something small.

Second, I don't how to get it so that upon successfully creating the contact record, to also update the custom check box field on the account page. Idealy I'd like this to happen in the background and refresh the page so that the user isn't taken to the contact record page.

Thanks in advance for any help!
@anilbathula@@anilbathula@
Hi Brenzo,

Use Accountid instead of Account on contact object it will work.
To update check box field on account .first you need to query it for the field and update it .

Thanks
Anil.B
BrenzoBrenzo
I think 'Accountid' is the only combination that I didn't try, silly of me. 

Quick follow up @anilbathula...

I'm stuck trying to do two things with a modified version of this javascript button.
  1. I want to make it so that if a field on the account record is blank, clicking the button will take the user to a contact creation screen with certain values pre-populated. If this account field is not blank, then automatically create the contact record in the background and refresh the page (updating the account check box field in the process.)
  2. I've got the query set up (see below) to update the check box on the account page, but it isn't working as intended. If I had to guess, I have something out of order....
Updated code:

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

var APContact = '{!Account.Accounts_Payable_Contact__c}';

//Check to see if there is a value in the Accounts Payable Contact Field
if(APContact == "")
{
var ContactObj = new sforce.SObject("Contact");

//Default or pre-populated values for new contact record
ContactObj.Accountid='{!Account.Id}';
ContactObj.Responsible_for_A_P__c='1';
ContactObj.RecordTypeid='012G0000000GTlH';

var result = sforce.connection.update([ContactObj]);

if(result[0].getBoolean("success")){
window.location = "/" + result[0].id + "/e";
}else{
alert('Could not create record '+result);
}

}

else{

var ContactObj = new sforce.SObject("Contact");
var AccountObj = new sforce.SObject("Account");

AccountObj.A_P_Contact_Created__c='{!Account.A_P_Contact_Created__c}';
AccountObj.A_P_Contact_Created__c='1';
var result = sforce.connection.update([AccountObj]);

//Default or pre-populated values for new contact record
ContactObj.Accountid='{!Account.Id}';
ContactObj.FirstName='{!Account.AP_First_Name__c}';
ContactObj.LastName='{!Account.AP_Last_Name__c}';
ContactObj.Phone='{!Account.Accounts_Payable_Phone__c}';
ContactObj.Email='{!Account.Accounts_Payable_Email__c}';
ContactObj.Responsible_for_A_P__c='1';
ContactObj.RecordTypeid='012G0000000GTlH';

var result = sforce.connection.create([ContactObj]);

if (result[0].getBoolean("success")) {
alert("You have successfully created an accounts payable contact for this customer. To add or change contact details, please click on the related contact record.");
} else {
alert("Operation Failed: Please try manually create a contact record.");
}

//refresh the page
window.parent.location.reload()}

 

BrenzoBrenzo
Just realized I was missing the following to get the account field to update properly:

AccountObj.id='{!Account.Id}';

Still having trouble figuring out how to get to the contact creation screen should the initial criteria be met (APContact == "")