0

I have written an apex sharing class and trying to invoke it with a trigger but I am getting error as "Method does not exist or incorrect signature: void shareMEUEnrollmentWithUsers(List<MEU_Enrollment__c>) from the type shareMEUEnrollment." Why it is showing error?Please help.

trigger shareMEUEnrollmentTrigger1 on MEU_Enrollment__c (before insert, after insert, before update, after update) {
    if(trigger.isbefore || trigger.isafter){
        if(trigger.isinsert || trigger.isupdate){
        shareMEUEnrollment.shareMEUEnrollmentWithUsers(trigger.new);

Apex Class

public without sharing class shareMEUEnrollment {
    
    public static void shareMEUEnrollmentWithUsers(List<ID> recordId) {
        List<MEU_Enrollment__Share> enrolExistingSharingList = [select id from MEU_Enrollment__Share where id in : recordId and RowCause = 'Manual'];
        if(enrolExistingSharingList.size() > 0) {
            delete enrolExistingSharingList ;  
        }
        
        List<MEU_Enrollment__c> enrolList = [select id, Owner.Id from MEU_Enrollment__c where id in : recordId ];
        Set<Id> OwnerIdSet = new Set<Id>();
        
        for(MEU_Enrollment__c l : enrolList) {
            OwnerIdSet.add(l.OwnerId);
        }
        
        List<User> userIdList = [select id, contact.Id from user where id in : OwnerIdSet];
        List<id> contactIdSet = new List<id>();
        Map<id,id> contactUserMap = new Map<id,id>();
        
        for(User u : userIdList) {
            contactIdSet.add(u.Contact.Id);
            contactUserMap.put(u.id, u.Contact.Id);
        }
        
        Map<Id, Id> enrolContactMap = new Map<Id,Id>();
        
        for(MEU_Enrollment__c l : enrolList) {
            enrolContactMap.put(l.id, contactUserMap.get(l.Owner.Id));
        }
        
        List<Contact> agentContactsList = [select id, Account.ParentId from Contact where id in :contactIdSet ];
        Set<Id> parentAccountId = new Set<Id>();
        Map<Id,Id> contactAccountMap = new Map<Id, Id>();
        for(Contact con : agentContactsList ) {
            if(con.Account.ParentId!=null) {
                parentAccountId.add(con.Account.ParentId);
                contactAccountMap.put(con.id,con.Account.ParentId);
            }
        }
        
        Map<id, List<Contact>> accountAdminsMap = new Map<Id,List<Contact>>();
        List<Contact> adminsList = new List<Contact>();
        Set<Id> adminContactId = new Set<Id>();
        
        for(Contact c : [select id,AccountId from Contact where accountid in : parentAccountId]) {
            adminsList.add(c);
            accountAdminsMap.put(c.AccountId, adminsList);
            adminContactId.add(c.id);
        }   
        
        Map<id,List<Contact>> enrolAdminsMap = new Map<Id,List<Contact>>();
        
        for(MEU_Enrollment__c l : enrolList) {
            if(accountAdminsMap.get(contactAccountMap.get(enrolContactMap.get(l.id)))!=null)
                enrolAdminsMap.put(l.id, accountAdminsMap.get(contactAccountMap.get(enrolContactMap.get(l.id))));
        }
        
        Map<id,id> adminContactUserMap = new Map<id,id>();   
        //map to store the mapping of admin contact id and corresponding user id
        for(User u : [select id,contactid from user where contactid in : adminContactId]) {
            adminContactUserMap.put(u.ContactId,u.id);
        } 
        
        List<MEU_Enrollment__Share> enrolShareNew = new List<MEU_Enrollment__Share>();
        
        for(MEU_Enrollment__c l : enrolList) {
            
            if(enrolAdminsMap.get(l.id) != null) {
                
                for(Contact c : enrolAdminsMap.get(l.id)) {
                    
                    if(adminContactUserMap.get(c.id) != null ) {
                        
                        MEU_Enrollment__Share ls = new MEU_Enrollment__Share();
                        ls.ParentId = l.id ;
                        ls.AccessLevel = 'Edit' ;
                        ls.UserorGroupId = adminContactUserMap.get(c.id);
                        enrolShareNew.add(ls);
                    }
                }
            }
        }
        
        System.debug('enrolShareNew : ' + enrolShareNew);
        System.debug('Total enrolShareNew : ' + enrolShareNew.size());
        
        try {
            Database.insert(enrolShareNew);
        }
        catch(Exception ex) {
            System.debug('Exceptiom is " '+ex.getMessage());
        }
    }
    
    @InvocableMethod
    public static void shareMEURecords(List<ID> leadIds) {
        try {
            //Share enrollment records to lead owner and respective GA Admins 
            List<MEU_Enrollment__Share> enrollShareList = new List<MEU_Enrollment__Share>();
            enrollShareList.addAll( prepareEnrollmentShare(leadIds) );
            
            if(!enrollShareList.isEmpty() && enrollShareList.size() > 0) {
                Database.insert(enrollShareList);
            }
        }
        catch(exception e) {
            String commentStr = 'Line Number and Exception Message : \n';
            commentStr = commentStr + 'Line Number - ' + e.getLineNumber() + '\n';
            commentStr = commentStr + 'Error Message : ' + e.getMessage();
            
            Apex_Debug_Log__c logRecord = new Apex_Debug_Log__c();
            logRecord.Class__c = 'shareMEUEnrollment';
            logRecord.Comment__c = commentStr;
            logRecord.Message__c = 'Stack Trace Error Message - ' + e.getStackTraceString();
            logRecord.Method__c = 'execute';
            logRecord.Status__c = 'Error';
            
            insert logRecord;
            system.debug('@@@shareMEUEnrollment - shareMEURecords - Error - commentStr : ' + commentStr);
        }
    }
    
    public static List<MEU_Enrollment__Share> prepareEnrollmentShare(List<ID> leadIds) {
        System.debug('@@@shareMEUEnrollment - prepareEnrollmentShare - Method Entry - leadIds : ' + leadIds);
        
        List<Group> queueList = [ Select Id, Name, DeveloperName From Group Where Type = 'Queue' And DeveloperName = 'CMC_Internal_Queue' Limit 1];
        String cmcInternalQueueId = queueList.get(0).Id;
        
        MEU_Enrollment__Share enrShare = new MEU_Enrollment__Share();
        Set<Id> enrollmentIds = new Set<Id>();
        Map<Id, String> queueMap = new Map<Id, String>();
        Map<Id, Id> mapLeadOwnerIds = new Map<Id, Id>();
        Map<Id, Id> mapLeadGaAdminQueueIds = new Map<Id, Id>();
        List<MEU_Enrollment__Share> enrollShareList = new List<MEU_Enrollment__Share>();
        List<MEU_Enrollment__c> enrollmentList = new List<MEU_Enrollment__c>();
        
        List<Lead> leadList = [Select Id, Name, OwnerId, (Select Id, Name, Lead__c, OwnerId From MEU_Enrollments__r)
                               From lead Where Id In :leadIds];
        
        if(leadList.size() > 0) {
            for(Lead ld : leadList) {
                mapLeadOwnerIds.put(ld.Id, ld.OwnerId);
                enrollmentList.addAll(ld.MEU_Enrollments__r);
            }
        }
        System.debug('@@@shareMEUEnrollment - prepareEnrollmentShare - enrollmentList : ' + enrollmentList);
        
        //Get the GA Admin Queue Id
        mapLeadGaAdminQueueIds.putAll( getGaAdminQueues(leadList, mapLeadOwnerIds) );
        System.debug('@@@shareMEUEnrollment - prepareEnrollmentShare - mapLeadGaAdminQueueIds : ' + mapLeadGaAdminQueueIds);
        
        if(enrollmentList.size() > 0) {
            for(MEU_Enrollment__c enr : enrollmentList) {
                enrollmentIds.add(enr.Id);
            }
        }
        
        //Delete all existing share records
        List<MEU_Enrollment__Share> existingEnrollShareList = [Select Id From MEU_Enrollment__Share Where ParentId In :enrollmentIds 
                                                               and RowCause = 'Manual' And UserorGroupId <> :cmcInternalQueueId];
        System.debug('@@@shareMEUEnrollment - prepareEnrollmentShare - existingEnrollShareList : ' + existingEnrollShareList);
        if(!existingEnrollShareList.isEmpty() && existingEnrollShareList.size() > 0) {
            delete existingEnrollShareList;
        }
        
        //Prepare new enrollment share list
        if(enrollmentList.size() > 0) {
            for(MEU_Enrollment__c enr : enrollmentList) {
                System.debug('@@@shareMEUEnrollment - Enrollment Name : ' + enr.Name);
                
                //Share the record to Lead Owner
                enrShare = new MEU_Enrollment__Share();
                enrShare.ParentId = enr.Id;
                enrShare.AccessLevel = 'Edit';
                enrShare.RowCause = 'Manual';
                enrShare.UserorGroupId = mapLeadOwnerIds.get(enr.Lead__c);
                enrollShareList.add(enrShare);
                System.debug('@@@shareMEUEnrollment - Share Record with Leads current owner - ' + enr.Name + ' - ' + enrShare);
                
                //Share the record to GA Admin (while lead owner changed to Agent)
                if(mapLeadGaAdminQueueIds.containsKey(enr.Lead__c) && String.isNotBlank(mapLeadGaAdminQueueIds.get(enr.Lead__c)) ) {
                    enrShare = new MEU_Enrollment__Share();
                    enrShare.ParentId = enr.Id;
                    enrShare.AccessLevel = 'Edit';
                    enrShare.RowCause = 'Manual';
                    enrShare.UserorGroupId = mapLeadGaAdminQueueIds.get(enr.Lead__c);
                    
                    if(! enrollShareList.contains(enrShare)) {
                        enrollShareList.add(enrShare);
                    }
                    System.debug('@@@shareMEUEnrollment - (To Admin) Share Record for ' + enr.Name + ' - ' + enrShare);
                }
            }
        }
        
        System.debug('@@@shareMEUEnrollment - prepareEnrollmentShare - Method Exit - enrollShareList : ' + enrollShareList);
        return enrollShareList;
    }
    
    public static Map<Id, Id> getGaAdminQueues(List<Lead> leadList, Map<Id, Id> mapLeadOwnerIds) {
        String agencyName = '';
        Set<String> queueNameSet = new Set<String>();
        Map<String, String> agencyQueueNameMap = new Map<String, String>();
        Map<String, String> agencyQueueIdMap = new Map<String, String>();
        Map<String, String> userAgencyMap = new Map<String, String>();
        Map<Id, Id> mapLeadGaAdminQueueIds = new Map<Id, Id>();
        
        List<User> userList = [Select Id, Name, Department From User Where Id In :mapLeadOwnerIds.values()];
        
        if(!userList.isEmpty() && userList.size() > 0) {
            for(User u : userList) {
                userAgencyMap.put(u.Id, u.Department);
            }
        }
        
        List<Agency_Queue_Detail__mdt> agencyQueueList = [Select Label, DeveloperName, Queue_Label__c, Queue_Name__c
                                                          From Agency_Queue_Detail__mdt
                                                          Where Is_Agency_Active__c = true ];
        
        //system.debug('agencyQueueList : '+agencyQueueList);
        
        for(Agency_Queue_Detail__mdt aq : agencyQueueList){
            if(String.isNotBlank(aq.Queue_Name__c)){
                queueNameSet.add(aq.Queue_Name__c);
                agencyQueueNameMap.put(aq.Label, aq.Queue_Name__c);
            }
        }
        
        List<Group> grpDetailList = [ Select Id, Name, DeveloperName From Group Where Type = 'Queue' AND DeveloperName In :queueNameSet ];
        for(Group grp : grpDetailList){
            agencyQueueIdMap.put(grp.DeveloperName, grp.Id);
        }
        
        for(Lead ld : leadList) {
            agencyName = '';
            agencyName = userAgencyMap.get(ld.OwnerId);
            mapLeadGaAdminQueueIds.put(ld.Id, agencyQueueIdMap.get(agencyQueueNameMap.get(agencyName)));
        }
        
        return mapLeadGaAdminQueueIds;
    }
    
}

1 Answer 1

0
public static void shareMEUEnrollmentWithUsers(List<ID> recordId) {

Is asking for a List<Id>, but:

    shareMEUEnrollment.shareMEUEnrollmentWithUsers(trigger.new);

Is giving it a List<MEU_Enrollment__c>.

You'll need to change your trigger code:

    shareMEUEnrollment.shareMEUEnrollmentWithUsers(new List<Id>(trigger.newMap.keySet()));

Or your handler:

public static void shareMEUEnrollmentWithUsers(sObject[] records) {

Which would require significant refactoring.

It's important to note that before insert is a waste of time, as there are no record Ids (the records aren't inserted yet), and you're probably wasting a lot of CPU time using both before and after triggers. Just use the after triggers.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .