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
Charni WigginsCharni Wiggins 

Method does not exist or incorrect signature? Simple trigger counting child records

I have a trigger on the child object (event regs), which should update the Account on create/update/delete etc. I have an error so am unable to test at the moment, but also unsure if I am following the correct logic. Event regs are Account Contacts, so I only want to count an event where at least 1 related contact has been (if 2 contacts attended, this counts as one event). Please help with my error and logic

Trigger:
trigger CountEventTrigger on Event_Registrants__c(Before Update) {
    if(Trigger.isBefore && Trigger.isUpdate){
        CountEventRegs.CountEventRegs_method(Trigger.New);
    }    
}
Helper class:

public class CountEventRegs{
    public static void CountEventRegs_method(List<Account> accountList){
        set<Id> accountIds = new set<Id>();
        Map<Id, Integer> accountMap = new Map<Id, Integer>();
        for (Account acc : accountList) {
            accountIds.add(acc.Id);
            accountMap.put(acc.Id, 0);
        }
        if(accountIds.size() > 0){
            List<Event_Registrants__c> eventRegList = [SELECT Id, Contact__r.AccountId FROM Event_Registrants__c WHERE Contact__r.AccountId IN: accountIds LIMIT 10000];
            for (Event_Registrants__c regs : eventRegList) {
                if (accountMap.containsKey(regs.Contact__r.AccountId)){
                     accountMap.put(regs.Contact__r.AccountId,accountMap.get(regs.Contact__r.AccountId)+1);
                }
            }
        }
        for(Account accObj : accountList){
            accObj.Events_Attended__c = accountMap.get(accObj.Id);
        }
    }
}


Error: Method does not exist or incorrect signature: void CountEventRegs_method(List<Event_Registrants__c>) from the type CountEventRegs

 

Thank you!

Ajay K DubediAjay K Dubedi
Hi Charni ,

CountEventRegs_method will contains the List Event_Registrants__c, not Account list because you write trigger on Event_Registrants__c not Account object.
So you need to update the signature of  CountEventRegs_method.

Like : 
public static void CountEventRegs_method(List<Event_Registrants__c> EventReg_list){
       Write your code here
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

 
Charni WigginsCharni Wiggins

Hi Ajay,

Thank you! I have adjusted, and now I have a problem with the update - I fear a lot more needs to be changed, do I need to map the integer value to the account Id, then do the update? Now I essentially have two lists I think I need to combine somehow. Let me know what you think. P.S. I haven't forgotten that this is your code suggestion from my other question. :)

Error: Variable does not exist: Events_Attended__c

public class CountEventRegs{
    public static void CountEventRegs_method(List<Event_Registrants__c> eventList){
        
        List<Account> accountList = [SELECT Id, Events_Attended__c FROM Account];
        set<Id> accountIds = new set<Id>();
        Map<Id, Integer> accountMap = new Map<Id, Integer>();
        for (Account acc : accountList) {
            accountIds.add(acc.Id);
            accountMap.put(acc.Id, 0);
        }
        if(accountIds.size() > 0){
            List<Event_Registrants__c> eventRegList = [SELECT Id, Contact__r.AccountId FROM Event_Registrants__c WHERE Contact__r.AccountId IN: accountIds LIMIT 10000];
            for (Event_Registrants__c regs : eventRegList) {
                if (accountMap.containsKey(regs.Contact__r.AccountId)){
                     accountMap.put(regs.Contact__r.AccountId,accountMap.get(regs.Contact__r.AccountId)+1);
                }
            }
        }
        for(Event_Registrants__c accObj : eventList){
            accountList.Events_Attended__c = accountMap.get(accObj.Id);
        }
    }
}
Charni WigginsCharni Wiggins

I have updated my code further, not sure if I'm any closer, current error: Variable does not exist: Events_Attended__c

public class CountEventRegs{
    public static void CountEventRegs_method(List<Event_Registrants__c> eventList){
        
        List<Account> accountList = [SELECT Id, Events_Attended__c FROM Account];
        set<Id> accountIds = new set<Id>();
        Map<Id, Decimal> accountMap = new Map<Id, Integer>();
        for (Account acc : accountList) {
            accountIds.add(acc.Id);
            accountMap.put(acc.Id, acc.Events_Attended__c);
        }
        if(accountIds.size() > 0){
            List<Event_Registrants__c> eventRegList = [SELECT Id, Contact__r.AccountId FROM Event_Registrants__c WHERE Contact__r.AccountId IN: accountIds LIMIT 10000];
            for (Event_Registrants__c regs : eventRegList) {
                if (accountMap.containsKey(regs.Contact__r.AccountId)){
                     accountMap.put(regs.Contact__r.AccountId,accountMap.get(regs.Contact__r.AccountId)+1);
                }
            }
        }
        for(Event_Registrants__c accObj : eventList){
            accObj.Events_Attended__c == accountMap.get(accObj.Id);
        }
    }
}
Ajay K DubediAjay K Dubedi
Hi Charni ,
In last for loop :
        for(Event_Registrants__c accObj : eventList){
            accObj.Events_Attended__c == accountMap.get(accObj.Id);
        }
But according to your above code Events_Attended__c is the field of the Account object .
From :List<Account> accountList = [SELECT Id, Events_Attended__c FROM Account];
So you need to correct it.