{"record":{"id":"35ef89027b47c370","repo":"SonarSource/sonarqube","slug":"failed-to-set-the-new-code-definition-the-given-v-35ef89","errorCode":null,"errorMessage":"Failed to set the New Code Definition. The given value is not compatible with the Clean as You Code methodology. Please refer to the documentation for compliant options.","messagePattern":"Failed to set the New Code Definition\\. The given value is not compatible with the Clean as You Code methodology\\. Please refer to the documentation for compliant options\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":400,"severity":"error","filePath":"server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java","lineNumber":178,"sourceCode":"\n        if (branchKey != null) {\n          branch = getBranch(dbSession, project, branchKey);\n          dto.setBranchUuid(branch.getUuid());\n        } else if (isCommunityEdition) {\n          // in CE set main branch value instead of project value\n          branch = getMainBranch(dbSession, project);\n          dto.setBranchUuid(branch.getUuid());\n        }\n\n        dto.setProjectUuid(project.getUuid());\n      } else {\n        userSession.checkIsSystemAdministrator();\n      }\n\n      setValue(dbSession, dto, type, project, branch, valueStr);\n\n      if (!CaycUtils.isNewCodePeriodCompliant(dto.getType(), dto.getValue())) {\n        throw new IllegalArgumentException(\"Failed to set the New Code Definition. The given value is not compatible with the Clean as \" +\n          \"You Code methodology. Please refer to the documentation for compliant options.\");\n      }\n\n      newCodePeriodDao.upsert(dbSession, dto);\n      dbSession.commit();\n    }\n  }\n\n  private void setValue(DbSession dbSession, NewCodePeriodDto dto, NewCodePeriodType type, @Nullable ProjectDto project,\n    @Nullable BranchDto branch, @Nullable String value) {\n    switch (type) {\n      case PREVIOUS_VERSION -> Preconditions.checkArgument(value == null, \"Unexpected value for type '%s'\", type);\n      case NUMBER_OF_DAYS -> {\n        requireValue(type, value);\n        dto.setValue(parseDays(value));\n      }\n      case SPECIFIC_ANALYSIS -> {\n        checkValuesForSpecificBranch(type, value, project, branch);","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java#L160-L196","documentation":"After writing the requested New Code Period value into the DTO, SetAction verifies it against CaycUtils.isNewCodePeriodCompliant and throws IllegalArgumentException if the combination violates the Clean as You Code methodology (e.g. certain date/reference-branch settings that are not CaYC-compliant). The upsert is aborted before commit, so the invalid definition is never persisted.","triggerScenarios":"Calling api/new_code_periods/set with a type/value pair that parses successfully but fails the CaYC compliance check — e.g. a SPECIFIC_ANALYSIS date for the overall setting or a reference-branch configuration disallowed in that scope/edition.","commonSituations":"Migrating legacy 'days' or fixed-date definitions into newer SonarQube versions that enforce CaYC; administrators trying to set an overall definition to a type only valid on branches; edition downgrades changing which types are compliant.","solutions":["Choose a CaYC-compliant option per the linked documentation, typically PREVIOUS_VERSION or NUMBER_OF_DAYS for the project/overall scope.","If a legacy date-based definition is needed, set it at branch level only where permitted.","Check CaycUtils/edition constraints to confirm which types are valid for your scope (overall vs project vs branch)."],"exampleFix":"// before\ntype=SPECIFIC_ANALYSIS&value=2023-01-01\n// after\ntype=PREVIOUS_VERSION","handlingStrategy":"validation","validationCode":"const CAYC_COMPLIANT = ['PREVIOUS_VERSION', 'NUMBER_OF_DAYS'];\nif (!CAYC_COMPLIANT.includes(params.type)) throw new Error('Type not CaYC-compliant: ' + params.type);","typeGuard":null,"tryCatchPattern":"try { ... } catch (e) { if (String(e.message).includes('Clean as You Code')) { log('Non-compliant New Code Definition rejected; use PREVIOUS_VERSION'); } throw e; }","preventionTips":["Default to PREVIOUS_VERSION unless a compliant alternative is explicitly required.","Review the Clean as You Code docs when migrating legacy date-based definitions."],"tags":["webapi","new-code-period","cayc","validation","sonarqube"],"backgroundTag":"invalid-config-value","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}