Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Populate Install base item from Sold Product field

sreeshsurendran
Tera Guru

Hi all,

 

The requirement is to populate Install base item from my sold product on the case table. I have tried the below logic but somewhere it's going wrong and not working.

 

Business Rule:

 

When: after

Insert: true

Order: 200

 

Script:

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var installBase = new GlideRecord('sn_install_base_item');
    installBase.addQuery('name', current.sold_product.name);
    installBase.query();
    if (installBase.next()) {
        current.u_installed_base_item = installBase.sys_id;
        //current.update();
    }
})(current, previous);

 

I tried with current.update() and it was working, but it's not the best practice. Kindly guide me. A working code also would make a difference to me.

 

Best Regards,

Sreesh Surendran

1 ACCEPTED SOLUTION

Rupanjani
Giga Guru

Hi @sreeshsurendran,

 

Try using Before BR instead of After BR. Hope that works.

 


Mark the response correct and helpful if the answer assisted your question.

View solution in original post

4 REPLIES 4

Rupanjani
Giga Guru

Hi @sreeshsurendran,

 

Try using Before BR instead of After BR. Hope that works.

 


Mark the response correct and helpful if the answer assisted your question.

Thanks @Rupanjani  - it worked.

glenn_pinto
ServiceNow Employee
ServiceNow Employee

Does anyone have a script for the reverse use case of populating the sold product based on the install base item?

This is what i have client side onChange script:

var ga = new GlideAjax('ScriptIncludename');
ga.addParam('sysparm_name', 'functionName');
ga.addParam('sysparm_install_base', newValue); // install_base sys_id
ga.getXMLAnswer(function(response) {
if (response) {
g_form.setValue('sold_product', response);
} else {
g_form.clearValue('sold_product');
}
});
Function in script include :

getSoldProduct: function() {
var installBaseId = this.getParameter('sysparm_install_base');
if (!installBaseId) return '';

var gr = new GlideRecord('sn_install_base_m2m_installed_product');
gr.addQuery('install_base_item', installBaseId);
gr.setLimit(1);
gr.query();
if (gr.next()) {
return gr.getValue('sold_product');
}

return '';
},


Important to note that the Client script and script include should be in same scope for this to work