nopSolutions/nopCommerce · error · ArgumentException

Schedule task cannot be loaded

Error message

Schedule task cannot be loaded

What it means

Thrown by TaskUpdate (POST, MANAGE_SCHEDULE_TASKS) on ScheduleTaskController when GetTaskByIdAsync(model.Id) returns null. The inline edit on the schedule-tasks grid posts a ScheduleTaskModel; if no task matches model.Id the action cannot proceed and throws. The exception message 'Schedule task cannot be loaded' is generic (no nameof).

Source

Thrown at src/Presentation/Nop.Web/Areas/Admin/Controllers/ScheduleTaskController.cs:83

    }

    [HttpPost]
    [CheckPermission(StandardPermission.System.MANAGE_SCHEDULE_TASKS)]
    public virtual async Task<IActionResult> List(ScheduleTaskSearchModel searchModel)
    {
        //prepare model
        var model = await _scheduleTaskModelFactory.PrepareScheduleTaskListModelAsync(searchModel);

        return Json(model);
    }

    [HttpPost]
    [CheckPermission(StandardPermission.System.MANAGE_SCHEDULE_TASKS)]
    public virtual async Task<IActionResult> TaskUpdate(ScheduleTaskModel model)
    {
        //try to get a schedule task with the specified id
        var scheduleTask = await _scheduleTaskService.GetTaskByIdAsync(model.Id)
            ?? throw new ArgumentException("Schedule task cannot be loaded");

        //To prevent inject the XSS payload in Schedule tasks ('Name' field), we must disable editing this field, 
        //but since it is required, we need to get its value before updating the entity.
        if (!string.IsNullOrEmpty(scheduleTask.Name))
        {
            model.Name = scheduleTask.Name;
            ModelState.Remove(nameof(model.Name));
        }

        if (!ModelState.IsValid)
            return ErrorJson(ModelState.SerializeErrors());

        if (!scheduleTask.Enabled && model.Enabled)
            scheduleTask.LastEnabledUtc = DateTime.UtcNow;

        scheduleTask = model.ToEntity(scheduleTask);

        await _scheduleTaskService.UpdateTaskAsync(scheduleTask);

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Refresh the schedule-tasks grid and edit a current row.
  2. Confirm the task still exists after plugin install/uninstall cycles.
  3. Make the update idempotent or return ErrorJson when the task is missing.
  4. Validate model.Id > 0 and present before saving edits.

Example fix

// before
var scheduleTask = await _scheduleTaskService.GetTaskByIdAsync(model.Id)
    ?? throw new ArgumentException("Schedule task cannot be loaded");

// after
var scheduleTask = await _scheduleTaskService.GetTaskByIdAsync(model.Id);
if (scheduleTask == null)
    return ErrorJson("Schedule task no longer exists; refresh the list.");
Defensive patterns

Strategy: validation

Validate before calling

if (model.Id <= 0) return ErrorJson("Invalid task id.");
var task = await _scheduleTaskService.GetTaskByIdAsync(model.Id);
if (task == null) return ErrorJson("Task no longer exists.");

Type guard

// N/A

Try / catch

catch (ArgumentException ex) when (ex.Message.Contains("Schedule task cannot be loaded"))
{ return ErrorJson(ex.Message); }

Prevention

When it happens

Trigger: Inline-editing a schedule-task row whose underlying task was removed; tampered model.Id; tasks cleared by an uninstall that removed plugin-registered tasks; restored DB without that task.

Common situations: Plugin uninstall that deleted its schedule tasks while the grid was open; fixtures; DB restore; concurrent admin edits to the tasks grid.

Related errors


AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13). Data as JSON: /api/errors/814113dc71093e5d. Report an issue: GitHub.