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.

Change company based on "Requested for" in requested item table

dubey887766
Mega Contributor

Hi Folks,

Good Morning !
Can someone please let me know, how can I change the value for company filed, based on user selected for "Requested for" in requested item table.

Please note, Requested for, field is OOTB field which is coming from request (sc_request) table on sc_req_item table. This field is like(request.requested_for) on request item(sc_req_item) table.

I have written below onChange client script on requested item table on but it is not working.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

          return;

    }

    //Type appropriate comment here, and begin script below

var ab = g_form.getReference('requested_for', setRFC);

function setRFC(ab)

{

g_form.setValue('company',ab.company);

}

Any assistance will be appreciated.

Thank you,

NAveen Dubey      

4 REPLIES 4

Gaurav Bajaj
Mega Sage

Hi Naveen,



Try below line



var ab = g_form.getReference('request.requested_for', setRFC);



This should work.




Please mark the response correct/helpful based on the impact.




Thanks


Gaurav


Hi Gaurav,



Thanks for your response !
I tried your code but it is not working. so I removed request.requested_for field from sc_req_item table and added requested for(u_requested) field which belongs to sc_req_item table itself.




Now I want whenever I change value for u_requested field on requested item table, it make a copy of same value for request.requested_for field on sc_request table for selected request.


I wrote below after BR on requested item table but it is not working.


Can you please see if you can help with below:



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




// Add your code here



var gr = new GlideRecord('sc_request');



alert('I am here');


gr.addQuery('sys_id',çurrent.request);


gr.addQuery();


while(gr.next()){


//gr.requested_for = current.request.requested_for;


gr.requested_for = current.u_requested;


alert('I am here1');


gr.update();


}


alert('I am here2');




})(current, previous);



Thank you,


Naveen Dubey


bubuprasadswain
Tera Guru


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    /*if (isLoading || newValue == '') {


          return;*/


  if (newValue == '') {


          g_form.setValue('company', '');


          return;


    }




    if (!g_form.getControl('company'))


          return;




    var caller = g_form.getReference('request.requested_for', setCompany);


}




function setCompany(caller) {


    if (caller)


            g_form.setValue('company', caller.company);


    }












Can put like above.



Thanks & Regards,


Bubuprasad


Hi Bubuprasad,



Thanks for your response !
However above solution is not working.





Thank you,


Naveen Dubey