Savelyev_Vyacheslav

set sys_translation

Oct 11th, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // const packID = '156950344216170038' // ITSM
  2. const packID = '155931135900000002' // SIMPL
  3.  
  4. const obj =  {
  5.     "172778902704801735": {
  6.         "ru": "Инциденты в разрезе причин",
  7.         "en": "Incidents by cause"
  8.     },
  9.     "172758840504375611": {
  10.         "ru": "Портал",
  11.         "en": "Portal"
  12.     },
  13.     "172758832900858846": {
  14.         "ru": "Портал",
  15.         "en": "Portal"
  16.     },
  17.     "172709497603771004": {
  18.         "ru": "Все жалобы",
  19.         "en": "All complaints"
  20.     }
  21. }
  22.  
  23.  
  24. function iterateKeys(obj) {
  25.     for (let key in obj) {
  26.         const column_id = getColumnId(key)
  27.         let rec = getRecord(key, '156628684306200767');  // ru
  28.         if (rec && rec.sys_id) {
  29.             rec.setValue('value', obj[key].ru);
  30.             rec.update();
  31.         } else {
  32.             let newRec = new SimpleRecord('sys_translation');  // создаем новый объект
  33.             newRec.setValue('record_id', key);
  34.             newRec.setValue('language_id', '156628684306200767');  // ru
  35.             newRec.setValue('column_id', column_id);
  36.             newRec.setValue('value', obj[key].ru);
  37.             newRec.insert();
  38.         }
  39.  
  40.         rec = getRecord(key, '156628684306541141');  // en
  41.         if (rec && rec.sys_id) {
  42.             rec.setValue('value', obj[key].en);
  43.             rec.update();
  44.         } else {
  45.             let newRec = new SimpleRecord('sys_translation');  // создаем новый объект
  46.             newRec.setValue('record_id', key);
  47.             newRec.setValue('language_id', '156628684306541141');  // en
  48.             newRec.setValue('column_id', column_id);
  49.             newRec.setValue('value', obj[key].en);
  50.             newRec.insert();
  51.         }
  52.     }
  53. }
  54.  
  55. // Пример использования функции:
  56. iterateKeys(obj);
  57.  
  58. function getRecord(key, langID) {
  59.     const record = new SimpleRecord('sys_translation');
  60.     record.addQuery('record_id', key);
  61.     record.addQuery('language_id', langID);
  62.     record.addQuery('application_id', packID);
  63.  
  64.     record.orderByDesc('record_id');
  65.     record.setLimit(1);
  66.     record.query();
  67.    
  68.     if (record.next()) return record;
  69.     return null;  // возвращаем null, если запись не найдена
  70. }
  71.  
  72. function getColumnId(key) {
  73.     const record = new SimpleRecord('sys_translation');
  74.     record.addQuery('record_id', key);
  75.     record.orderByDesc('record_id');
  76.     record.setLimit(1);
  77.     record.query();
  78.    
  79.     if (record.next()) return record.getValue('column_id')
  80.     return null;  // возвращаем null, если запись не найдена
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment