- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2024 07:25 PM
Hi Community,
I'm facing an issue with the string type field which is removing the leading zeroes of a number when exporting report to mid server in csv file format.
Example, the employee number 00123, after export the report to mid server using export set, the number updated as "123".
Appreciate if someone could provide a script in export set pre-export script to add apostrophe in front of the number so that the leading zeros remain.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 08:18 AM
Try changing the table to a database view and running it.
var gr = new GlideRecord(tableName);
↓
var gr = new GlideRecord(viewName);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2024 08:38 PM
Below is sample code to add to your export set's pre-export script that will add a leading apostrophe to any numeric values in the specified field.
(function() {
// Field name to export
var fieldName = 'employee_number'; // Specify the field name to be exported
// Retrieve records for export
var gr = new GlideRecord('your_table_name'); // Specify the table name to target
gr.query();
while (gr.next()) {
var fieldValue = gr.getValue(fieldName);
// Add an apostrophe at the beginning of the number to preserve leading zeros
if (fieldValue && fieldValue.match(/^\d+$/)) {
// Add an apostrophe at the beginning of the number
var formattedValue = "'" + fieldValue;
gr.setValue(fieldName, formattedValue);
gr.update();
}
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 02:38 AM - edited 08-06-2024 11:38 PM
I've tried the script that you have provided but the apostrophe is not added to the field.
Actually the Export Definition from Export Set is coming from Database View which join 3 tables (refer to the attachment below). Btw the excel header display as table_name.field_name (dot walk) once exported to CSV.
Please help me with the solution/ scripts
I would like to know what is the difference between pre-script & post-script ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 08:47 AM - edited 08-06-2024 08:48 AM
In this case, I think it should be processed with "Pre-Processing Script".
前処理スクリプト
- 目的: このスクリプトは、インポート プロセスが開始される前に実行されます。インポートする前にデータを準備、検証、または変換するために使用されます。
- 使用例: データのクレンジング、変換、フィールドの前処理、外部データの取得など。
- 実行タイミング: データが ServiceNow にロードされる前に実行されます。
- 例: フィールド値の標準化、データの検証、エラーのログ記録。
後処理スクリプト
- 目的: このスクリプトは、データがインポートされた後に実行されます。インポート プロセスが完了したら、追加の処理または検証に使用されます。
- ユースケース: 追加のデータ処理、データの関連付け、トリガーまたはアラートの作成など。
- 実行タイミング: データが ServiceNow にロードされた後に実行されます。
- 例: インポートされたデータに基づいて他のテーブルにデータを挿入したり、関連レコードを更新したり、ビジネス ルールやワークフローをトリガーしたりします。
まとめ
- 前処理スクリプト: インポート前にデータを準備または変更します。
- 後処理スクリプト: インポート後に追加の処理または検証を実行します。
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 09:10 AM
Navigate to your Export Set:
Go to the Export Set you are using for the export.Create an Export Script:
Add the following script to the Export Set. This script will add an apostrophe before numeric fields:
(function() {
var tableName = 'your_table_name'; // Replace with your table name
var fieldName = 'employee_number'; // Replace with your field name
// Combine table name and field name for CSV export
var csvFieldName = tableName + '.' + fieldName;
var gr = new GlideRecord(tableName);
gr.query();
while (gr.next()) {
var fieldValue = gr.getValue(csvFieldName);
if (fieldValue && !fieldValue.startsWith("'")) {
gr.setValue(csvFieldName, "'" + fieldValue);
gr.update();
}
}
})();
- Replace 'employee_number' with the actual field name containing the numbers.
- Replace 'your_table_name' with the actual table name where the data is stored.
Execute the Export:
Run your export set as usual. The leading zeros should be preserved in the CSV file.