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.

Issue with recursive function

caffry
Kilo Guru

Trying to call a same function until the value is found. But when the value is found its returning undefined.

var user=new GlideRecord("sys_user");
user.get("1328d77f97ea45104c913b6e6253af14");
checkmanageradbusinessunit(user.u_business_unit,user.manager)


function checkadbusinessunit(businessunit, manager)
{
     
     if(businessunit == "")
     {
          gs.print(checkmanagerbusinessunit(manager));
     }
}

 function checkmanagerbusinessunit(manager) {
        gs.print("check manager")
        var user = new GlideRecord("sys_user");
        user.get(manager);
        if(user.u_business_unit == "")
        {
             gs.print("if");
             checkmanagerbusinessunit(user.manager);
        }
        else{
             
             var bu=user.u_business_unit;
gs.print(bu);
             return bu;
        }

    }

find_real_file.png

1 ACCEPTED SOLUTION

Subhanand
Tera Guru

Hi caffry,

declare the recursive function in a temp variable and check.

 

var user=new GlideRecord("sys_user");


user.get("1328d77f97ea45104c913b6e6253af14");
checkmanageradbusinessunit(user.u_business_unit,user.manager)


function checkadbusinessunit(businessunit, manager)
{

if(businessunit == "")
{
gs.print(checkmanagerbusinessunit(manager));
}
}

function checkmanagerbusinessunit(manager) {
gs.print("check manager")
var user = new GlideRecord("sys_user");
user.get(manager);
if(user.u_business_unit == "")
{
gs.print("if");
var temp;
temp=checkmanagerbusinessunit(user.manager);
}
else{

var bu=user.u_business_unit;
gs.print(bu);
return bu;
}

}

 

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@caffry 

check this link for recursive function solution

https://community.servicenow.com/community?id=community_question&sys_id=c01f6d68db734c941cd8a345ca96...

Regards
Ankur

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

Hi Ankur,

I dont see much differences on the script.

its similar to the one which i wrote but still something is failing.

Akif_Shah
Mega Sage

Can you try changing the last 2 lines to 

var bu= user.u_business_unit.toString();
return bu.toString();

 

Still it didnt work