You need to sign in to do that
Don't have an account?
Tony Garand
updating all contact owners to match account owners via developer console
Hello Developer Community!
I am tasked with updating my company's whole database so that the contact owners match the Account owners. There are around 5k records to change and it would be taxing to go through an excel sheet and change them and re-load them via dataloader. So, I figured some code could do the trick in the Developer Console via Execute Anonymous Window. Would the code below work?
Let me know your thoughts and thank you for your time.
Tony Garand
I am tasked with updating my company's whole database so that the contact owners match the Account owners. There are around 5k records to change and it would be taxing to go through an excel sheet and change them and re-load them via dataloader. So, I figured some code could do the trick in the Developer Console via Execute Anonymous Window. Would the code below work?
Let me know your thoughts and thank you for your time.
Tony Garand
Use this code .
for(contact c:[SELECT Id, AccountId, Ownerid, Account.ownerid from contact])
Change to
for(contact c:[SELECT Id, AccountId, Ownerid, Account.ownerid from contact limit 5000])
And change the Query from
SELECT Id, AccountId, Ownerid, Account.ownerid from contact
to
SELECT Id, AccountId, Ownerid, Account.ownerid from contact where AccountId != null limit 5000
Rest is fine.
Please let me know if it helped.
Ramakant would the batch command be best for running this many records? or can I just use Rajamohan.Vakati's code?
You can ignore limit 5K in the query (I had given as you mentioned you have around 5K so, limiting to 5K only) & instead you can keep what I mentioned below.
There is not much diff between Rajamohan.Vakati's code & mine. Both are same only in my query itself I am taking out the leads who are not associated with any account (AccountIds) record where as in his it's getting checked with in the loop in a if condition.
You can use anyone's code both will give same result.
My modified code
Let me know if my explantion help you.
Thank you for the follow up! Just to clarify this code will make the owner in red the owner of the contacts assocated with the accounts?
You are welcome & yes account owner will be set to associated contact's owners if it's different than account owner.
Should I use Ramakant's Batch Command?
For ref https://help.salesforce.com/articleView?id=000232681&language=en_US&type=1
But yes you can use the batch command, use the below code I changed few lines.
remember you have to create an apex class with the batch class name "RunAccountBatch" and then go to Anony Execute window & run the below two lines of code:
Please let me know if it help you out.
If yes, then please approve it & to keep the board clean mark it as answered.