Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Getting values from fields dynamically

ravish2
Giga Expert

I am writing a business rule which need to get data from 31 fields. All the fields name are in similar pattern For Example:

day1, day2 ,day3, day4.........day31

So what i want to achieve here is to get the data from all these fields without writing gr.day1, gr.day2 etc instead i want something like

for (var i=1;i<=31;i++){

gs.log(gr."day"+i )     // Of course this will not work but something like this. i also tried using getLabel/Name but i haven't got any success

}

Thanks

3 REPLIES 3

dvp
Mega Sage

by using   setValue method you can achieve this



here is the sample code



temp = 'day'+i;



gr.setValue(temp)


Hi dvp



Thanks a lot it worked. Now if this fields is a reference field and i want to go down one level below this field how to achieve that. For Example


I want to drill down to hours also



temp = 'day'+i;


temp1 = 'hours'+j;



gr.setValue(temp.temp1)   // this code is not working is there any alternative to this


Dominik Simunek
Tera Guru

Hi Ravish,



In Javascript you can retrieve values from an object by two ways - dot notation or by wrapping the name in [ ] brackets. E.g. this code executed as background script gets the department sys_id and then the last row the name of the department.



var gr = new GlideRecord('sys_user');


gr.get(gs.getUserID());


gs.print('Department sys_id = ' + gr["department"]);


gs.print('Department name = ' + gr["department"]["name"]);



In similar way, you could use something like: gr[dayVar] to get value of day variable and to get values from day object gr[dayVarName][minVarName]. Notice that in my example above the var name are strings, however it can be also dynamically defined like variable and called like:


var gr = new GlideRecord('sys_user');


gr.get(gs.getUserID());


var departmentVarName = "department";


gs.print('Department sys_id = ' + gr[departmentVarName]);


gs.print('Department name = ' + gr[departmentVarName]["name"]);



Best regards,


Dominik