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
EricLehnenEricLehnen 

Change Account Owner via Contact Data

Hey all,

 

First time posting on the dev boards...

 

Anyways, I have maxed out our org with Formulas, WFR's and validation rules and I am at a point where a trigger is necessary.

 

I am trying to change the Account Owner to a specific person if a Contact has:

 

1. The field "Attended_SIS_Demo__c" = True

2. Current Account Owner is someone else

3. Account Billing State = MN,ID,MT

 

If anyone would be willing to start this out for me in APEX, I would be very grateful. I unfortunately do not know much about APEX.

 

Cheers!

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

You can use below logic.

 

List<Account> lstAccount = [Select id, OwnerId From Account Where Id in(Select AccountId From Contact Where Attended_SIS_Demo__c=true And 
Account_Billing_State__c in ('MN','ID','MT'))];

if(lstAccount.size()>0){
	for(Account acc:lstAccount){
		acc.OwnerId = '005d0000001Jm8k';
	}
	Update lstAccount;
}

 

All Answers

Dhaval PanchalDhaval Panchal
You want to set contact as a owner? Please explain who will be new owner for account.
EricLehnenEricLehnen

I am trying to change the account owner to "005d0000001Jm8k" when any contact within an account meets the following criteria:

 

1. The field "Attended_SIS_Demo__c" = True

2. Current Account Owner is "005d00000014RxE"

3. Account Billing State = "MN,ID,MT"

Dhaval PanchalDhaval Panchal

You can use below logic.

 

List<Account> lstAccount = [Select id, OwnerId From Account Where Id in(Select AccountId From Contact Where Attended_SIS_Demo__c=true And 
Account_Billing_State__c in ('MN','ID','MT'))];

if(lstAccount.size()>0){
	for(Account acc:lstAccount){
		acc.OwnerId = '005d0000001Jm8k';
	}
	Update lstAccount;
}

 

This was selected as the best answer