{"record":{"id":"1aee4ce359dfa8f0","repo":"SonarSource/sonarqube","slug":"entity-not-found-1aee4c","errorCode":null,"errorMessage":"Entity not found","messagePattern":"Entity not found","errorType":"http","errorClass":"NotFoundException","httpStatus":404,"severity":"error","filePath":"server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java","lineNumber":119,"sourceCode":"      PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(\n        request.getTemplateId(), request.getTemplateName()));\n\n      ProjectWsRef.validateUuidAndKeyPair(request.getProjectId(), request.getProjectKey());\n      EntityDto entityDto = getEntityByKeyOrUuid(request.getProjectId(), request.getProjectKey(), dbSession);\n\n      if (entityDto.isProject()) {\n        managedInstanceChecker.throwIfProjectIsManaged(dbSession, entityDto.getUuid());\n      }\n      permissionTemplateService.applyAndCommit(dbSession, template, Collections.singletonList(entityDto));\n    }\n  }\n\n  private EntityDto getEntityByKeyOrUuid(@Nullable String uuid, @Nullable String key, DbSession dbSession) {\n    Optional<EntityDto> entityDto = uuid != null ? dbClient.entityDao().selectByUuid(dbSession, uuid) : dbClient.entityDao().selectByKey(dbSession, key);\n    if (entityDto.isPresent() && !ComponentQualifiers.SUBVIEW.equals(entityDto.get().getQualifier())) {\n      return entityDto.get();\n    } else {\n      throw new NotFoundException(\"Entity not found\");\n    }\n  }\n\n  private static class ApplyTemplateRequest {\n    private String projectId;\n    private String projectKey;\n    private String templateId;\n    private String templateName;\n\n    @CheckForNull\n    public String getProjectId() {\n      return projectId;\n    }\n\n    public ApplyTemplateRequest setProjectId(@Nullable String projectId) {\n      this.projectId = projectId;\n      return this;\n    }","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java#L101-L137","documentation":"ApplyTemplateAction.getEntityByKeyOrUuid looks up the entity a permission template is applied to by uuid or key and throws NotFoundException when the entity is missing or is a SUBVIEW. This guards api/permissions/apply_template against applying templates to nonexistent or invalid entities.","triggerScenarios":"POST api/permissions/apply_template with projectKey/projectId that does not exist, was deleted, or resolves to a sub-view; calling with neither uuid nor key can also produce failures downstream.","commonSituations":"Automation applying permission templates right after project creation with a mismatched key; using a project id from a different SonarQube instance; targeting portfolio sub-views.","solutions":["Confirm the project key with GET api/projects/search before calling apply_template","Use the correct parameter: projectKey for keys, projectId for uuids","Recreate or re-key the reference if the project was deleted; templates must be applied to existing entities","Target the top-level view instead of a sub-view qualifier"],"exampleFix":"// before\nawait applyTemplate({ projectKey: 'typo_key', templateId: 't1' });\n\n// after\nconst p = await client.projects.search({ q: 'real_key' });\nif (p.components.length) await applyTemplate({ projectKey: p.components[0].key, templateId: 't1' });","handlingStrategy":"validation","validationCode":"const found = (await client.projects.search({ q: projectKey })).components.some(c => c.key === projectKey);\nif (!found) throw new Error(`project ${projectKey} does not exist`);","typeGuard":"function hasApplyTemplateArgs(args) { return Boolean(args.templateId) && (Boolean(args.projectKey) !== Boolean(args.projectId)); }","tryCatchPattern":"try { await applyTemplate({ projectKey, templateId }); } catch (e) { if (e.status === 404) { /* re-lookup key or skip missing project */ } else { throw e; } }","preventionTips":["Confirm project existence right before applying templates","Use projectKey (not projectId) unless you have the real uuid","Exclude sub-view components when enumerating targets from portfolios"],"tags":["webapi","permissions","permission-templates","not-found"],"backgroundTag":"entity-not-found","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}