You need to sign in to do that
Don't have an account?
IT 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?
{!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?
SELECT Name, Id, (SELECT AccountId, Name, Id FROM Contacts) FROM Account
All Answers
SELECT Name, Id, (SELECT AccountId, Name, Id FROM Contacts) FROM Account
My query looks something like this "SELECT AccountId FROM Contact WHERE id='"+records[i]+"'"