Populate the value of the field depending on the created date of a configuration item

matthew_hughes
Kilo Sage

I've got a situation where I'm trying to populate a field called 'Legacy or New' depending on the value of the created date:

matthew_hughes_0-1705410689357.png

 

I'm wanting to do a comparison of the value of the Test create date field against the value of a system property:

matthew_hughes_1-1705410776145.png

 

The outcome that I'm wating to achieve is that if the created date of the selected business application is older than 01-09-2024, then the 'Legacy or New' field should be populated to 'Legacy'. Otherwise, the 'Legacy or New' field should be populated to 'New'.

 

If somebody could let me know what I need to do, that would be great.

 

 

10 REPLIES 10

Hello @matthew_hughes ,

 

There must be something wrong with your code, can you share the code that you are using?

Hi @Siddhesh Gawade I've updated the code, but now it seems that the response is always being set to 'Legacy' even though the create date of an application is newer than the system property:

Client Script:

matthew_hughes_0-1705483438236.png

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var createDate = g_form.getValue('test_create_date');
   
        var ga = new GlideAjax('LBGAMABusinessAssessmentCatItem');
        ga.addParam('sysparm_name', 'validateDate');
        ga.addParam('sysparm_date', createDate);

        ga.getXMLAnswer(processResponse);

        function processResponse(response) {
            alert('response is ' + response);
            if (response == 'true') {
                g_form.setValue('legacy_or_new', 'legacy');

            } else if (response == 'false') {
                g_form.setValue('legacy_or_new', 'new');
            }
        }
}
 
Script Include:
var LBGAMABusinessAssessmentCatItem = Class.create();
LBGAMABusinessAssessmentCatItem.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    type: 'LBGAMABusinessAssessmentCatItem',
    getWholeApplication: function() {
        // Retrieve parameters
        var sysid = this.getParameter("sysparm_sysid");

        // Outer JSON
        var jsonRet = {};

        // Look up the status
        var grApp = new GlideRecord("cmdb_ci_business_app");
        grApp.get(sysid);
        jsonRet["u_access_management_controls"] = grApp.getDisplayValue("u_access_management_controls");
        jsonRet["sys_created_on"] = grApp.getDisplayValue("sys_created_on");
        jsonRet["number"] = grApp.getValue("number");

        // Return it
        return JSON.stringify(jsonRet);


    },

    //Calculates difference in days between the creation date of the selected business application and the date defined in the lbg.AMANewApplicationDate system property
    getDateTodayDiff: function() {
        var firstDT = this.getParameter('sysparm_dt');
        var toDate = new GlideDateTime(gs.getProperty('lbg.AMANewApplicationDate'));
        //Convert dates to GlideDateTime format
        var fromDate = this.convertToGlideDateTime(firstDT);
        //Calculate difference
        var diff = GlideDateTime.subtract(fromDate,toDate);
        //Convert to seconds
        var diffseconds = diff.getNumericValue();  
        //Convert to days
        var diffdays = diffseconds/86400000;
       
        return diffdays;
    },

    validateDate: function() {
        var CreateDate = this.getParameter('sysparm_date');
        var DateTime = new GlideDateTime(CreateDate);
        var propDate = new GlideDateTime(gs.getProperty('lbg.AMANewApplicationDate'));

        if (CreateDate < propDate) {
            return true;

        } else {
            return false;
        }
    },

});

it should be returning false if the create date is newer than the property.

 

Can you please check the format of date you are using in your system property. it should similar to "var DateTime"  in your script.

Ahana 01
Tera Expert


Sure, you can achieve this by creating a business rule in ServiceNow. Here's a step-by-step guide:

1. Navigate to System Definition > Business Rules in ServiceNow.
2. Click on New to create a new business rule.
3. Give your business rule a name, for example, "Update Field Based on CI Creation Date".
4. In the Table field, select the table where your configuration item (CI) resides, typically this would be the cmdb_ci table.
5. In the When to run section, select "before" and "insert" if you want the rule to run before a new record is inserted into the database.
6. In the Advanced tab, you can write a script to update the field value based on the created date of the CI. Here's a sample script:

javascript
(function executeRule(current, previous /*null when async*/) {
var createdDate = current.sys_created_on;
if (createdDate != '') {
var year = createdDate.getFullYear();
if (year < 2020) {
current.field_name = 'Value for CIs created before 2020';
} else {
current.field_name = 'Value for CIs created in 2020 and later';
}
}
})(current, previous);

Replace "field_name" with the actual name of the field you want to update.

7. Click on Submit to save the business rule.

Please note that this is a basic example and you might need to adjust the script based on your specific requirements.


nowKB.com

For a good and optimistic result, and solving ServiceNow-related issues please visit this website.https://nowkb.com/home

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @matthew_hughes ,

 

I checked your client script and script include it is working on my side. So I am confident that the date format of your property must be incorrect. Please check with format: 2024-01-09

SiddheshGawade_0-1705487617017.png

 

 

Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.


Regards,

Siddhesh