code: def add_xmlid(ctx, record, xmlid, noupdate=False):
      
      try:
          ref_id, __, __ = ctx.env['ir.model.data'].xmlid_lookup(xmlid)
      except ValueError:
          pass  # does not exist, we'll create a new one
      else:
          return ctx.env['ir.model.data'].browse(ref_id)
      if '.' in xmlid:
          module, name = xmlid.split('.')
      else:
          module = ''
          name = xmlid
      return ctx.env['ir.model.data'].create({
          'name': name,
          'module': module,
          'model': record._name,
          'res_id': record.id,
          'noupdate': noupdate,
      })
docs: Add a XMLID on an existing record