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
Simina RomanSimina Roman 

Edit Task Button Crashing with List has no rows for assignment to SObject

We have an Apex class that we use to edit tasks for Accounts, Opportunities, Contacts and a custom object called Watch List.  We need to ensure the Edit button does crash when  we click on it and the  task is associated to a Lead.

Here is the top part of the Apex class:


public class TaskEditCtrl {
    @AuraEnabled
    public static TaskWrapper getInfo(String taskId) {
        
        List<Task> tList = [
                SELECT
                        OwnerId, RecordType.Name, WhoId, WhatId, Subject, Status, Priority,
                        Send_notification_lightning__c,Is_Important__c, Type, Category__c,
                        Description,CreatedBy.Name,LastModifiedBy.Name,CreatedDate,LastModifiedDate,
                        Download_Link__c,Download__c,CreatedById,LastModifiedById, ReminderDateTime,
                        ActivityDate, RecurrenceActivityId, RecurrenceType, RecurrenceInterval, RecurrenceInstance ,
                        RecurrenceDayOfWeekMask, RecurrenceDayOfMonth , RecurrenceMonthOfYear, IsRecurrence,
                        RecurrenceStartDateOnly, RecurrenceEndDateOnly
                FROM
                        Task
                WHERE
                        id = :taskId
        ];
            if(tList.size() > 0) {
                TaskWrapper cls = new TaskWrapper();
                tList[0].Send_notification_lightning__c  = true;
                cls.taskRec = tList[0];
                Contact con = new Contact();
                if(tList[0].WhoId != null) {
                    con = [SELECT Name, Email FROM Contact WHERE id = :tList[0].WhoId LIMIT 1];
                }
                cls.who = con;
                
 
ShirishaShirisha (Salesforce Developers) 
Hi Simina,

Greetings!

This error occurs whenever you try to query the records to store it in the list which might return 0 records and if you try to use the List.

I would suggest you to check the size of the List first and then you need to use the list as below;
 
Player__c[] players = [SELECT Id from Player__c where Name = :username];
if (players.size() > 0)
p = players[0].Id;
I can see that you have 2 SOQL queries and you need to figure out which one is causing the issue by enabling the debug logs:

Reference:https://help.salesforce.com/apex/HTViewHelpDoc?id=code_add_users_debug_log.htm&language=en_us

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri