Updating a date field via background script not working

matthew_hughes
Kilo Sage

I'm trying to developer a background script that will update the value of the 'Next AMA Due Date' field, but in order to do that,  I need to retrieve the value of the date from the 'Last AMA Scoping Date' field and add a year to it. I've tried to run the following script in the background, however, nothing is happening:

 

 

 

var app = new GlideRecord('cmdb_ci_business_app');
var correctDate = new GlideDateTime();
app.addEncodedQuery('u_next_ama_due_dateNSAMEASu_last_scoping_ama_completed_date@day^u_in_scope_for_controls=not_in_scope^number=AL05053');
app.query();
while(app.next()) {
correctDate = new GlideDateTime(app.u_last_scoping_ama_completed_date);
correctDate.addYearsUTC(1);
app.setValue('u_next_ama_due_date', correctDate);
app.update();
}

 

Is anyone able to see that issue and explain where I'm going wrong?

1 ACCEPTED SOLUTION

matthew_hughes
Kilo Sage

I managed to get it to work by using the following script:

//Search for any Business Applications that are Not in Scope with an 'AMA rating' of Enhanced and where the 'Last scoping AMA completed date' field is different to the 'Next AMA due date' field
var enhancedApp = new GlideRecord('cmdb_ci_business_app');
enhancedApp.addEncodedQuery('u_next_ama_due_dateNSAMEASu_last_scoping_ama_completed_date@day^u_in_scope_for_controls=not_in_scope^u_access_management_controls=enhanced');
enhancedApp.query();
while(enhancedApp.next()) {

//If any business applications return from the search, add 365 days to the 'Last scoping AMA completed date' date field
var enhancedScopingDate = enhancedApp.u_last_scoping_ama_completed_date;
var enhancedGdt = new GlideDateTime(enhancedScopingDate);
enhancedGdt.addDaysLocalTime(365);
enhancedApp.setValue('u_next_ama_due_date', enhancedGdt);

//Update the 'Next AMA due date' field with the new date
enhancedApp.update();
}

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Add some gs.print lines to log what is happening - are any records returned by the GR?  What is the value of correctDate before and after addYears?

Satishkumar B
Giga Sage
Giga Sage

Hi @matthew_hughes 

 

// Initialize GlideRecord for cmdb_ci_business_app table
var app = new GlideRecord('cmdb_ci_business_app');

// Add encoded query to filter records
app.addEncodedQuery('u_in_scope_for_controls=not_in_scope^number=AL05053'); // Adjust this query as per your requirements
app.query(); // Execute the query

// Loop through each record matching the query
while (app.next()) {
// Retrieve the value of 'u_last_scoping_ama_completed_date' and add a year
var lastScopingDate = new GlideDateTime(app.u_last_scoping_ama_completed_date);
lastScopingDate.addYearsUTC(1);

// Set the value of 'u_next_ama_due_date' to the calculated date
app.u_next_ama_due_date = lastScopingDate;

// Update the record
app.update();
}

 

----------------------------------------
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

matthew_hughes
Kilo Sage

I managed to get it to work by using the following script:

//Search for any Business Applications that are Not in Scope with an 'AMA rating' of Enhanced and where the 'Last scoping AMA completed date' field is different to the 'Next AMA due date' field
var enhancedApp = new GlideRecord('cmdb_ci_business_app');
enhancedApp.addEncodedQuery('u_next_ama_due_dateNSAMEASu_last_scoping_ama_completed_date@day^u_in_scope_for_controls=not_in_scope^u_access_management_controls=enhanced');
enhancedApp.query();
while(enhancedApp.next()) {

//If any business applications return from the search, add 365 days to the 'Last scoping AMA completed date' date field
var enhancedScopingDate = enhancedApp.u_last_scoping_ama_completed_date;
var enhancedGdt = new GlideDateTime(enhancedScopingDate);
enhancedGdt.addDaysLocalTime(365);
enhancedApp.setValue('u_next_ama_due_date', enhancedGdt);

//Update the 'Next AMA due date' field with the new date
enhancedApp.update();
}