GlideAgentWorkspace (g_aw): クライアント
g_aw API は、UI アクションまたはクライアントスクリプトが指定されたレコードを [エージェントワークスペース] タブで開くことができるようにするメソッドを提供します。
このクラスにはコンストラクターはありません。g_aw グローバルオブジェクトを使用して、GlideAgentWorkspace のメソッドにアクセスします。
GlideAgentWorkspace - closeRecord()
エージェントワークスペース 内のサブタブで現在開いているレコード (フォームなど) を閉じます。
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
| タイプ | 説明 |
|---|---|
| なし |
function 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 処理: .then() と .catch() を使用して、 closeRecord() の非同期性を処理します。
- エラー処理:成功した試行と失敗した試行の両方の詳細なログを提供します。
(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(文字列 table, 文字列 sysId, オブジェクト params)
エージェントワークスペース 内のサブタブで、指定されたレコード (フォームなど) を開きます。
| 名前 | タイプ | 説明 |
|---|---|---|
| table | 文字列 | 開くレコードを含むテーブルの名前です。 |
| 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 |
| ドメイン有効化レコード() | ブール | 現在のレコードがドメインセパレーション (ドメイン対応) であるかどうかをメソッドがチェックするかどうかを示すフラグ。 有効な値:
デフォルト値: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」クライアントスクリプトとして追加できます。エージェントがレコードを開くと、現在のドメイン名を示すアラートがポップアップ表示されます。