'Copy Change' UI Action custom field validation.

Ben42
Tera Contributor

Hello,

 

We have a custom group field on change request form. And I have given that field name in system property 'com.snc.change_request.copy.attributes' so that when we click on “copy change” ui action that custom field also gets copied to new change request.

 

But, if we create a change request with a group ‘A’ in the custom field and if that group ‘A’ is deactivated later and if we then use ootb “copy change” ui action the inactivated group ‘A’ is also copied to new change request. But I want to avoid copying inactive groups. 


We are using ootb copy change ui action and I see scripts in it are read-only.

 

Thanks,

Ben.

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Paste this code below the "Add customer changes below" comment in ChangeUtils Script Include.
(Please correct 'u_custom_group' to an appropriate column name.)

    getChangeQueryParams: function() {
        //read attributes list
        var attributesList = this._getCsvPropertyValue(this.PROP_CHANGE_ATTRS,
            this.CHANGE_REQUEST_DEFAULT_ATTR_VALUE);
        var srcSysId = this.getParameter('sysparm_src_sysid');
        var gr = new GlideRecordSecure(this.CHANGE_REQUEST);
        if (gr.get(srcSysId)) {
            return this._getRecordValuesAsEncodedQueryForCopyChange(gr, attributesList);
        } else {
            this.log.logErr('Invalid src change_request sysid provided = ' +
                srcSysId);
        }
    },

    _getRecordValuesAsEncodedQueryForCopyChange: function(record, attributesList) {
        var table = j2js(record.getTableName());

        var gr = new GlideRecordSecure(table);
        for (var i = 0; i < attributesList.length; ++i) {
            var name = attributesList[i];
            if (record.isValidField(name)) {
                if (record.getValue(name)) {
                    if (!gs.nil(record.getElement(name)) && !gs.nil(record.getElement(name).getED())) {

                        // copy custom group only if active is true
                        if (name == "u_custom_group") {
                            var isActive = record.u_custom_group.active;
                            if (isActive)
                                gr.addQuery(name, record.getValue(name));

                        } else {
                            var ed = record.getElement(name).getED();
                            // We have to use the display value if it's a date based field for form filter
                            if (ed.getInternalType() + '' === "glide_date_time" || ed.getInternalType() + '' === "glide_time" || ed.isEncrypted())
                                gr.addQuery(name, record.getDisplayValue(name));
                            else
                                gr.addQuery(name, record.getValue(name));
                        }

                    } else
                        gr.addQuery(name, record.getValue(name));
                }
            } else
                this.log.logWarning("Invalid field '" + name + "' provided for table '" + table + "'.");
        }
        return gr.getEncodedQuery();
    },

 

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi.
To override a function defined in the SNC script include, copy the function to the non-SNC script include and customize it.
For example, you can copy the function from ChangeUtilsSNC and paste it into ChangeUtils.ChangeUtils.png

Hello  Yu,

Thanks for the response, Could you tell me how should the script look like for my requirement?

 

Thanks,

Ben.

Community Alums
Not applicable

Paste this code below the "Add customer changes below" comment in ChangeUtils Script Include.
(Please correct 'u_custom_group' to an appropriate column name.)

    getChangeQueryParams: function() {
        //read attributes list
        var attributesList = this._getCsvPropertyValue(this.PROP_CHANGE_ATTRS,
            this.CHANGE_REQUEST_DEFAULT_ATTR_VALUE);
        var srcSysId = this.getParameter('sysparm_src_sysid');
        var gr = new GlideRecordSecure(this.CHANGE_REQUEST);
        if (gr.get(srcSysId)) {
            return this._getRecordValuesAsEncodedQueryForCopyChange(gr, attributesList);
        } else {
            this.log.logErr('Invalid src change_request sysid provided = ' +
                srcSysId);
        }
    },

    _getRecordValuesAsEncodedQueryForCopyChange: function(record, attributesList) {
        var table = j2js(record.getTableName());

        var gr = new GlideRecordSecure(table);
        for (var i = 0; i < attributesList.length; ++i) {
            var name = attributesList[i];
            if (record.isValidField(name)) {
                if (record.getValue(name)) {
                    if (!gs.nil(record.getElement(name)) && !gs.nil(record.getElement(name).getED())) {

                        // copy custom group only if active is true
                        if (name == "u_custom_group") {
                            var isActive = record.u_custom_group.active;
                            if (isActive)
                                gr.addQuery(name, record.getValue(name));

                        } else {
                            var ed = record.getElement(name).getED();
                            // We have to use the display value if it's a date based field for form filter
                            if (ed.getInternalType() + '' === "glide_date_time" || ed.getInternalType() + '' === "glide_time" || ed.isEncrypted())
                                gr.addQuery(name, record.getDisplayValue(name));
                            else
                                gr.addQuery(name, record.getValue(name));
                        }

                    } else
                        gr.addQuery(name, record.getValue(name));
                }
            } else
                this.log.logWarning("Invalid field '" + name + "' provided for table '" + table + "'.");
        }
        return gr.getEncodedQuery();
    },