Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Brad Bowman
Kilo Patron
Kilo Patron

Hi Matthew,

You can do this with a Display or before Update Business Rule, if you want the field populated when a record is viewed or updated, or a Fix Script if you want to update all of the records.  The BR would look similar to this:

(function executeRule(current, previous /*null when async*/) {
	var newDate = gs.getProperty("lbg.AMANewApplicationDate");
	if (current.date_field_name < newDate) {
		current.string_field_name = 'Legacy';
	} else {
		current.string_field_name = 'New';
	}
        current.update(); //only use this line if When to run is Display or before Update 
})(current, previous);

@Brad Bowman  It's on a catalogue item when it needs to be displayed

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @matthew_hughes ,

 

Assuming you wanted this to work on catalog item.....

1. Create "Legacy or New" variable as select Box. as follow.

SiddheshGawade_0-1705423405538.png

2. Then Create a onchnage Client script on Create Date Variable .

SiddheshGawade_1-1705423474798.png

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

        ga.getXMLAnswer(processResponse);

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

            } else if (reposnse == 'false') {
                g_form.setValue('legacy_or_new', 'new');
            }
        }
}

 

Create a client callable script include: 

SiddheshGawade_2-1705423542483.png

var validateCreateDateUtils = Class.create();
validateCreateDateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateDate: function() {
        var CreateDate = this.getParameter('sysparm_date');
        var DateTime = new GlideDateTime(CreateDate);
        var propDate = new GlideDateTime(gs.getProperty('applicationDatecheck'));   ///get your sys_property Value

        if (CreateDate < propDate) {
            return true;

        } else {
            return false;
        }
    },


    type: 'validateCreateDateUtils'
});

 

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


Regards,

Siddhesh

 

@Siddhesh Gawade When I try and select a business application, I get the error message of 'There is a JavaScript error in your browser console' and the Legacy or New system stays at None