GlideAgentWorkspace (g_aw): クライアント
g_aw API には、UI アクションまたはクライアントスクリプトが [エージェントワークスペース] タブで指定されたレコードを開くことができるメソッドが用意されています。
このクラスのコンストラクターはありません。g_aw グローバルオブジェクトを使用して GlideAgentWorkspace メソッドにアクセスします。
GlideAgentWorkspace - closeRecord()
エージェントワークスペース内のサブタブにある、フォームなどの現在開いているレコードを閉じます。
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
| タイプ | 説明 |
|---|---|
| なし |
関数 onClick(g_form) {function onClick(g_form) {
g_form.save().then(function(){
g_aw.closeRecord();
});
}
g_aw.closeRecord() メソッドを使用して、 エージェントワークスペースでボタンがクリックされたときにレコードを閉じます。このスクリプトは次のように使用できます。- このスクリプトを、 エージェントワークスペース用に構成された UI アクション (ボタン) に追加します。
- ボタンをクリックすると、現在のレコードを閉じようとします。
- 基本ログは成功または失敗を示します。
functioncloseCurrentRecord() {
if (typeof g_aw !== 'undefined' && g_aw.closeRecord) {
g_aw.closeRecord().then(function(response) {
console.log(response.success ? 'Record closed successfully.' : 'Failed to close the record.');
}).catch(function(error) {
console.error('Error closing the record:', error);
});
}
}
- トリガー条件:スクリプトは、インシデントのステータスが「解決済み」(
ステータス = 6) に設定されているかどうかを確認します。 - ワークスペースの検証:
typeof g_aw !== 'undefined'を使用して、コードが エージェントワークスペース 内でのみ実行されるようにします。 - Promise 処理: closeRecord() の非同期特性を処理するために .then() と .catch() を使用します。
- エラー処理:成功した試行と失敗した試行の両方について詳細なログ記録を提供します。
(function executeRule(current, gForm, gUser, gSNC) {
// Check if the incident state is 'Resolved' (state = 6 in default ServiceNow setup)
if (current.state == 6) {
// Ensure we're in Agent Workspace
if (typeof g_aw !== 'undefined' && g_aw.closeRecord) {
g_aw.closeRecord().then(function(response) {
if (response.success) {
console.log('Incident record closed successfully in Agent Workspace.');
} else {
console.error('Failed to close the record:', response.errorMessage);
}
}).catch(function(error) {
console.error('An error occurred while closing the record:', error);
});
}
}
})(current, gForm, gUser, gSNC);
GlideAgentWorkspace - openRecord (文字列テーブル、文字列 sysId、オブジェクトパラメーター)
フォームなどの指定されたレコードを エージェントワークスペース内のサブタブで開きます。
| 名前 | タイプ | 説明 |
|---|---|---|
| テーブル | 文字列 | 開くレコードを含むテーブルの名前。 |
| sysId | 文字列 | 開くレコードの Sys ID。 |
| params | オブジェクト | オプション。レコードに渡すパラメーターの名前/値のペア。 |
| params.readOnlyForm | ブール | 開かれたレコードのすべてのフィールドが読み取り専用かどうかを示すフラグ。UI ポリシーと ACL に関係なく。
デフォルト値:false |
| params.defaultTab | 文字列 | ワークスペースに表示する最初のタブの名前。関連アイテムまたは関連リストのみを指定できます。 指定しない場合、 hideDetails が true に設定されていない限り、詳細タブが表示されます。 関連リスト名を取得するために使用するメソッドの詳細については、「 getRelatedListNames()」を参照してください。 |
| params.hideDetails | ブール | 詳細タブと UI アクションを非表示にするかどうかを示すフラグ。
デフォルト値:false |
| タイプ | 説明 |
|---|---|
| なし |
サブタブでsys_userレコードを開きます。
g_aw.openRecord('sys_user', '62826bf03710200044e0bfc8bcbe5df1');
すべてのフィールドが読み取り専用のサブタブでレコードを開きます。
g_aw.openRecord('sys_user', '62826bf03710200044e0bfc8bcbe5df1', {readOnlyForm: true});
サブタブでレコードを開き、[グループ] 関連リストに直接移動します。
g_aw.openRecord('sys_user', '62826bf03710200044e0bfc8bcbe5df1', {defaultTab: "sys_user_grmember.user"});
サブタブでレコードを開きますが、フォームヘッダーと他のタブのみを表示します。
g_aw.openRecord('sys_user', '62826bf03710200044e0bfc8bcbe5df1', {hideDetails: true});
- 動的レコードを開く:スクリプトは、関連する変更要求のsys_idを現在のインシデントから取得します。
- エージェントワークスペースコンテキスト:スクリプトがエージェントワークスペースで実行されていることを確認するために、
g_awが利用可能かどうかをチェックします。 - カスタムパラメーター:
表示:「エージェント」は、特定のビューでレコードを開きます (オプション)。readOnly:trueの場合、レコードは読み取り専用モードで開きます (オプション)。
- エラー処理:応答とエラーの処理に .then() と .catch() を使用します。
function openRelatedChangeRequest() {
// Get the sys_id of the related Change Request from the current incident
var changeRequestSysId = g_form.getValue('change_request'); // Assuming 'change_request' is the field name
if (changeRequestSysId && typeof g_aw !== 'undefined' && g_aw.openRecord) {
g_aw.openRecord('change_request', changeRequestSysId, {
view: 'agent', // Optional: Specify a custom view
readOnly: true // Optional: Open the record in read-only mode
}).then(function(response) {
if (response.success) {
console.log('Change Request opened successfully.');
} else {
console.error('Failed to open Change Request:', response.errorMessage);
}
}).catch(function(error) {
console.error('Error opening Change Request:', error);
});
} else {
console.warn('No related Change Request found or Agent Workspace is not available.');
}
}
GlideAgentWorkspace - setSectionExpanded(文字列 section_name, ブール展開)
フォームセクションを展開状態または折りたたみ状態に設定します。
| 名前 | タイプ | 説明 |
|---|---|---|
| section_name | 文字列 | エージェントワークスペース のフォームセクションの名前。 |
| 展開済み | ブール | セクションをデフォルトで展開するか折りたたむかを示すフラグ。
|
| タイプ | 説明 |
|---|---|
| なし |
次の例は、 related_records という名前のフォームセクションをデフォルトで折りたたむように設定する方法を示しています。
function onLoad() {
g_aw.setSectionExpanded('related_records', false);
}
- 優先度ベースのロジック:スクリプトは
g_form.getValue('priority')を使用してインシデントの優先度をチェックします。 - 動的セクションコントロール:優先度が 1 (重大) または 2 (高) の場合は、[作業メモ] セクションを展開します。すっきりとした UI を維持するために、優先度を下げるには折りたたみます。
- エージェントワークスペースのチェック:スクリプトが エージェントワークスペースでのみ実行されるようにします。
javascriptCopyEdit(functiontoggleWorkNotesSection() {
// Check if we're in Agent Workspace and the method is availableif (typeof g_aw !== 'undefined' && g_aw.setSectionExpanded) {
// Get the incident priority from the formvar priority = g_form.getValue('priority');
// Automatically expand the "Work Notes" section for high-priority incidents (1 or 2)var shouldExpand = (priority == '1' || priority == '2');
// Expand or collapse the section based on priority
g_aw.setSectionExpanded('Work Notes', shouldExpand);
}
})(); この例は、 エージェントワークスペース のインシデントに対して、タイプを「onLoad」に設定したクライアントスクリプトとして追加できます。セクション名がフォームレイアウトに表示されると正確に一致していることを確認してください (「作業メモ」など)。GlideAgentWorkspace - domainScopeProvider()
ドメインスコープの詳細を取得します。
domainScopeProvider() メソッドは、4 つの関数にアクセスして、ドメインスコープに関する情報を返します。詳細については、「Domain scope」を参照してください。
必要なロール:domain_expand_scope
| 関数名 | 戻り値の型 | 説明 |
|---|---|---|
| getDomainScope() | 文字列 | ドメインスコープを取得します。 可能な値:
デフォルト値:false |
| hasDomainChanged() | ブール | 元のドメインと比較して現在のレコードのドメインが変更されたかどうかを示すフラグ。 有効な値:
デフォルト値:false |
| isDomainEnabledRecord() | ブール | 現在のレコードがドメインセパレーション (ドメイン対応) であるかどうかをメソッドがチェックするかどうかを示すフラグ。 有効な値:
デフォルト値:false |
| toggleDomainScope() | ブール | レコードのドメインスコープコンテキストを有効または無効にするかどうかを示すフラグ。 有効な値:
デフォルト:true |
| タイプ | 説明 |
|---|---|
| なし |
例
次の例は、UI アクションの職場クライアントスクリプトで、ユーザーセッションとレコードの間でドメインスコープを展開 (セッションスコープ) または折りたたみ (レコードスコープ) に切り替える方法を示しています。
function onClick(g_form) {
var provider = g_aw.domainScopeProvider();
provider.toggleDomainScope();
var domainScopeNow = provider.getDomainScope();
if (domainScopeNow === 'SESSION')
g_form.addInfoMessage(getMessage("Domain Scope Expanded"));
else if (domainScopeNow === 'RECORD')
g_form.addInfoMessage(getMessage("Domain Scope Collapsed"));
}
function onSubmit() {
if (typeof g_aw === 'undefined' || !g_aw.domainScopeProvider || typeof g_scratchpad === 'undefined') return true;
if (g_scratchpad._domainConfirmationPassed ||
g_scratchpad._domainCheckErrorBypass || g_scratchpad._domainCheckPassed) return true;
var provider = g_aw.domainScopeProvider();
if (!provider || !provider.isDomainEnabledRecord || !provider.isDomainEnabledRecord()) return true;
// if you change these messages, please change them in the above messages field var title = getMessage("Change Domain"); var message = getMessage("You are about to change the domain of this record which may result in data
loss.We will copy the information we can but you may need to replace the lost data.Do you want to proceed ? ");
var gFormRef = g_form;
var popModalConfirm = function() { g_modal.confirm(title, message , function(response) {
if (response) {
g_scratchpad._domainConfirmationPassed = true;
gFormRef.submit(gFormRef.getActionName());
}
});
return false;
};
var proceedWithSubmit = function() {
gFormRef.submit(gFormRef.getActionName());
};
var hasDomainChanged = provider.hasDomainChanged();
if (hasDomainChanged === false) return true;
if (hasDomainChanged === true) return popModalConfirm();
else {
hasDomainChanged.then(function(isChanged) {
if (isChanged)
return popModalConfirm();
else {
g_scratchpad._domainCheckPassed = true;
proceedWithSubmit();
}
}, function(error) {
g_scratchpad._domainCheckErrorBypass = true;
proceedWithSubmit();
});
return false;
}
}- 基本チェック:
typeof g_aw !=='undefined'は、スクリプトが エージェントワークスペースでのみ実行されるようにします。g_aw.domainScopeProviderは、メソッドが存在するかどうかを確認します。 - 単純な非同期処理:ドメイン情報が利用可能な場合は、
.then()を使用して結果を処理します。.catch()を使用してエラー (たとえば、ドメインの取得時に問題が発生した場合) を処理します。 - ユーザーフレンドリーなアラート:シンプルでわかりやすいドメイン名 (
alert('You are working in the domain: ...'))のアラートを表示します。ドメイン情報が見つからない場合は、「ドメイン情報は利用できません。" - エラー処理: エラーは、基本的なトラブルシューティングのために
console.error()を使用してコンソールに記録されます。
(function showDomainAlert() {
// Check if we're in Agent Workspace and the domainScopeProvider is available
if (typeof g_aw !== 'undefined' && g_aw.domainScopeProvider) {
// Get the current domain information
g_aw.domainScopeProvider().then(function(domainInfo) {
if (domainInfo && domainInfo.name) {
// Show an alert with the current domain name
alert('You are working in the domain: ' + domainInfo.name);
} else {
alert('Domain information is not available.');
}
}).catch(function(error) {
console.error('Error getting domain scope:', error);
});
}
})();
このスクリプトは、 エージェントワークスペースで「onLoad」クライアントスクリプトとして追加できます。エージェントがレコードを開くと、現在のドメイン名を示すアラートがポップアップ表示されます。