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
ASFASF 

how to change contact owner Name with related Account Owner Name

hi

 

 First i create a button on Account. and if i click that button that related contacts owner name is changed as Account owner Name. 

 

Please give me a solution for this... 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")} 

var url = parent.location.href;

var AccId = "{!Account.Id}"; 
var accOwnerId= "{!Account.OwnerId}";

var updateRecords = []; 
var update_Cons = new sforce.SObject("Contact"); 

var result = sforce.connection.query("SELECT Id, OwnerId, AccountId FROM Contact WHERE AccountId = '"+AccId +"'"); 
var recs = result.getArray("records");

if(confirm("Are you sure you want to update the name on related contacts?") )
{
    for(var i = 0; i < recs.length; i++)
    {
        recs[i].OwnerId = accOwnerId;
        updateRecords.push(recs[i]); 
    }    

var result = sforce.connection.update(updateRecords); parent.location.href = url; if(result[0].success == 'true') { alert("Related contacts updated successfully"); } else { alert("Failed to update the related contacts"); } }

 

Create a custom button on Account with 

Display Type : Detail Page Button

Behaviour : Execute Javascript

 

and copy this code...

 

let me know if it doesnt work.

All Answers

Jeff BloomerJeff Bloomer

Is the reason you are using a button for this because you don't intend for this to be done all of the time?  The reason I ask is because if you intend to do this for Contacts based on some criteria, you can implement this through a trigger instead and remove the need for a user to manually click a button to effect the change.

Naidu PothiniNaidu Pothini
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")} 

var url = parent.location.href;

var AccId = "{!Account.Id}"; 
var accOwnerId= "{!Account.OwnerId}";

var updateRecords = []; 
var update_Cons = new sforce.SObject("Contact"); 

var result = sforce.connection.query("SELECT Id, OwnerId, AccountId FROM Contact WHERE AccountId = '"+AccId +"'"); 
var recs = result.getArray("records");

if(confirm("Are you sure you want to update the name on related contacts?") )
{
    for(var i = 0; i < recs.length; i++)
    {
        recs[i].OwnerId = accOwnerId;
        updateRecords.push(recs[i]); 
    }    

var result = sforce.connection.update(updateRecords); parent.location.href = url; if(result[0].success == 'true') { alert("Related contacts updated successfully"); } else { alert("Failed to update the related contacts"); } }

 

Create a custom button on Account with 

Display Type : Detail Page Button

Behaviour : Execute Javascript

 

and copy this code...

 

let me know if it doesnt work.

This was selected as the best answer
ASFASF

Thanks Mr.Naidu. Its Working....  Thank you so much.............

ASFASF

hi Mr.Naidu..

 

 

       I have an another problem the same thing i want implement in campaign. I want create a button in Campaign. and if click that button i want to change the ownership of the campaign members. I tried your code. just modifying for campaign But i don't know how to make a query Because here i wnt to update three objects Contact Account and Lead. So i'm confused. If you Know  please help me.....

ASFASF

This is the Modified code. Please tell me where did i make a mistake....

 

 

 

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

var url = parent.location.href; 

var AccId = "{!Account.Id}"; 
var CampId ="{!Campaign.Id}"; 
var CampOwnerId= "{!Campaign.OwnerId}"; 

var updateRecords = []; 
var update_Cons = new sforce.SObject("Contact"); 

var result = sforce.connection.query("SELECT Id, Contact.OwnerId, Lead.OwnerId, campaignid FROM CampaignMember WHERE campaignid = "+CampId+""); 
var recs = result.getArray("records"); 

if(confirm("Are you sure you want to update the name on related contacts?") ) 

for(var i = 0; i < recs.length; i++) 

recs[i].Contact.OwnerId= CampOwnerId; 
recs[i]. Lead.OwnerId= CampOwnerId; 
recs[i].campaignid = CampOwnerId; 
updateRecords.push(recs[i]); 


var result = sforce.connection.update(updateRecords); 

parent.location.href = url; 

if(result[0].success == 'true') 

alert("Related contacts updated successfully"); 

else 

alert("Failed to update the related contacts"); 

}

Naidu PothiniNaidu Pothini

Let me check the above code.. i will reply you shortly....

Naidu PothiniNaidu Pothini
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")} 

var url = parent.location.href; 

var CampId ="{!Campaign.Id}"; 
var CampOwnerId= "{!Campaign.OwnerId}"; 

var updateRecords = []; 
var update_Cons = new sforce.SObject("Contact"); 

var result = sforce.connection.query("SELECT Id, Contact.OwnerId, Lead.OwnerId, campaignid FROM CampaignMember WHERE campaignid = '"+CampId+"'"); 
var recs = result.getArray("records"); 

if(confirm("Are you sure you want to update the name on related contacts?") ) 
{ 
for(var i = 0; i < recs.length; i++) 
{ 
recs[i].Contact.OwnerId= CampOwnerId; 
recs[i]. Lead.OwnerId= CampOwnerId; 
recs[i].campaignid = CampOwnerId; // Remove this, as you are trying to update campaignId with OwnerId
updateRecords.push(recs[i]); 
} 

var result = sforce.connection.update(updateRecords); 

parent.location.href = url; 

if(result[0].success == 'true') 
{ 
alert("Related contacts updated successfully"); 
} 
else 
{ 
alert("Failed to update the related contacts"); 
} 
}

 try this..