Business Rule

pansarepooj
Tera Contributor

Can we call BUSINESS RULE in client script?

3 REPLIES 3

Jean Ferreira
Giga Guru

You cannot.
But, you can create a client callable script include with the function contained in you business rule.

Then you modify the business rule to call the script include function and then you create a client script that will also call the same script include function.

 

Script Include

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

    myFunction: function () {
        var recordSysID = this.getParameter('sysparm_record_sys_id');
        return new global.JSON().encode(this.myFunctionServer(recordSysID));
    },

    myFunctionServer: function (recordSysID) {
        return {
            sys_id: '' + recordSysID,
            result: 'Here is the result'
        }

    },
    type: "CustomScriptInclude"
};

Business Rule

(function executeRule(current, previous /*null when async*/ ) {
    var customScriptInclude = new CustomScriptInclude();
    customScriptInclude.myFunctionServer('' + current.sys_id)
});

 On change client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var ajax = new GlideAjax('CustomScriptInclude');
    ajax.addParam('sysparm_record_sys_id', g_form.getUniqueValue());
    ajax.addParam('sysparm_name', 'myFunction');
    ajax.getXMLAnswer(getAnswer);

    function getAnswer(result) {
        var resultObj = JSON.parse(result);
        gs.addInfoMessage(resultObj.result)
    }
}

Nikhil Bajaj9
Giga Sage

Hi @pansarepooj ,

 

Nope. That is the reason we have Glideajax and g_scratchpad to get server side data/code on client side.

 

If my answer helped you in anyways, please mark it- Solution Accepted.

Regards,

Nikhil Bajaj

 

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj

Yousaf
Giga Sage

Hi @pansarepooj 

No you cannot directly call BR from client script. Can you explain what are you trying to acheive?
you can use what Jean said by using script include and client script
or you can send data to client side by using display business rule with g_scratchpad


***Mark Correct or Helpful if it helps.***