-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathGlideRecordImpl.js
More file actions
1009 lines (849 loc) · 26.8 KB
/
GlideRecordImpl.js
File metadata and controls
1009 lines (849 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This Script Include serves as a comprehensive facade around ServiceNow's
* out-of-the-box `GlideRecord` and `GlideRecordSecure` classes, providing enhanced
* functionality, improved error handling and additional safety mechanisms. It can
* be used as a base class for your own data access and business objects.
*
* **Please note:**
* - Please bear in mind that this class is only a starting point and may need to be
* extended or adapted by you.
* - The source code incorporates language elements from the ECMAScript 2021 engine,
* like `class`, `const` or string literals.
* - Only `GlideRecord` methods related to single-record access are wrapped. Bulk or
* list-based operations are intentionally excluded, as such scenarios are better
* addressed using a different design pattern, for example, the "Repository Pattern".
*
* @author Maik Skoddow (https://www.linkedin.com/in/maik-skoddow)
* @version 2.0.0
* @see {@link https://www.linkedin.com/pulse/servicenow-deployment-pipeline-part-5-programming-worth-skoddow-huxee/}
*/
class GlideRecordImpl {
/**
* This constructor is invoked to instantiate new objects from that class.
*
* There are three different ways of constructing a new object, and for the
* last two approaches, an additional Boolean parameter can be used to
* specify whether a `GlideRecordSecure` object should be used
* instead of a `GlideRecord` object.
* @param {...(GlideRecord|GlideRecordSecure|string|boolean)} args
* Constructor arguments (see above)
*
* @throws {TypeError}
* If the signature is not supported or arguments are invalid.
*
* @example
* // (1) with an existing `GlideRecord` instance (e.g. in a Business Rule)
* var objIncident = new GlideRecordImpl(current);
*
* // (2.1) only with a table name (creates a new record that can be inserted)
* var objIncident = new GlideRecordImpl('incident');
*
* // (2.2) Just like (2.1) but with a `GlideRecordSecure` object
* var objIncident = new GlideRecordImpl('incident', true);
*
* // (3.1) with table name and Sys ID (retrieves an existing record)
* var objIncident = new GlideRecordImpl('incident', 'b3af7471c31a6a90108c78edd40131aa');
*
* // (3.2) Just like (3.1) but with a `GlideRecordSecure` object
* var objIncident = new GlideRecordImpl('incident', 'b3af7471c31a6a90108c78edd40131aa', true);
*/
constructor(
...args
) {
const OVERLOAD_REGISTRY =
new Map([
['object', (...args) => this._newWithGlideRecord(...args)],
['string', (...args) => this._newWithTable(...args)],
['string,boolean', (...args) => this._newWithTable(...args)],
['string,string', (...args) => this._newWithTableAndSysID(...args)],
['string,string,boolean', (...args) => this._newWithTableAndSysID(...args)],
]);
//build the constructor signature
let _strSignature =
args.map(arg =>
Array.isArray(arg) ? 'array' : typeof arg
).join(',');
//is a constructor method registered for the signature?
if (OVERLOAD_REGISTRY.has(_strSignature)) {
//invoke the registered constructor method
OVERLOAD_REGISTRY.get(_strSignature)(...args);
}
else {
throw new TypeError(
`[${this.constructor.name}.constructor] ` +
`Unsupported signature "${_strSignature}"`
);
}
}
/**
* @private
* Initializes with an existing `GlideRecord` or `GlideRecordSecure` instance.
*
* @param {GlideRecord|GlideRecordSecure|null} objRecord
*
* @throws {TypeError}
* If objRecord is not a valid `GlideRecord` or `GlideRecordSecure` instance.
*/
_newWithGlideRecord(
objRecord = null,
) {
if ((objRecord instanceof GlideRecord || objRecord instanceof GlideRecordSecure)
&&
(objRecord.isValidRecord() === true)
) {
this._grRecordInstance = objRecord;
}
else {
throw new TypeError(
`[${this.constructor.name}.constructor] Passed object ` +
`does not represent a valid GlideRecord instance!`
);
}
}
/**
* @private
* Initializes a new `GlideRecord` or `GlideRecordSecure` record based
* on the given table name.
*
* @param {string} strTableName
* A valid table name for that instance.
*
* @param {boolean} [isSecure=false]
* If `true` a `GlideRecordSecure` object is instantiated, otherwise
* a `GlideRecord` object.
*
* @throws {TypeError}
* If table name is invalid.
*/
_newWithTable(
strTableName = 'x',
isSecure = false,
) {
if (!gs.tableExists(strTableName)) {
throw new TypeError(
`[${this.constructor.name}.constructor] "${strTableName}" ` +
`does not represent a valid table name for that instance!`
);
}
this._wasNewRecord = true;
this._grRecordInstance =
isSecure ?
new GlideRecordSecure(strTableName) :
new GlideRecord(strTableName);
this._grRecordInstance.newRecord();
}
/**
* @private
* Retrieves an existing `GlideRecord` or `GlideRecordSecure` record based
* on the given table name and Sys ID.
*
* @param {string} strTableName
* A valid table name for that instance.
*
* @param {string} strSysID
* A valid ServiceNow Sys ID.
*
* @param {boolean} [isSecure=false]
* If `true` a `GlideRecordSecure` object is instantiated, otherwise
* a `GlideRecord` object.
*
* @throws {TypeError}
* If table name or Sys ID is invalid.
*/
_newWithTableAndSysID(
strTableName = 'x',
strSysID = 'x',
isSecure = false,
) {
if (!gs.tableExists(strTableName)) {
throw new TypeError(
`[${this.constructor.name}.constructor] "${strTableName}" ` +
`does not represent a valid table name for that instance!`
);
}
if (!GlideStringUtil.isEligibleSysID(strSysID)) {
throw new TypeError(
`[${this.constructor.name}.constructor] "${strSysID}" ` +
`does not represent a valid Sys ID!`
);
}
this._grRecordInstance =
isSecure ?
new GlideRecordSecure(strTableName) :
new GlideRecord(strTableName);
this._grRecordInstance.get(strSysID);
}
//-------------------------------------------------------------------------------------------
// public Methods
//-------------------------------------------------------------------------------------------
/**
* Provide a human-readable string representation of the created object,
* typically for debugging, logging, or display purposes.
*
* @returns {string}
* Some technical information about the `GlideRecord` instance that this object is
* the facade for.
*/
toString(
) {
return '' +
`Facade for a GlideRecord instance:\n` +
`Instantiated class: "${this.type}"\n` +
`Record table: ${this.getTableName()} (${this.getLabel()})\n` +
`Record Sys ID: ${this.getSysID()}\n` +
`Record is new: ${this.isNewRecord()}\n` +
`Record is deleted: ${this.isDeletedRecord()}\n`;
}
/**
* Returns the internally stored reference to the
* `GlideRecord` or `GlideRecordSecure` instance.
*
* @returns {GlideRecord|GlideRecordSecure}
* Reference to the ServiceNow `GlideRecord` or `GlideRecordSecure`
* instance the object was initialized for.
*/
getGlideRecord(
) {
return this._grRecordInstance;
}
/**
* Determines if the referenced `GlideRecord` instance is valid.
*
* @returns {boolean}
* `true` if the referenced `GlideRecord` instance is valid, otherwise `false`.
*/
isValidRecord(
) {
return true &&
this._isDeleted !== true &&
this.getGlideRecord().isValidRecord();
}
/**
* Determines if the specified field is defined in the table the
* internally referenced `GlideRecord` or `GlideRecordSecure` instance was
* intialized for.
*
* @param {string} strFieldName
* Name of the field to check.
*
* @returns {boolean}
* `true` if the field is defined in the underlying table, otherwise `false`.
*/
isValidField(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.isValidField] `;
try {
this._testFieldName(METHOD_NAME, strFieldName);
return true;
}
catch (e) {
return false;
}
}
/**
* Determines if the underlying record has been inserted into the database or not.
*
* @returns {boolean}
* `true` if the underlying record was not already inserted
* into the database, otherwise `false`.
*/
isNewRecord(
) {
return this.getGlideRecord().isNewRecord();
}
/**
* Determines if the underlying record was deleted.
*
* @returns {boolean}
* `true` if record was deleted, otherwise `false`.
*/
isDeletedRecord(
) {
return this._isDeleted === true;
}
/**
* Determines if the access control rules permit reading records in this table
* or - if specified - in the associated field.
*
* @param [string] strFieldName
* If specified `strFieldName` must represent a column in the underlying table.
* @returns {boolean}
* `true` if reading records or values in the specified field is
* permitted, otherwise `false`.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
canRead(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.canRead] `;
this._testIsDeleted(METHOD_NAME);
if (arguments.length === 0) {
return this.getGlideRecord().canRead();
}
this._testFieldName(METHOD_NAME, strFieldName);
return this.getGlideRecord().getElement(strFieldName).canRead();
}
/**
* Determines if the access control rules permit updating records in this table
* or - if specified - in the associated field.
*
* @param [string] strFieldName
* If specified `strFieldName` must represent a column in the underlying table.
* @returns {boolean}
* `true` if updating records or values in the specified field is
* permitted, otherwise `false`.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
canWrite(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.canWrite] `;
this._testIsDeleted(METHOD_NAME);
if (arguments.length === 0) {
return this.getGlideRecord().canWrite();
}
this._testFieldName(METHOD_NAME, strFieldName);
return this.getGlideRecord().getElement(strFieldName).canWrite();
}
/**
* Determines if the access control rules permit creating records in this table
* or - if specified - in the associated field.
*
* @param [string] strFieldName
* If specified `strFieldName` must represent a column in the underlying table.
* @returns {boolean}
* `true` if creating new records or values in the specified field is
* permitted, otherwise `false`.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
canCreate(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.canCreate] `;
this._testIsDeleted(METHOD_NAME);
if (arguments.length === 0) {
return this.getGlideRecord().canCreate();
}
this._testFieldName(METHOD_NAME, strFieldName);
return this.getGlideRecord().getElement(strFieldName).canCreate();
}
/**
* Determines if the access control rules permit deleting records in this table
* or - if specified - in the associated field.
*
* @param [string] strFieldName
* If specified `strFieldName` must represent a column in the underlying table.
* @returns {boolean}
* `true` if deleting records or values in the specified field is
* permitted, otherwise `false`.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
canDelete(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.canDelete] `;
this._testIsDeleted(METHOD_NAME);
if (arguments.length === 0) {
return this.getGlideRecord().canDelete();
}
this._testFieldName(METHOD_NAME, strFieldName);
return this.getGlideRecord().getElement(strFieldName).canDelete();
}
/**
* Retrieves the name of the underlying table that the `GlideRecord` was initialized for.
*
* @returns {string}
* Name of the underlying table that the `GlideRecord` was initialized for.
*
* @throws {Error}
* If the underlying record already was deleted before.
*/
getTableName(
) {
const METHOD_NAME = `[${this.constructor.name}.getTableName] `;
this._testIsDeleted(METHOD_NAME);
return this.getGlideRecord().getTableName();
}
/**
* Gets the Sys ID of the record.
*
* @returns {string}
* Sys ID of the record.
*
* @throws {Error}
* If the underlying record already was deleted before.
*/
getSysID(
) {
const METHOD_NAME = `[${this.constructor.name}.getSysID] `;
this._testIsDeleted(METHOD_NAME);
return this.getGlideRecord().getValue('sys_id');
}
/**
* Gets the primary key of the record, which is usually the Sys ID unless otherwise specified.
*
* @returns {string}
* Primary key of the record, which is usually the `sys_id` unless otherwise specified.
*
* @throws {Error}
* If the underlying record already was deleted before.
*/
getUniqueValue(
) {
const METHOD_NAME = `[${this.constructor.name}.getUniqueValue] `;
this._testIsDeleted(METHOD_NAME);
return this.getGlideRecord().getUniqueValue();
}
/**
* Retrieves the link for the current record.
*
* @param {boolean} noStack
* Flag to indicate whether to suppress the `sysparm_stack` URL paramter.
*
* @returns {string}
* URL of the underlying record.
*
* @throws {Error}
* If the underlying record already was deleted before.
*/
getLink(
noStack
) {
const METHOD_NAME = `[${this.constructor.name}.getLink] `;
this._testIsDeleted(METHOD_NAME);
if (arguments.length > 0 && typeof noStack !== 'boolean') {
throw new TypeError(
METHOD_NAME +
`Value in parameter "noStack" is not of boolean type!`
);
}
return this.getGlideRecord().getLink(noStack);
}
/**
* Retrieves the display value for the current record if no parameter was passed
* or for the specified field in parameter `strFieldName`.
*
* @param [string] strFieldName
* If specified `strFieldName` must represent a column in the underlying table.
*
* @returns {string}
* Either the display value of the underlying record or of the specified field.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
getDisplayValue(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.getDisplayValue] `;
this._testIsDeleted(METHOD_NAME);
if (arguments.length === 0) {
return String(this.getGlideRecord().getDisplayValue());
}
this._testFieldName(METHOD_NAME, strFieldName);
return String(this.getGlideRecord().getElement(strFieldName).getDisplayValue());
}
/**
* Returns an internal name for the type of a field.
*
* @param {string} strFieldName
* Name of the corresponding database column.
*
* @returns {string}
* Internal name for the field type like "number" or "glide_data_time".
*
* @throws {TypeError}
* If value in `strFieldName` does not exists or is empty.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*
* @see
* {@link https://www.servicenow.com/docs/csh?topicname=r_FieldTypes.html&version=latest}
* for a list of all types.
*/
getFieldType(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.getFieldType] `;
this._testIsDeleted(METHOD_NAME);
this._testFieldName(METHOD_NAME, strFieldName);
return this.getGlideRecord().getElement(strFieldName).getED().getInternalType();
}
/**
* Returns for the specified field an object representing any ServiceNow object
* depending on the field type. For example for a field of type 'date/time' that method
* would return a reference to an initialized GlideDateTime object.
*
* @param {string} strFieldName
* Name of the corresponding database column.
*
* @returns {object}
* Any ServiceNow object, or null if the field has no value or is not eligible to
* return an object, such as a "string" field.
*
* @throws {TypeError}
* If value in `strFieldName` does not exists or is empty.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
getGlideObject(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.getGlideObject] `;
this._testIsDeleted(METHOD_NAME);
this._testFieldName(METHOD_NAME, strFieldName);
return this.hasValue(strFieldName) ?
this.getGlideRecord().getElement(strFieldName).getGlideObject() || null :
null;
}
/**
* Returns a `GlideRecord` object for a given reference field.
*
* **Warning:** If the reference element does not contain a value,
* it returns an empty `GlideRecord` object, not a NULL object.
*
* @param {string} strFieldName
* Name of the corresponding database column.
*
* @returns {GlideRecord}
* An instance of a `GlideRecord` object - either fully initialized or empty.
*
* @throws {TypeError}
* - If value in `strFieldName` does not exists or is empty.
* - If field is not of type "reference".
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
getRefRecord(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.getRefRecord] `;
this._testIsDeleted(METHOD_NAME);
this._testFieldName(METHOD_NAME, strFieldName);
if (this.getFieldType(strFieldName) !== 'reference') {
throw new TypeError(
METHOD_NAME +
`"${strFieldName}" does not represent a reference field!`
);
}
return this.getGlideRecord().getElement(strFieldName).getRefRecord();
}
/**
* Retrieves the label of either the table (optionally in the plural form)
* or the specified field.
*
* @param [boolean|string] objParam
* If the parameter is empty or of boolean type, the label of the underlying table
* is returned in singular form. If `objParam` is `true`, the label is returned in
* plural form. If `objParam` is a string, the label of the respective field is returned.
*
* @returns {string}
* Either the label of the underlying table or the specified field.
*
* @throws {TypeError}
* For field-based retrieval:
* If value in `objParam` does not exists or is empty.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - For field-based retrieval:
* If value in `objParam` does not represent a valid database column.
*/
getLabel(
objParam
) {
const METHOD_NAME = `[${this.constructor.name}.getLabel] `;
this._testIsDeleted(METHOD_NAME);
//1st option: retrieve the table label - optionally in the plural form
if (arguments.length === 0 || typeof objParam === 'boolean') {
return objParam === true ?
String(this.getGlideRecord().getPlural()) :
String(this.getGlideRecord().getClassDisplayValue());
}
//2nd option: retrieve a field's label
this._testFieldName(METHOD_NAME, objParam);
return this.getGlideRecord().getElement(objParam).getLabel();
}
/**
* Determines if the specified field is defined has a value.
*
* @param {string} strFieldName
* Name of the field to check.
*
* @returns {boolean}
* `true` if the field has a value, otherwise `false`.
*
* @throws {TypeError}
* If value in `strFieldName` does not exists or is empty.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
hasValue(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.hasValue] `;
this._testIsDeleted(METHOD_NAME);
this._testFieldName(METHOD_NAME, strFieldName);
return !this.getGlideRecord().getElement(strFieldName).nil();
}
/**
* Returns the value of the specified field.
*
* @param {string} strFieldName
* Name of the field whose content is to be returned.
*
* @returns {string}
* The value of the specified field or an empty string if it has no value.
*
* @throws {TypeError}
* If value in `strFieldName` does not exists or is empty.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
getValue(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.getValue] `;
this._testIsDeleted(METHOD_NAME);
this._testFieldName(METHOD_NAME, strFieldName);
return String(this.getGlideRecord().getValue(strFieldName) || '').trim();
}
/**
* Returns the clear text value for Password (2 way encrypted) fields.
*
* @param {string} strFieldName
* Name of the field whose content is to be decrypted.
*
* @returns {string}
* Plain text value of the dercypted content.
*
* @throws {TypeError}
* - If value in `strFieldName` does not exists or is empty.
* - If field is not of type "password2".
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
getDecryptedValue(
strFieldName
) {
const METHOD_NAME = `[${this.constructor.name}.getDecryptedValue] `;
this._testIsDeleted(METHOD_NAME);
this._testFieldName(METHOD_NAME, strFieldName);
if (this.getFieldType(strFieldName) !== 'password2') {
throw new TypeError(
METHOD_NAME +
`"${strFieldName}" does not represent a Password2 field!`
);
}
return this.getGlideRecord().getElement(strFieldName).getDecryptedValue();
}
/**
* Sets the value of a field by considering different field types, so this method also
* works for "Password2" and "Journal" fields.
*
* @param {string} strFieldName
* Name of the field whose content is to be decrypted.
*
* @param {object} objFieldValue
* Value that should be set for the specified field.
*
* @throws {TypeError}
* - If value in `strFieldName` does not exists or is empty.
* - If value in `objFieldValue` does not exists or is null.
*
* @throws {Error}
* - If the underlying record already was deleted before.
* - If the specified field name does not represent a valid database column.
*/
setValue(
strFieldName,
objFieldValue
) {
const METHOD_NAME = `[${this.constructor.name}.setValue] `;
this._testIsDeleted(METHOD_NAME);
this._testFieldValue(METHOD_NAME, strFieldName, objFieldValue);
switch (this.getFieldType(strFieldName)) {
case 'password2':
this.getGlideRecord().getElement(strFieldName).setDisplayValue(objFieldValue);
break;
case 'journal_input':
this.getGlideRecord().getElement(strFieldName).setJournalEntry(objFieldValue);
break;
default:
this.getGlideRecord().getElement(strFieldName).setValue(objFieldValue);
break;
}
}
/**
* Inserts a new record into the database with its specified field values.
*
* @returns {string}
* The Sys ID of the inserted record, or `null` if the record could not be inserted.
*
* @throws {Error}
* - If the underlying record already was inserted before.
* - If the underlying record already was deleted before.
*/
insert(
) {
const METHOD_NAME = `[${this.constructor.name}.insert] `;
this._testIsInserted(METHOD_NAME);
this._testIsDeleted(METHOD_NAME);
return this.getGlideRecord().insert();
}
/**
* Updates a new record into the database with its specified field values.
*
* @returns {string}
* The Sys ID of the updated record, or `null` if the record could not be updated.
*
* @throws {Error}
* - If the underlying record was not inserted before.
* - If the underlying record already was deleted before.
*/
update(
) {
const METHOD_NAME = `[${this.constructor.name}.update] `;
this._testIsNotInserted(METHOD_NAME);
this._testIsDeleted(METHOD_NAME);
return this.getGlideRecord().update();
}
/**
* Deletes the underlying record in the database.
*
* @returns {boolean}
* `true` if the record was successfully deleted, otherwise `false`.
*
* @throws {Error}
* - If the underlying record was not inserted before.
* - If the underlying record already was deleted before.
*/
deleteRecord(
) {
const METHOD_NAME = `[${this.constructor.name}.deleteRecord] `;
this._testIsNotInserted(METHOD_NAME);
this._testIsDeleted(METHOD_NAME);
var _grRecord = this.getGlideRecord();
//if the record recently was initialized we need to get a fresh GlideRecord,
//otherwise an internal exception will be thrown by ServiceNow
if (this._wasNewRecord === true) {
_grRecord = new GlideRecord(this.getTableName());
if (!_grRecord.get(this.getSysID())) {
throw new Error(
METHOD_NAME +
`No record exists for Sys ID = "${this.getSysID()}" ` +
`at table "${this.getTableName()}"!`
);
}
}
this._isDeleted = _grRecord.deleteRecord();
return this._isDeleted;
}
//-------------------------------------------------------------------------------------------
// private Methods
//-------------------------------------------------------------------------------------------
/**
* Tests if the passed field name is valid and also represents a database column
* in the underlying table.
*
* @param {string} strFieldName
* Name of the database column to test.
*
* @throws {TypeError}
* If value in `strFieldName` does not exists or is empty.
*
* @throws {Error}
* If the specified field name does not represent a valid database column.
*/
_testFieldName(
strMethodName,
strFieldName
) {
if (typeof strFieldName === 'undefined') {
throw new TypeError(
strMethodName + 'No field name for validation passed!'
);
}
var _strFieldName = String(strFieldName).trim();
if (_strFieldName === '') {
throw new TypeError(
strMethodName + 'No field name for validation passed!'
);
}
if (!this.getGlideRecord().isValidField(_strFieldName)) {
throw new Error(
strMethodName +
`"${_strFieldName}" is not a valid field name ` +
`for table "${this.getTableName()}"!`
);
}
}
_testFieldValue(
strMethodName,
strFieldName,
strFieldValue
) {
this._testFieldName(strMethodName, strFieldName);
if (typeof strFieldValue === 'undefined' || strFieldValue === null) {
throw new TypeError(
strMethodName +
`No value for the field ${strFieldName} was passed!`
);
}
}
_testIsInserted(
strMethodName
) {
if (!this.isNewRecord()) {
throw new Error(
strMethodName +
`Record with Sys ID = "${this.getSysID()}" already exists in the database!`
);
}
}
_testIsNotInserted(
strMethodName
) {
if (this.isNewRecord()) {
throw new Error(
strMethodName + 'The record was not inserted before yet!'
);
}
}
_testIsDeleted(
strMethodName