- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 01:23 AM
@Ankur Bawiskar Hi Hope you doing good
I have one requiremet can you please help to resolved this
I need to create one notification based on the field.In afe table i have two fields total request and amount spent so A nightly job should run and compare the project total request to amount spend on project if the amount spend is greater than the total request send notification. Set a flag to indicate that the notification has been sent and capture the date and time that the over budget notification was sent for the project. Log this information in the Activity log for the active AFE on the project and in the Activity Log on the Project record.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 01:57 AM
hello Everyone i have tried below scheduled job its working fine
//var recordCount = 0;
try {
var gr = new GlideRecord('x_asfi_afe_afe');
gr.addEncodedQuery('amount_spentISNOTEMPTY^total_requestISNOTEMPTY^budget=true');
gr.query();
while (gr.next()) {
try {
var amountSpent = parseFloat(gr.getValue('amount_spent')) || 0;
var totalRequest = parseFloat(gr.getValue('total_request')) || 0;
// Skip if totalRequest is 0
if (totalRequest === 0) continue;
var percentageSpent = (amountSpent / totalRequest) * 100;
if (percentageSpent >= 90) {
recordCount++;
// Format currency values
var formattedAmountSpent = amountSpent.toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
});
var formattedTotalRequest = totalRequest.toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
});
//Log record details
gs.info(
"AFE Details:\n" +
"Number: " + gr.number + "\n" +
"Amount Spent: " + formattedAmountSpent + "\n" +
"Total Budget: " + formattedTotalRequest + "\n" +
"Percentage: " + percentageSpent.toFixed(2) + "%"
);
// Queue the event notification
gs.eventQueue('afe_90_of_there_budget', gr, '');
// Update the record
// gr.setValue('over_budget', 'true');
// gr.update();
}
} catch (recordError) {
gs.error("Error processing AFE record " + gr.number + ": " + recordError);
gr.setValue('budget','true');
}
}
} catch (error) {
gs.error("Script execution failed: " + error);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 02:23 AM
what debugging did you do?
are you able to find records matching that query?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 05:46 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 01:57 AM
hello Everyone i have tried below scheduled job its working fine
//var recordCount = 0;
try {
var gr = new GlideRecord('x_asfi_afe_afe');
gr.addEncodedQuery('amount_spentISNOTEMPTY^total_requestISNOTEMPTY^budget=true');
gr.query();
while (gr.next()) {
try {
var amountSpent = parseFloat(gr.getValue('amount_spent')) || 0;
var totalRequest = parseFloat(gr.getValue('total_request')) || 0;
// Skip if totalRequest is 0
if (totalRequest === 0) continue;
var percentageSpent = (amountSpent / totalRequest) * 100;
if (percentageSpent >= 90) {
recordCount++;
// Format currency values
var formattedAmountSpent = amountSpent.toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
});
var formattedTotalRequest = totalRequest.toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
});
//Log record details
gs.info(
"AFE Details:\n" +
"Number: " + gr.number + "\n" +
"Amount Spent: " + formattedAmountSpent + "\n" +
"Total Budget: " + formattedTotalRequest + "\n" +
"Percentage: " + percentageSpent.toFixed(2) + "%"
);
// Queue the event notification
gs.eventQueue('afe_90_of_there_budget', gr, '');
// Update the record
// gr.setValue('over_budget', 'true');
// gr.update();
}
} catch (recordError) {
gs.error("Error processing AFE record " + gr.number + ": " + recordError);
gr.setValue('budget','true');
}
}
} catch (error) {
gs.error("Script execution failed: " + error);
}