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
IT AccuAirIT AccuAir 

Update Account object from Contact List View

I have a List Button on a Contact List View which I've set to execute Javascript. I am trying to update a custom field on the Account associated with the selected Contact when the button is pressed. Right now I can't get the Account ID which relates the selected Contact(s). I will post my Javascript code below.

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
// Get list of selected Contact IDs
var records = {!GETRECORDIDS($ObjectType.Contact)};

if (records[0] == null) {
   alert("Please select at least one record to update.");
}

// Loop through Contact IDs
for(var i=0; i < records.length; i++) {
   // Create Account object to update field on
   var account = new sforce.SObject("Account");
   account.id = records[i]; // won't work because Account.Id != Contact.Id

   // Update custom field value
   account.My_Custom_Field__c = "value";
   sforce.connection.update([account]);
}
window.location.reload();

As you can see, I have most of the code ready to use. The only problem is that the Contact's id does not match the Account id. Is it possbile I could get the Contact.AccountId field value and use it to set account.id?
Best Answer chosen by IT AccuAir
Mathew Andresen 5Mathew Andresen 5
Yes, Contact.Accountid should match account.id.  You can see this by doing a query in the developer console

SELECT Name, Id, (SELECT AccountId, Name, Id FROM Contacts) FROM Account

 

All Answers

Mathew Andresen 5Mathew Andresen 5
Yes, Contact.Accountid should match account.id.  You can see this by doing a query in the developer console

SELECT Name, Id, (SELECT AccountId, Name, Id FROM Contacts) FROM Account

 
This was selected as the best answer
IT AccuAirIT AccuAir
Thanks for tip. I was able to use a SOQL to find the value of the Contact.AccountId field. Once I had that, I simply assigned that value to my Account object I was trying to update.

My query looks something like this "SELECT AccountId FROM Contact WHERE id='"+records[i]+"'"
Mathew Andresen 5Mathew Andresen 5
Glad I could help