background script

Community Alums
Not applicable

I'm having two tables table1 and table2, I have written script for insertion of record once I insert record in table1 automatically in table2 record should be create here is my script

var gr = new GlideRecord("u_mytable");
    gr.u_name = "abraham.lincoln";
    gr.u_mobile_number = "040-00282262";
    gr.insert();

    var grc = new GlideRecord("u_mytablecopy");
        grc.u_name = gr.u_name;
        grc.u_mobile_number = gr.u_mobile_number;
        grc.insert();
 
Now I want to use same case for deletion
2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums Here is how you should write your script.

var gr = new GlideRecord("u_mytable");
gr.addQuery('u_name',"abraham.lincoln");
if(gr.next()){
var grc = new GlideRecord("u_mytablecopy");
grc.addQuery('u_name',gr.getValue('u_name'));
if(grc.next()){
grc.delete();
}
gr.delete()
}       

Hope this helps.

View solution in original post

Manisha Reddy K
Mega Guru

Hi @Community Alums ,

    Please use gr.initialize() when your inserting the new record.

var gr = new GlideRecord("u_mytable");
     gr.initialize();
    gr.u_name = "abraham.lincoln";
    gr.u_mobile_number = "040-00282262";
    gr.insert();

 

    var grc = new GlideRecord("u_mytablecopy");
        grc.initialize();        
        grc.u_name = gr.u_name;
        grc.u_mobile_number = gr.u_mobile_number;
        grc.insert();
 
Deletion code:
---------------------------
var deleteRecord;
var gr = new GlideRecord("u_mytable");
gr.addQuery('sys_id', 'the sys_id of record to be deleted');
gr.query();
if(gr.next()){
deleteRecord = gr.getValue('sys_id');
gr.deleteRecord();
}
 
var grc = new GlideRecord("u_mytablecopy");
grc.addQuery('sys_id', deleteRecord);
grc.query();
if(grc.next()){
grc.deleteRecord();
}
 
 
 
 
 

View solution in original post

10 REPLIES 10

Community Alums
Not applicable

Thank you. It's working.

Manisha Reddy K
Mega Guru

Hi @Community Alums ,

    Please use gr.initialize() when your inserting the new record.

var gr = new GlideRecord("u_mytable");
     gr.initialize();
    gr.u_name = "abraham.lincoln";
    gr.u_mobile_number = "040-00282262";
    gr.insert();

 

    var grc = new GlideRecord("u_mytablecopy");
        grc.initialize();        
        grc.u_name = gr.u_name;
        grc.u_mobile_number = gr.u_mobile_number;
        grc.insert();
 
Deletion code:
---------------------------
var deleteRecord;
var gr = new GlideRecord("u_mytable");
gr.addQuery('sys_id', 'the sys_id of record to be deleted');
gr.query();
if(gr.next()){
deleteRecord = gr.getValue('sys_id');
gr.deleteRecord();
}
 
var grc = new GlideRecord("u_mytablecopy");
grc.addQuery('sys_id', deleteRecord);
grc.query();
if(grc.next()){
grc.deleteRecord();
}
 
 
 
 
 

Anand Kumar P
Giga Patron
Giga Patron

Hi @Community Alums ,
For deletion  you can write before delete BR on table 1 record when deleted.

 

    var grc = new GlideRecord("u_mytablecopy");
    grc.addQuery("u_name", current.u_name);//take unique field in table 1 to match with table 2
    grc.query();
if (grc.next()) {
    grc.deleteRecord(); 
}

 

For insertion you can write after BR on table 1 record when inserted.

 

 

   var grc = new GlideRecord("u_mytablecopy");
      grc.initlize()
        grc.u_name = current.u_name;
        grc.u_mobile_number = current.u_mobile_number;
        grc.insert();

 

 Please mark helpful if it works.
Thanks,

Anand

Slava Savitsky
Giga Sage

Writing code without having a clear design or even a well defined requirement will get you nowhere. Moreover, background scripts are only suitable for a very limited number of uses cases. Most of the time, there is a better alternative.

hatchhozzen
Tera Contributor

A "background script" typically refers to a script or program that runs in the background of a computer or system, performing tasks without a user interface or direct interaction. Background scripts are often used for automation, system maintenance, data processing, or other tasks roofing companies Auburn AL that don't require user intervention. Here's a simple example of a background script in Python that periodically prints a message:

pythonCopy code
import time def background_task(): while True: print("Background script is running...") time.sleep(5) # Print the message every 5 seconds if __name__ == "__main__": background_task()

In this example, the script defines a function background_task that runs indefinitely, printing a message every 5 seconds. This script can be executed in the background, and the user won't interact with it directly.

Keep in mind that more complex background scripts can perform tasks like data analysis, file processing, server maintenance, and much more. The key is that they operate independently, often without a graphical user interface, to carry out specific functions........