get the function name

User370547
Giga Contributor

Hello

When I write scripts (business rules, script includes (classes and functions), functions) I try to add the name to the log output.

like:

function add_sec_sc_relation_function()

{   gs.log('add_sec_sc_relation_function ' + 'start');

}

Then I can filter to the syslog  table with this string.  (Best way to debug).

Is there a way to get the function name without hard coding ?  like

gs.log(get_name_of_function() +  ' start');

Many thanks.

 

Sincerely Detlef Biedermann

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you cannot get the current function name

best way is to print logs at start of the function and end of function to ensure the script within function ran fine

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you cannot get the current function name

best way is to print logs at start of the function and end of function to ensure the script within function ran fine

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

asifnoor
Kilo Patron

Hi,

you can use arguments object and can fetch the name by using some string manipulations. 

Check this link https://www.geeksforgeeks.org/how-to-get-currently-running-function-name-using-javascript/ This might give you some ideas.

Mark the comment as a correct answer and helpful if this helps to solve the problem.

User370547
Giga Contributor

Hello

 

I have tried in "Script Background"

function getFuncName()
{   
    return getFuncName.caller.name;
}
function teddy() {
    gs.info(getFuncName());
}

teddy();

 

 

Result:

*** Script: undefined

Sincerely Detlef Biedermann

Did you try arguments.callee.toString()

This returns the source. Check the below link. This might give you some ideas

Ref: https://stackoverflow.com/questions/1935521/javascript-arguments-callee-tostring-and-arguments-calle...