0

I keep getting this error on my Apex trigger while trying to deploy it to the org:

Method does not exist or incorrect signature: void get(Id) from the type List Case.

this is my code for trigger:

public static void handleAfterUpdate(List<Case> triggerNew, List<Case> triggerOld){
        System.debug('Case trigger handler');


        for (Case newCase : triggerNew){
            
            System.debug(newCase);
            if (triggerNew.get(newCase.id).Email_Sent__c == 'true' && triggerOld.get(newCase.id).Email_Sent__c == 'false') {
                
                Map<String, String> notificationBody = new Map<String, String>();

                Mobile_Device_Notification_Token__c mobileDeviceNotificationToken = [SELECT Token__c, Account__c 
                                                                                    FROM Mobile_Device_Notification_Token__c 
                                                                                    WHERE Account__c =: newCase.AccountId];

                notificationBody.put('CaseId', newCase.id);
                notificationBody.put('Case Number', newCase.CaseNumber);
                notificationBody.put('Case Status', newCase.status);
                notificationBody.put('AccountId', newCase.AccountId);
                notificationBody.put('Subject', newCase.subject);
                notificationBody.put('Token', mobileDeviceNotificationToken.Token__c);

                string jsonString = JSON.serialize(notificationBody);

                System.debug(notificationBody);
                sendNotification(jsonString);
            }
        }
    }

2 Answers 2

0

In your code triggerNew is a List. But inside the for loop you are get as if it is a map triggerNew.get(newCase.id).Email_Sent__c . Try to change the code in that if condition as below and check

public static void handleAfterUpdate(List<Case> triggerNew, map<id,Case> triggerOld){
    System.debug('Case trigger handler');


    for (Case newCase : triggerNew){
        
        System.debug(newCase);
        if (newCase.Email_Sent__c == true && triggerOld.get(newCase.id).Email_Sent__c == false) {
            
            Map<String, String> notificationBody = new Map<String, String>();

            Mobile_Device_Notification_Token__c mobileDeviceNotificationToken = [SELECT Token__c, Account__c 
                                                                                FROM Mobile_Device_Notification_Token__c 
                                                                                WHERE Account__c =: newCase.AccountId];

            notificationBody.put('CaseId', newCase.id);
            notificationBody.put('Case Number', newCase.CaseNumber);
            notificationBody.put('Case Status', newCase.status);
            notificationBody.put('AccountId', newCase.AccountId);
            notificationBody.put('Subject', newCase.subject);
            notificationBody.put('Token', mobileDeviceNotificationToken.Token__c);

            string jsonString = JSON.serialize(notificationBody);

            System.debug(notificationBody);
            sendNotification(jsonString);
        }
    }
}

One more important thing is you are writing the SOQL query inside For Loop. This is not a good practice. You need to use collections like map or set and write the SOQL query outside the FOR loop

Example of how to handle SOQL outside For loop

1
  • @Karolis Belozorovas, I have updated the entire code Can you check and confirm if any errors Commented Jan 31, 2022 at 10:35
0

triggerNew is a List

So triggerNew.get() will only accept index. Like 0,1, 2, 3 ...

Use newCase directly like this. And pass triggerOld map and not List

if (newCase.Email_Sent__c == 'true' && triggerOldMap.get(newCase.id).Email_Sent__c == 'false') {

Also dont use SOQL in for loop.

[SELECT Token__c, Account__c
    FROM Mobile_Device_Notification_Token__c
    WHERE Account__c =: newCase.AccountId]

You will end up hitting governor limits. You will have to create a map of Map<Id,Mobile_Device_Notification_Token__c> where Id is account Id and use this map.

Something like this:

Set<Id> uniqueAccounts = new Set<Id>();
for (Case newCase : triggerNew){
    uniqueAccounts.add(newCase.AccountId);
}
Map<Id,Mobile_Device_Notification_Token__c> deviceNotificationTokenMap = new Map<Id, Mobile_Device_Notification_Token__c>();
if(!uniqueAccounts.isEmpty()){
    for(Mobile_Device_Notification_Token__c deviceNotificationToken : [SELECT Token__c, Account__c FROM Mobile_Device_Notification_Token__c WHERE Account__c = :uniqueAccounts]) {

        deviceNotificationTokenMap.put(deviceNotificationToken.Account__c, deviceNotificationToken);
    }
}

for (Case newCase : triggerNew){
    
    if (newCase.Email_Sent__c == 'true' && triggerOldMap.get(newCase.id).Email_Sent__c == 'false') {

        Map<String, String> notificationBody = new Map<String, String>();
        Mobile_Device_Notification_Token__c mobileDeviceNotificationToken = deviceNotificationTokenMap.get(newCase.AccountId);
        notificationBody.put('CaseId', newCase.id);
        notificationBody.put('Case Number', newCase.CaseNumber);
        notificationBody.put('Case Status', newCase.status);
        notificationBody.put('AccountId', newCase.AccountId);
        notificationBody.put('Subject', newCase.subject);
        if(mobileDeviceNotificationToken != null) {
            notificationBody.put('Token', mobileDeviceNotificationToken.Token__c);
        }

        string jsonString = JSON.serialize(notificationBody);

        System.debug(notificationBody);
        sendNotification(jsonString);
    }
}
4
  • I have updated if statement as you suggested, but got these errors: Comparison arguments must be compatible types: Boolean, String; Method does not exist or incorrect signature: void get(Id) from the type List<Case>;
    – Karol
    Commented Jan 31, 2022 at 10:13
  • Email_Sent__c is a boolean field I guess. So you have to remove Quotations (newCase.Email_Sent__c == true) Commented Jan 31, 2022 at 10:18
  • it solved one error, but still getting this one: Method does not exist or incorrect signature: void get(Id) from the type List<Case>
    – Karol
    Commented Jan 31, 2022 at 10:25
  • Updated my answer, you will have to pass that as triggerOld as map and not as list. Commented Jan 31, 2022 at 10:29

You must log in to answer this question.

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