Great to hear it's working! To add the additional condition that the RITM in which the asset is already used is not in a "Closed Complete" state, you can enhance the Script Include's query accordingly. You'll need to adjust the query to exclude RITMs that are in the "Closed Complete" state.
Assuming the state for "Closed Complete" is represented by a specific value (e.g., 3), you can add this to your query logic.
Updated Query with State Check
Modify the Script Include to exclude RITMs that are in a "Closed Complete" state.
Script Include: RitmAssetChecker
Make sure the state value for "Closed Complete" matches the state value in your instance (typically 3).
var RitmAssetChecker = Class.create();
RitmAssetChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkAssetUsage: function() {
var assetSysId = this.getParameter('assetSysId');
var currentRitmSysId = this.getParameter('currentRitmSysId');
if (!assetSysId || assetSysId == '') {
return JSON.stringify({status: 'error', message: 'Asset ID is required'});
}
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addEncodedQuery('asset=' + assetSysId + '^sys_id!=' + currentRitmSysId + '^state!=3');
ritmGr.query();
if (ritmGr.next()) {
return JSON.stringify({status: 'error', message: 'This asset is already used in RITM: ' + ritmGr.number});
}
return JSON.stringify({status: 'success', message: 'Asset is available for use'});
}
});
Explanation
- Added
^state!=3: This part of the query ensures that any RITMs with the assetSysId but not in "Closed Complete" state are considered.
- Return Correct Info: If a matching, non-closed RITM is found, the script returns an error with the relevant RITM number.
Client Script: No Changes Needed
Your client script doesn’t need to change for this additional condition since the logic for state check is entirely on the server-side in the Script Include. It should still look like this:
function onSubmit() {
var transfer = g_form.getValue('variables.transfer');
var asset = g_form.getValue('asset');
// Proceed only if the transfer variable is set to 'yes'
if (transfer == 'yes' && asset) {
var ga = new GlideAjax('RitmAssetChecker');
ga.addParam('sysparm_name', 'checkAssetUsage');
ga.addParam('assetSysId', asset);
ga.addParam('currentRitmSysId', g_form.getUniqueValue());
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
if (result.status === 'error') {
g_form.showErrorBox('asset', result.message);
g_form.addErrorMessage(result.message);
g_form.submitted = false; // Prevent form submission
return false;
}
g_form.submit(); // Allow form submission
});
return false; // Prevent form submission until the server response is processed
}
return true; // Allow form submission if `transfer` is not 'yes' or asset is not selected
}
Finishing Touches
- Test: Ensure comprehensive testing is performed, especially with different states of RITMs to confirm the condition works as intended.
- Adapt State Value: If "Closed Complete" state in your instance has a different value, adjust it accordingly in the query.
By including the state check in your query, the script should now also consider the closure state of the RITM before determining if the asset is in use, thus preventing form submission only when necessary.