You need to sign in to do that
Don't have an account?
Reshmi Sangani 5
product Account trigger to update a checkbox field
I am trying to write a trigger on Account Object.
There are 2 types of Accounts. Product Accounts and Agency Account.
Product accounts have an Agency lookup field.
Requirement:
There is a checkbox field on Agency Accounts called "Agency_Commissionable__c". When this field is checked all the related product Account's( have a checkbox field called) "AdPoint_Sync_Due__c" should be check.
If "Agency_Commissionable__c" on Agency Accounts is unchecked then related Product Account's "AdPoint_Sync_Due__c" should be unchecked.
My Trigger:
//Author: Reshmi.S
// Name: AccTriggerHandlerUpdAccPaths
// Created: 09 /25/2020
// Event: after Insert and after update
// Desc: Refresh the child Accounts after Parent Account has been updated
public class AccTriggerHandlerUpdRelAgencyProducts extends TriggerHandlerBase {
public virtual override void mainEntry(TriggerParameters tp) {
UpdRelAgencyProducts(tp);
}
public virtual override void inProgressEntry(TriggerParameters tp) {
UpdRelAgencyProducts(tp);
}
void UpdRelAgencyProducts(TriggerParameters tp) {
List < Account > lstToUpdate = new List < Account > ();
List < Account > checkedAgncyAcc = new List < Account > ();
List < Account > unCheckedAgncyAcc = new List < Account > ();
List < Account > ListAcc = (List < Account > ) tp.newList;
Map < Id, Account > MapAcc = (Map < Id, Account > ) tp.newMap;
Map < Id, Account > oldAccMap = (Map < Id, Account > ) tp.oldMap;
Set < Id > checkedsetAccIds = new Set < Id > ();
Set < Id > unCheckedsetAccIds = new Set < Id > ();
//Map < id, Account > accMap = new Map < Id, Account > ();
for (Account acc: ListAcc) {
if (acc.Agency_Commissionable__c != oldAccMap.get(acc.Id).Agency_Commissionable__c) {
if (acc.Agency_Commissionable__c == true) {
checkedsetAccIds.add(acc.Id);
} else if (acc.Agency_Commissionable__c == false) {
unCheckedsetAccIds.add(acc.Id);
}
}
}
if (!checkedsetAccIds.isEmpty()) {
list < Account > accList = [select Id, name, AdPoint_Sync_Due__c, sync__c from Account where Agency__c IN: checkedsetAccIds AND Type = 'product'];
if (!accList.isEmpty()) {
for (Account acct: accList) {
acct.sync__c = true;
lstToUpdate.add(acct);
}
}
} else if (!unCheckedsetAccIds.isEmpty()) {
list < Account > accLists = [select Id, name, AdPoint_Sync_Due__c, sync__c from Account where Agency__c IN: unCheckedsetAccIds AND Type = 'product'];
if (!accLists.isEmpty()) {
for (Account accts: accLists) {
accts.sync__c = false;
lstToUpdate.add(accts);
}
}
}
if (lstToUpdate.size() > 0) database.update(lstToUpdate);
}
}
When I am trying to create a new product record, the following error is coming up
Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: AccountTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: ()
There are 2 types of Accounts. Product Accounts and Agency Account.
Product accounts have an Agency lookup field.
Requirement:
There is a checkbox field on Agency Accounts called "Agency_Commissionable__c". When this field is checked all the related product Account's( have a checkbox field called) "AdPoint_Sync_Due__c" should be check.
If "Agency_Commissionable__c" on Agency Accounts is unchecked then related Product Account's "AdPoint_Sync_Due__c" should be unchecked.
My Trigger:
//Author: Reshmi.S
// Name: AccTriggerHandlerUpdAccPaths
// Created: 09 /25/2020
// Event: after Insert and after update
// Desc: Refresh the child Accounts after Parent Account has been updated
public class AccTriggerHandlerUpdRelAgencyProducts extends TriggerHandlerBase {
public virtual override void mainEntry(TriggerParameters tp) {
UpdRelAgencyProducts(tp);
}
public virtual override void inProgressEntry(TriggerParameters tp) {
UpdRelAgencyProducts(tp);
}
void UpdRelAgencyProducts(TriggerParameters tp) {
List < Account > lstToUpdate = new List < Account > ();
List < Account > checkedAgncyAcc = new List < Account > ();
List < Account > unCheckedAgncyAcc = new List < Account > ();
List < Account > ListAcc = (List < Account > ) tp.newList;
Map < Id, Account > MapAcc = (Map < Id, Account > ) tp.newMap;
Map < Id, Account > oldAccMap = (Map < Id, Account > ) tp.oldMap;
Set < Id > checkedsetAccIds = new Set < Id > ();
Set < Id > unCheckedsetAccIds = new Set < Id > ();
//Map < id, Account > accMap = new Map < Id, Account > ();
for (Account acc: ListAcc) {
if (acc.Agency_Commissionable__c != oldAccMap.get(acc.Id).Agency_Commissionable__c) {
if (acc.Agency_Commissionable__c == true) {
checkedsetAccIds.add(acc.Id);
} else if (acc.Agency_Commissionable__c == false) {
unCheckedsetAccIds.add(acc.Id);
}
}
}
if (!checkedsetAccIds.isEmpty()) {
list < Account > accList = [select Id, name, AdPoint_Sync_Due__c, sync__c from Account where Agency__c IN: checkedsetAccIds AND Type = 'product'];
if (!accList.isEmpty()) {
for (Account acct: accList) {
acct.sync__c = true;
lstToUpdate.add(acct);
}
}
} else if (!unCheckedsetAccIds.isEmpty()) {
list < Account > accLists = [select Id, name, AdPoint_Sync_Due__c, sync__c from Account where Agency__c IN: unCheckedsetAccIds AND Type = 'product'];
if (!accLists.isEmpty()) {
for (Account accts: accLists) {
accts.sync__c = false;
lstToUpdate.add(accts);
}
}
}
if (lstToUpdate.size() > 0) database.update(lstToUpdate);
}
}
When I am trying to create a new product record, the following error is coming up
Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: AccountTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: ()
RituSharma
This requirement can be easily achieved using process builder and flow. So try doing declarative ways.