You need to sign in to do that
Don't have an account?
![Vignesh Ramshetty Vignesh Ramshetty](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Need to update of child bases on ceteria
HI
Below is the code which iam calling whenever the record is inserted , updated, deleated , undeleated i able to get the count of releated to that account But i want the filter the count based on policy status and need to update that size can anyone help on this please.
Trigger forcounttherecords on Customer_Policy_details__c (After insert, After Update , After delete ,After undelete){
if(trigger.isafter==true &&(trigger.isinsert== true || trigger.isupdate == true || trigger.isdelete == true || trigger.isundelete == true)){
forcountingCDPrecords.Methodforcounting(trigger.new,trigger.old);
}
}
Public class forcountingCDPrecords{
Public Static void Methodforcounting (List<Customer_Policy_details__c> varnewCon,List<Customer_Policy_details__c> varold){
map<Id,Customer_Policy_details__c> varActivelist = new map<Id,Customer_Policy_details__c>();
map<Id,Customer_Policy_details__c> varGettingexpitedlist = new map<Id,Customer_Policy_details__c>();
map<Id,Customer_Policy_details__c> varexpiredlist = new map<Id,Customer_Policy_details__c>();
if (varnewCon != null){
for(Customer_Policy_details__c varc : varnewCon){
if(varc.Account__c != null && varc.Policy_Status__c == 'Active'){
varActivelist.put(varc.Account__c,varc);
}
else if (varc.Account__c != null && varc.Policy_Status__c == 'Getting Expited'){
varGettingexpitedlist.put(varc.Account__c,varc);
}
else if (varc.Account__c != null && varc.Policy_Status__c == 'Expired'){
varexpiredlist.put(varc.Account__c,varc);
}
}
}
if (varold != null){
for(Customer_Policy_details__c varcc : varold){
if(varcc.Account__c != null && varcc.Policy_Status__c == 'Active'){
varActivelist.put(varcc.Account__c,varcc);
}
else if(varcc.Account__c != null && varcc.Policy_Status__c == 'Getting Expited'){
varGettingexpitedlist.put(varcc.Account__c,varcc);
}
else if (varcc.Account__c != null && varcc.Policy_Status__c == 'Expired'){
varexpiredlist.put(varcc.Account__c,varcc);
}
}
}
List<Account> GettingActivelist= [SELECT id,Expired__c,Active_Policies__c,Acction_Required__c,(SELECT id,Policy_Status__c FROM Customer_Policy_details__r )
FROM Account Where id in: varActivelist.keyset()];
if(GettingActivelist.size() > 0) {
for(Account a : GettingActivelist){
a.Active_Policies__c = a.Customer_Policy_details__r.size();
}
update GettingActivelist;
}
List<Account> GettingActionrequiredlist= [SELECT id,Expired__c,Active_Policies__c,Acction_Required__c,
(SELECT id,Policy_Status__c FROM Customer_Policy_details__r )
FROM Account Where id in: varGettingexpitedlist.keyset()];
if(GettingActivelist.size() > 0) {
for(Account a : GettingActionrequiredlist){
a.Acction_Required__c = a.Customer_Policy_details__r.size();
}
update GettingActionrequiredlist;
}
List<Account> GettingExpiredlist= [SELECT id,Expired__c,Active_Policies__c,Acction_Required__c,
(SELECT id,Policy_Status__c FROM Customer_Policy_details__r )
FROM Account Where id in: varexpiredlist.keyset()];
if(GettingExpiredlist.size() > 0) {
for(Account a : GettingExpiredlist){
a.Expired__c = a.Customer_Policy_details__r.size();
}
update GettingExpiredlist;
}
}
}
Below is the code which iam calling whenever the record is inserted , updated, deleated , undeleated i able to get the count of releated to that account But i want the filter the count based on policy status and need to update that size can anyone help on this please.
Trigger forcounttherecords on Customer_Policy_details__c (After insert, After Update , After delete ,After undelete){
if(trigger.isafter==true &&(trigger.isinsert== true || trigger.isupdate == true || trigger.isdelete == true || trigger.isundelete == true)){
forcountingCDPrecords.Methodforcounting(trigger.new,trigger.old);
}
}
Public class forcountingCDPrecords{
Public Static void Methodforcounting (List<Customer_Policy_details__c> varnewCon,List<Customer_Policy_details__c> varold){
map<Id,Customer_Policy_details__c> varActivelist = new map<Id,Customer_Policy_details__c>();
map<Id,Customer_Policy_details__c> varGettingexpitedlist = new map<Id,Customer_Policy_details__c>();
map<Id,Customer_Policy_details__c> varexpiredlist = new map<Id,Customer_Policy_details__c>();
if (varnewCon != null){
for(Customer_Policy_details__c varc : varnewCon){
if(varc.Account__c != null && varc.Policy_Status__c == 'Active'){
varActivelist.put(varc.Account__c,varc);
}
else if (varc.Account__c != null && varc.Policy_Status__c == 'Getting Expited'){
varGettingexpitedlist.put(varc.Account__c,varc);
}
else if (varc.Account__c != null && varc.Policy_Status__c == 'Expired'){
varexpiredlist.put(varc.Account__c,varc);
}
}
}
if (varold != null){
for(Customer_Policy_details__c varcc : varold){
if(varcc.Account__c != null && varcc.Policy_Status__c == 'Active'){
varActivelist.put(varcc.Account__c,varcc);
}
else if(varcc.Account__c != null && varcc.Policy_Status__c == 'Getting Expited'){
varGettingexpitedlist.put(varcc.Account__c,varcc);
}
else if (varcc.Account__c != null && varcc.Policy_Status__c == 'Expired'){
varexpiredlist.put(varcc.Account__c,varcc);
}
}
}
List<Account> GettingActivelist= [SELECT id,Expired__c,Active_Policies__c,Acction_Required__c,(SELECT id,Policy_Status__c FROM Customer_Policy_details__r )
FROM Account Where id in: varActivelist.keyset()];
if(GettingActivelist.size() > 0) {
for(Account a : GettingActivelist){
a.Active_Policies__c = a.Customer_Policy_details__r.size();
}
update GettingActivelist;
}
List<Account> GettingActionrequiredlist= [SELECT id,Expired__c,Active_Policies__c,Acction_Required__c,
(SELECT id,Policy_Status__c FROM Customer_Policy_details__r )
FROM Account Where id in: varGettingexpitedlist.keyset()];
if(GettingActivelist.size() > 0) {
for(Account a : GettingActionrequiredlist){
a.Acction_Required__c = a.Customer_Policy_details__r.size();
}
update GettingActionrequiredlist;
}
List<Account> GettingExpiredlist= [SELECT id,Expired__c,Active_Policies__c,Acction_Required__c,
(SELECT id,Policy_Status__c FROM Customer_Policy_details__r )
FROM Account Where id in: varexpiredlist.keyset()];
if(GettingExpiredlist.size() > 0) {
for(Account a : GettingExpiredlist){
a.Expired__c = a.Customer_Policy_details__r.size();
}
update GettingExpiredlist;
}
}
}
try with below code and modify the field API names as per your org.
If this helps, Please mark it as best answer.
Thanks!!
forcounttherecords: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 0015g000011jpg2AAA Trigger.forcounttherecords: line 77, column 1