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
matty80matty80 

Adding Contact Transfer to Account Transfer custom button

I've cobbled together a button to transfer Accounts to a particular user with one click versus going through the change link - it works great but the ownership does not change on Contacts when executed. Could anyone offer some recommendations or help me add some code here to execute transfers of any Contacts on that Account to a particular user? I'm not very experienced in Java, or coding - but it seems like one route would be to count the number of contacts, find those ids and then execute transfers one by one - I'm open to any options or suggestions.

 

Here's the main transfer element of the code (there are some other tests on ownership, etc that are run prior to executing the code):

 

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

var acctObj = new sforce.SObject("Account");
acctObj.Id = '{!Account.Id}';
acctObj.OwnerId = '{!Account.OwnerId}';

 

acctObj.OwnerId = '005A0000000TBEF';

var newRecords = [];

acctObj.Last_Transferred_to_House__c = new Date();

newRecords.push(acctObj);

result = sforce.connection.update(newRecords);

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

location.reload(true);

 

--

 

Thank you,

 Matt.

SoleesSolees

Accounts and Contacts are Master-Detail unless you have Person Account.

 

{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")}
var __sfdcSessionId = '{!GETSESSIONID()}';
try {
var strQuery="Select Id from Contact where AccountId = '" +'{!Account.Id}'+"'";
var newRecords = [];
var otherP = sforce.connection.query(strQuery);
var records = otherP.getArray("records");
  if (records.length >= 1) {
    for(var i=0; i < records.length ; i ++) {
	var ob = new sforce.SObject("Contact");
        ob.OwnerId = '005A0000000TBEF';
	var result = sforce.connection.update([ob]);
    }
    alert(result);
    location.reload(true);
  }
}catch(er) {
  alert(er);
}