building a URL in a UI Action script

patricklatella
Mega Sage

Hi all,

I've got a UI Action for a "Copy Form" button that will enable a user to open a new record producer that has several field entries copied from the original record.

I am having trouble getting the URL string to build properly, and I've isolated to the line in the script below in bold.   What it's doing is insert the content "ID" not only where it should be, as indicated in the script, but also to after each gr.value as well.   See the screen shot for example.   Now oddly (or a clue perhaps), is that it's not adding after the very last variable in the filter search.   Anyone see what might be causing this?

____________________________

var ID;

switch (current.template.toString()){

case 'db5f551b4fca6e0089c7029d0210c7d9'://sys_id of Contract template

ID = '3e4c5d974fca6e0089c7029d0210c79d';//sys_id of Contracts record producer

break;

case '7481334c4f3db60089c7029d0210c72d'://sys_id of Cost Accounting template

ID = '3891bb4c4f3db60089c7029d0210c725';//sys_id of Cost Accounting record producer

break;

case 'b7fe73444f7db60089c7029d0210c72a'://sys_id of Business Development template

ID = '400f73444f7db60089c7029d0210c72c';//sys_id of BD PM record producer

break;

}

var url;

url = '/com.glideapp.servicecatalog_cat_item_view.do?sysparm_initial=true&sysparm_id=';//url to build string upon

var gr = new GlideRecord("question_answer");//table to query

gr.addQuery('table_sys_id',current.sys_id);//field to query

gr.query();//execute the query

while (gr.next()){

if (gr.question.name == 'request_type' || gr.question.name == 'responsible_person' || gr.question.name == 'pfo_employee' || gr.question.name == 'customer_type' || gr.question.name == 'project_type')

url = url+ID+'&sysparm_'+gr.question.name+'='+gr.value;//this is the line that I have traced the problem to

}

gs.addInfoMessage('+++My URL is '+url);//this here for testing purposes

action.setRedirectURL(url);

____________________________

and here is a screen shot of the URL this generates, and you can see the ID var being inserted after every gr.value except the last one.

find_real_file.png

1 ACCEPTED SOLUTION

patricklatella
Mega Sage

got it!   it was what I suspect and I needed to add a "&" to the end of the url var to get the URL to build properly so my catalog client scripts could parse the info.



var ID;



switch (current.template.toString()){


case 'db5f551b4fca6e0089c7029d0210c7d9'://sys_id of Contract template


ID = '3e4c5d974fca6e0089c7029d0210c79d';//sys_id of Contracts record producer


break;



case '7481334c4f3db60089c7029d0210c72d'://sys_id of Cost Accounting template


ID = '3891bb4c4f3db60089c7029d0210c725';//sys_id of Cost Accounting record producer


break;



case 'b7fe73444f7db60089c7029d0210c72a'://sys_id of Business Development template


ID = '400f73444f7db60089c7029d0210c72c';//sys_id of BD PM record producer


break;


}



var url;


url = '/com.glideapp.servicecatalog_cat_item_view.do?sysparm_initial=true&sysparm_id=';//url to build string upon



var gr = new GlideRecord("question_answer");//table to query


gr.addQuery('table_sys_id',current.sys_id);//field to query


gr.query();//execute the query




while (gr.next()){


if (gr.question.name == 'request_type' || gr.question.name == 'responsible_person' || gr.question.name == 'pfo_employee' || gr.question.name == 'customer_type' || gr.question.name == 'project_type')



url = url+ID+'&sysparm_'+gr.question.name+'='+gr.value+"&";//this is the line that was key to fixing this!



}



gs.addInfoMessage('+++My URL is '+url);//this here for testing purposes


action.setRedirectURL(url);


View solution in original post

3 REPLIES 3

patricklatella
Mega Sage

one note, it does put the var "ID" where it should be initially after "initial=true&sysparm_id=...", but you see it also adding later in the URL and I can't see why it's doing that.


patricklatella
Mega Sage

I think I've figured out a little part of the what's going wrong.   The URL is not being built with the var "ID" being inserted AFTER the "gr.value", it's more like it's being inserted BEFORE each "&sysparm_", and its doing it each time there is a "while" condition being met.   That's why it's not doing it on the very last value in the URL



So now I need to figure out to make the "while" loop do it's job, and then have it added to the "url = url+ID" after it's done.



any thoughts?


patricklatella
Mega Sage

got it!   it was what I suspect and I needed to add a "&" to the end of the url var to get the URL to build properly so my catalog client scripts could parse the info.



var ID;



switch (current.template.toString()){


case 'db5f551b4fca6e0089c7029d0210c7d9'://sys_id of Contract template


ID = '3e4c5d974fca6e0089c7029d0210c79d';//sys_id of Contracts record producer


break;



case '7481334c4f3db60089c7029d0210c72d'://sys_id of Cost Accounting template


ID = '3891bb4c4f3db60089c7029d0210c725';//sys_id of Cost Accounting record producer


break;



case 'b7fe73444f7db60089c7029d0210c72a'://sys_id of Business Development template


ID = '400f73444f7db60089c7029d0210c72c';//sys_id of BD PM record producer


break;


}



var url;


url = '/com.glideapp.servicecatalog_cat_item_view.do?sysparm_initial=true&sysparm_id=';//url to build string upon



var gr = new GlideRecord("question_answer");//table to query


gr.addQuery('table_sys_id',current.sys_id);//field to query


gr.query();//execute the query




while (gr.next()){


if (gr.question.name == 'request_type' || gr.question.name == 'responsible_person' || gr.question.name == 'pfo_employee' || gr.question.name == 'customer_type' || gr.question.name == 'project_type')



url = url+ID+'&sysparm_'+gr.question.name+'='+gr.value+"&";//this is the line that was key to fixing this!



}



gs.addInfoMessage('+++My URL is '+url);//this here for testing purposes


action.setRedirectURL(url);