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
Dave The RaveDave The Rave 

Convert DML Statement to Apex Class

As a admin, I have created a DML Statement to run in the Developer Console adhoc whenever I need to change account record owners en mass.

Now I need to convert that into an Apex Class and Trigger so that when records are uploaded from an external party the account record owner will be changed automatically based on shippingcity. See DML Code below:
 
List<Account> recList = [select id, shippingcountry, ownerid, name, shippingcity FROM Account WHERE shippingcountry = 'NL' AND sasid__c = null and ownerid='0052o00000AHDCeAAP' AND shippingcity IN ('Nijmegen','Arnhem')];
for (Account rec : recList) {
rec.ownerid = '0052o00000AI4zfAAD';
}
update recList;

3rd Party OwnerID = '0052o00000AHDCeEAP'
New record owner = '0052o00000AI4zfACD'
Best Answer chosen by Dave The Rave
vishal-negandhivishal-negandhi

Hi Dave, 

Although hard-coded Id's are not recommended in code (code would break in different salesforce instances, user deactivation, etc), I'm writing the trigger snippet below to help you get started

 

trigger AccountTrigger on Account(before insert){
	for(Account acc : Trigger.new){
		if(acc.OwnerID == '0052o00000AHDCeAAP' && acc.ShippingCountry == 'NL' && acc.sasid__c == null  && (acc.ShippingCity == 'Nijmegen' || acc.ShippingCity == 'Arnhem')){
		   acc.OwnerId =  '0052o00000AI4zfAAD';
		}
	}
}
 

Hope it helps!

 

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Dave,

Greetings!

Please find the document for the sample example which might help you to proceed further on this.

https://www.tutorialspoint.com/apex/apex_dml.htm

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
vishal-negandhivishal-negandhi

Hi Dave, 

Although hard-coded Id's are not recommended in code (code would break in different salesforce instances, user deactivation, etc), I'm writing the trigger snippet below to help you get started

 

trigger AccountTrigger on Account(before insert){
	for(Account acc : Trigger.new){
		if(acc.OwnerID == '0052o00000AHDCeAAP' && acc.ShippingCountry == 'NL' && acc.sasid__c == null  && (acc.ShippingCity == 'Nijmegen' || acc.ShippingCity == 'Arnhem')){
		   acc.OwnerId =  '0052o00000AI4zfAAD';
		}
	}
}
 

Hope it helps!

 

This was selected as the best answer
Dave The RaveDave The Rave
Hi Vishal, thanks this looks good. Point taken about hard coding IDs, I will use <owner.name>. I have a question no, as an admin. I understand you have apex triggers, apex classes and test apex classes. My understand was that you use a Apex Trigger to trigger an Apex Class, I see here you do not need an Apex Class, so can you just amend records using an apex trigger? please explain, ok I am an admin hense the question.
vishal-negandhivishal-negandhi

Hi Dave, 

In an ideal world, all business logic should like in an apex class which is then called by the apex trigger. We call these classes trigger helpers. But again, it's a best practice and not the only way to write code. 
I hope this answers your question. 

Also, if your original query is resolved, please mark the right answer as the best answer to help others.

Regards,

Vishal