Script Action is not working

Pratiksha Lang1
Kilo Sage

I have an event triggered from UI action followed by script action, but the script action code is not working. 

event and script action both is getting triggered but the script action code is not working. Please find the code below:

 

Script Action:

 

var sourceSite = event.parm1;
var destinationSite = event.parm2;
 
importAll(sourceSite,destinationSite);
 
function importAll(sourceSite,destinationSite){
 
var targetTable = new GlideRecord('x_amspi_smdrs_app_olas');
targetTable.addQuery('site',current.source_site);
targetTable.query();
while (targetTable.next()) {
gs.info('Inside the script action second');
    var targetTable1 = new GlideRecord('x_amspi_smdrs_app_olas');
    targetTable1.addQuery('ola_name', targetTable.ola_name);
    targetTable1.addQuery('frequency_ola', targetTable.frequency_ola);
    targetTable1.addQuery('function_ola', targetTable.function_ola);
    targetTable1.addQuery('sub_function_ola', targetTable.sub_function_ola);
    targetTable1.addQuery('formula_ola', targetTable.formula_ola);
    targetTable1.addQuery('ola_type', targetTable.ola_type);
    // targetTable1.addQuery('status', targetTable.status);
    targetTable1.addQuery('role', targetTable.role);
    // targetTable1.addQuery('artefacts', targetTable.artefacts);
    targetTable1.addQuery('site',current.destination_site);
    targetTable1.query();
 
    if (targetTable1.next()) {
gs.info('Inside the script action third');
        gs.addErrorMessage('Record already exists in ola table: ' + targetTable1.ola_name);
 
    } else {
gs.info('Inside the script action fourth');
         targetTable1.initialize();
         targetTable1.ola_name = targetTable.ola_name;
         targetTable1.frequency_ola = targetTable.frequency_ola;
         targetTable1.function_ola = targetTable.function_ola;
         targetTable1.sub_function_ola = targetTable.sub_function_ola;
         targetTable1.formula_ola = targetTable.formula_ola;
         targetTable1.ola_type = targetTable.ola_type;
         targetTable1.status = targetTable.status;
         targetTable1.role = targetTable.role;
         targetTable1.artefacts = targetTable.artefacts;
         targetTable1.site = current.destination_site;
         targetTable1.insert();
         gs.addInfoMessage('Record Inserted Successfully');
    }
}
}
 
 
 
UI Action :
 
var source = current.getValue('source_site');
var destination = current.getValue('destination_site');
gs.eventQueue("x_amspi_smdrs_app.smdrs.import", null, source, destination);
11 REPLIES 11

sushantmalsure
Mega Sage
Mega Sage

Hello 

I think you have used current in your script action function importAll().

 

var sourceSite = event.parm1;
var destinationSite = event.parm2;
 
importAll(sourceSite,destinationSite);
 
function importAll(sourceSite,destinationSite){
 
var targetTable = new GlideRecord('x_amspi_smdrs_app_olas');
targetTable.addQuery('site',sourceSite);
targetTable.query();
while (targetTable.next()) {
gs.info('Inside the script action second');
    var targetTable1 = new GlideRecord('x_amspi_smdrs_app_olas');
    targetTable1.addQuery('ola_name', targetTable.ola_name);
    targetTable1.addQuery('frequency_ola', targetTable.frequency_ola);
    targetTable1.addQuery('function_ola', targetTable.function_ola);
    targetTable1.addQuery('sub_function_ola', targetTable.sub_function_ola);
    targetTable1.addQuery('formula_ola', targetTable.formula_ola);
    targetTable1.addQuery('ola_type', targetTable.ola_type);
    // targetTable1.addQuery('status', targetTable.status);
    targetTable1.addQuery('role', targetTable.role);
    // targetTable1.addQuery('artefacts', targetTable.artefacts);
    targetTable1.addQuery('site',destinationSite);
    targetTable1.query();
 
    if (targetTable1.next()) {
gs.info('Inside the script action third');
        gs.addErrorMessage('Record already exists in ola table: ' + targetTable1.ola_name);
 
    } else {
gs.info('Inside the script action fourth');
         targetTable1.initialize();
         targetTable1.ola_name = targetTable.ola_name;
         targetTable1.frequency_ola = targetTable.frequency_ola;
         targetTable1.function_ola = targetTable.function_ola;
         targetTable1.sub_function_ola = targetTable.sub_function_ola;
         targetTable1.formula_ola = targetTable.formula_ola;
         targetTable1.ola_type = targetTable.ola_type;
         targetTable1.status = targetTable.status;
         targetTable1.role = targetTable.role;
         targetTable1.artefacts = targetTable.artefacts;
         targetTable1.site = destinationSite;
         targetTable1.insert();
         gs.addInfoMessage('Record Inserted Successfully');
    }
}
}

 

 

try above, I have removed current and used paramter values directly. 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

@sushantmalsure I tried with parameter values also but still it is not working.

when you say its not working then what does that mean?

Can you put more logs at multiple places to see execution length and post it here ?

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

@sushantmalsure I have given multiple logs. info messages and error messages. find the code below: 

The only log I am getting is  - Inside the script action second. 

By not working what I mean is that if record already exist then I have given a error message which it should show and if doesn't exist then it should show message as record inserted successfully.

 

gs.info('Inside the script action');
var sourceSite = event.parm1;
var destinationSite = event.parm2;
 
importAll(sourceSite,destinationSite);
 
function importAll(sourceSite,destinationSite){
 
var targetTable = new GlideRecord('x_amspi_smdrs_app_olas');
targetTable.addQuery('site',sourceSite);
targetTable.query();
gs.info('Inside the script action first');
while (targetTable.next()) {
gs.info('Inside the script action second');
    var targetTable1 = new GlideRecord('x_amspi_smdrs_app_olas');
    targetTable1.addQuery('ola_name', targetTable.ola_name);
    targetTable1.addQuery('frequency_ola', targetTable.frequency_ola);
    targetTable1.addQuery('function_ola', targetTable.function_ola);
    targetTable1.addQuery('sub_function_ola', targetTable.sub_function_ola);
    targetTable1.addQuery('formula_ola', targetTable.formula_ola);
    targetTable1.addQuery('ola_type', targetTable.ola_type);
    // targetTable1.addQuery('status', targetTable.status);
    targetTable1.addQuery('role', targetTable.role);
    // targetTable1.addQuery('artefacts', targetTable.artefacts);
    targetTable1.addQuery('site',destinationSite);
    targetTable1.query();
gs.info('Inside the script action third');
    if (targetTable1.next()) {
gs.info('Inside the script action fourth');
        gs.addErrorMessage('Record already exists in ola table: ' + targetTable1.ola_name);
 
    } else {
gs.info('Inside the script action fifth');
         targetTable1.initialize();
         targetTable1.ola_name = targetTable.ola_name;
         targetTable1.frequency_ola = targetTable.frequency_ola;
         targetTable1.function_ola = targetTable.function_ola;
         targetTable1.sub_function_ola = targetTable.sub_function_ola;
         targetTable1.formula_ola = targetTable.formula_ola;
         targetTable1.ola_type = targetTable.ola_type;
         targetTable1.status = targetTable.status;
         targetTable1.role = targetTable.role;
         targetTable1.artefacts = targetTable.artefacts;
         targetTable1.site = destinationSite;
         targetTable1.insert();
gs.info('Inside the script action sixth');
         gs.addInfoMessage('Record Inserted Successfully');
    }
}
}