I have the following use case and have trouble to write a correct trigger for it
On object Account we save our patients. Every patient has a connected general practitioner (GP). The general practitioners are saved in object Contact. Sometimes a GP is retiring, a new GP takes over the general practice so al connected patients to that GP must change to the new GP.
I made in Salesforce in object Contact 2 fields, GPChange__c (boolean) and NewGP__c (lookup field on contact). When a user set the boolean field to true and fill in the new GP, the Apex trigger must be run and collect all accounts(patients) connected to the 'retired' GP and update the field GeneralPracticioner__c on account with the id from NewGP__C.
This is how far i come, it will run and collect, but doesn't update the field. Can anyone plz help me?
trigger trgnewGP on Contact (before update) {
for (Contact mycontact : Trigger.new) {
if (mycontact.GPChange__c = true && mycontact.NewGP__c != null){
list <account> gps = [SELECT GeneralPracticioner__c from account
where GeneralPracticioner__c =: mycontact.id];
for(Account acc : gps){
if (gps.size() > 0) {
acc.GeneralPracticioner__c = mycontact.NewGP__c;
} }
}
}
}