Last active
June 19, 2024 04:27
-
-
Save codaroma/824fc9680bf1ac46fff4b21447e34dfd to your computer and use it in GitHub Desktop.
ServiceNow script to update existing store applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Find all store applications that have a newer version available | |
* exclude apps with compatibility issues | |
* create a batch install plan to update them to the latest version | |
* pass the plan to sn_appclient.AppManagerAPI.installBatch | |
*/ | |
var result = (function () { | |
var storeApp = new GlideRecord("sys_store_app"); | |
storeApp.addQuery("version", "ISNOTEMPTY", ""); | |
storeApp.addQuery("assigned_version", "ISNOTEMPTY", ""); | |
storeApp.addQuery("latest_version", "ISNOTEMPTY", ""); | |
storeApp.addQuery("version", "SAMEAS", "assigned_version"); | |
storeApp.addQuery("version", "NSAMEAS", "latest_version"); | |
storeApp.addQuery("hide_on_ui", false); | |
storeApp.orderBy("name"); | |
storeApp.query(); | |
var updateApps = []; | |
while (storeApp.next()) { | |
var targetVersion = storeApp.latest_version.toString(); | |
if ( | |
!new sn_appclient.VersionComparator().isUpgrade( | |
storeApp.version, | |
targetVersion | |
) | |
) { | |
continue; //latest version is not newer | |
} | |
var appID = storeApp.getUniqueValue(); | |
var appVersion = new sn_appclient.UpdateChecker()._getSysAppVersion( | |
appID, | |
targetVersion | |
); | |
if (!appVersion || appVersion.block_install) { | |
continue; //not compatible | |
} | |
if ( | |
new sn_appclient.AppsDataAPI() | |
.getAppDependencyStatus( | |
storeApp.scope.toString(), | |
appVersion.dependencies | |
) | |
.some(function (dependencyDetails) { | |
return !dependencyDetails.active; | |
}) | |
) { | |
continue; //blocked by a dependency | |
} | |
updateApps.push({ | |
type: "application", | |
load_demo_data: false, | |
id: appID, | |
requested_version: targetVersion, | |
}); | |
//break; //uncomment to only install the 1st app found rather than all | |
} | |
if (updateApps.length === 0) { | |
return { error: "no compatible app updates found" }; | |
} | |
var batchResponse = JSON.parse( | |
sn_appclient.AppManagerAPI.installBatch( | |
JSON.stringify({ | |
name: | |
"Update existing applications " + | |
new GlideDateTime().getValue(), | |
packages: updateApps, | |
}) | |
) | |
); | |
var baseUrl = gs.getProperty("glide.servlet.uri"); | |
return { | |
links: { | |
execution_tracker: | |
baseUrl + | |
"sys_execution_tracker.do?sys_id=" + | |
batchResponse.links.progress.id, | |
batch_install_plan: | |
baseUrl + | |
"sys_batch_install_plan.do?sys_id=" + | |
batchResponse.links.results.id, | |
apps_list: | |
baseUrl + | |
"sys_store_app_list.do?sysparm_query=batch_install_plan%3D" + | |
batchResponse.links.results.id, | |
}, | |
response: batchResponse, | |
count: updateApps.length, | |
apps: updateApps, | |
}; | |
})(); | |
[result]; //result displays better in xplore if it's wrapped in an array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
var storeApp = new GlideRecord("sys_store_app"); | |
storeApp.addQuery("version", "ISNOTEMPTY", ""); | |
storeApp.addQuery("assigned_version", "ISNOTEMPTY", ""); | |
storeApp.addQuery("latest_version", "ISNOTEMPTY", ""); | |
storeApp.addQuery("version", "SAMEAS", "assigned_version"); | |
storeApp.addQuery("version", "NSAMEAS", "latest_version"); | |
storeApp.addQuery("hide_on_ui", false); | |
storeApp.orderBy("name"); | |
storeApp.query(); | |
var updateApps = []; | |
while (storeApp.next()) { | |
var targetVersion = storeApp.latest_version.toString(); | |
var appDetails = [ | |
storeApp.scope.toString(), | |
storeApp.name.toString(), | |
targetVersion, | |
storeApp.version.toString(), | |
]; | |
if ( | |
!new sn_appclient.VersionComparator().isUpgrade( | |
storeApp.version, | |
targetVersion | |
) | |
) { | |
updateApps.push( | |
appDetails.concat("latest version is not newer").join("\t") | |
); | |
continue; | |
} | |
var appID = storeApp.getUniqueValue(); | |
var appVersion = new sn_appclient.UpdateChecker()._getSysAppVersion( | |
appID, | |
targetVersion | |
); | |
if (!appVersion || appVersion.block_install) { | |
updateApps.push(appDetails.concat("not compatible").join("\t")); | |
continue; | |
} | |
if (!appVersion) { | |
updateApps.push( | |
appDetails.concat("version data not found").join("\t") | |
); | |
continue; | |
} | |
if (appVersion.block_install) { | |
updateApps.push(appDetails.concat("block_install=true").join("\t")); | |
continue; | |
} | |
if ( | |
new sn_appclient.AppsDataAPI() | |
.getAppDependencyStatus( | |
storeApp.scope.toString(), | |
appVersion.dependencies | |
) | |
.some(function (dependencyDetails) { | |
return !dependencyDetails.active; | |
}) | |
) { | |
updateApps.push( | |
appDetails.concat("blocked by a dependency").join("\t") | |
); | |
continue; | |
} | |
updateApps.push(appDetails.concat("ready to install").join("\t")); | |
} | |
return updateApps.join("\n"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment