Need help with Background script

SD29
Tera Expert

Hi All,

On Our demand management, while creating the demands we populate the Business unit(u_business_unit) with the Department on the User record based on the submitted by field on the demand. Recently we have removed all the departments from our environment within snow and imported a new list of departments, which unfortunately removed the Business unit from Demands.

Is there a way to populate the Business unit field with the department of the Submitted by user record for the demands where Business unit field is empty?

Somekind of background script or fix script

Please help me with some sample script.

Thanks,

Sd

1 ACCEPTED SOLUTION

Patrick Fedigan
Giga Guru

Hello,



Here is an sample script that will hopefully help.



//call function


updateBusinessUnit();



function updateBusinessUnit() {


    //query records where business_unit is empty


    var dem = new GlideRecord('demands');//demand table


    dem.addQuery('u_business_unit', '');


    dem.query();



    //Iterate through records and set the business_unit field


    while(dem.next()) {


        dem.u_business_unit = dem.submitted_by.department;


        dem.update();


    }


}




Cheers,


View solution in original post

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

Yes. You can try below script



var ta = new GlideRecord('demand table name'); //replace with your demand table name


ta.addNullQuery('u_business_unit');


ta.query();



while (ta.next())


{


ta.u_business_unit= ta.submittedUser.department; //replace with you user and department field


ta.update();


}



Please mark this response as correct or helpful if it assisted you with your question.

Patrick Fedigan
Giga Guru

Hello,



Here is an sample script that will hopefully help.



//call function


updateBusinessUnit();



function updateBusinessUnit() {


    //query records where business_unit is empty


    var dem = new GlideRecord('demands');//demand table


    dem.addQuery('u_business_unit', '');


    dem.query();



    //Iterate through records and set the business_unit field


    while(dem.next()) {


        dem.u_business_unit = dem.submitted_by.department;


        dem.update();


    }


}




Cheers,


Gopal Harbola1
Tera Expert

Hi snow123,



//Script background


var dmn = new GlideRecord('dmn_demand');//demand table  


dmn.addQuery('u_business_unit', '');  


dmn.query();  


//Iterate through records and set the business_unit field  


while(dmn.next()) {


      //Assign department from User as a Business Unit for demand form


      dmn.u_business_unit = dmn.submitted_by.department;  


      dmn.update();  


}



I hope this helps 🙂


- Gopal Dutt Harbola


PS: Hit like, Helpful or Correct depending on the impact of the response