{"record":{"id":"f719ec27f35f9ad7","repo":"odoo/odoo","slug":"could-not-compute-any-code-for-the-copy-automatica","errorCode":null,"errorMessage":"Could not compute any code for the copy automatically. Please create it manually.","messagePattern":"Could not compute any code for the copy automatically\\. Please create it manually\\.","errorType":"exception","errorClass":"UserError","httpStatus":null,"severity":"warning","filePath":"addons/account/models/account_journal.py","lineNumber":761,"sourceCode":"            )[0][0])\n            for company_id, _ in groupby(vals_list, lambda v: v['company_id'])\n        }\n        for journal, vals in zip(self, vals_list):\n            # Find a unique code for the copied journal\n            all_journal_codes = code_by_company_id[vals['company_id']]\n\n            copy_code = vals['code']\n            code_prefix = re.sub(r'\\d+', '', copy_code).strip()\n            counter = 1\n            while counter <= len(all_journal_codes) and copy_code in all_journal_codes:\n                counter_str = str(counter)\n                copy_prefix = code_prefix[:journal._fields['code'].size - len(counter_str)]\n                copy_code = \"%s%s\" % (copy_prefix, counter_str)\n                counter += 1\n\n            if counter > len(all_journal_codes):\n                # Should never happen, but put there just in case.\n                raise UserError(_(\"Could not compute any code for the copy automatically. Please create it manually.\"))\n\n            vals.update(\n                code=copy_code,\n                name=_(\"%s (copy)\", journal.name or ''))\n        return vals_list\n\n    def write(self, vals):\n        # for journals, force a readable name instead of a sanitized name e.g. non ascii in journal names\n        if vals.get('alias_name') and 'type' not in vals:\n            # will raise if writing name on more than 1 record, using self[0] is safe\n            if (not self.env['mail.alias']._is_encodable(vals['alias_name']) or\n                not self.env['mail.alias']._sanitize_alias_name(vals['alias_name'])):\n                vals['alias_name'] = self._alias_prepare_alias_name(\n                    False, vals.get('name', self.name), vals.get('code', self.code), self[0].type, self[0].company_id)\n\n        for journal in self:\n            company = journal.company_id\n            if ('company_id' in vals and journal.company_id.id != vals['company_id']):","sourceCodeStart":743,"sourceCodeEnd":779,"githubUrl":"https://github.com/odoo/odoo/blob/1e661df964b1b264c9cef3ab28430d4785be3fda/addons/account/models/account_journal.py#L743-L779","documentation":"In the create/copy plumbing (_copy_data / create override around line 761), when duplicating a journal Odoo derives a new short code by stripping digits from the original code and appending a counter while the candidate collides with existing codes. If the counter exceeds the number of existing codes without finding a free slot (bounded by the code field's size), it raises this UserError as a safety net.","triggerScenarios":"Calling journal.copy() when the generated code candidates all collide with all_journal_codes; practically only reachable when the code column's size is too small to fit prefix+counter or when the loop guard is hit, e.g. many single-char codes exhausting combinations.","commonSituations":"Mass-duplicating journals with long alphabetic prefixes so the truncated copy_prefix plus counter cannot form a unique code; databases with many similarly coded journals.","solutions":["Create the copy manually with an explicit free 'code' value instead of relying on copy().","Shorten the source journal's code prefix so a counter suffix fits within the field size.","Free up codes by renaming/archiving journals that block the generated sequence."],"exampleFix":"# before\nnew_journal = journal.copy()  # may raise when codes are exhausted\n\n# after\nnew_journal = journal.copy({'code': 'BNK9', 'name': '%s (copy)' % journal.name})","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"from odoo.exceptions import UserError\ntry:\n    new_journal = journal.copy()\nexcept UserError:\n    new_journal = journal.copy({'code': free_code, 'name': f\"{journal.name} (copy)\"})","preventionTips":["Pass an explicit 'code' in copy() defaults instead of relying on auto-generation.","Keep journal code prefixes short so counter suffixes fit the field size.","Treat this as a rare guard: prefer deterministic codes in scripts."],"tags":["odoo","accounting","journal","copy","code-generation"],"backgroundTag":null,"analyzedSha":"1e661df964b1b264c9cef3ab28430d4785be3fda","analyzedAt":"2026-08-15T05:22:16.142Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}