What's the best way to auto populate a field dependent on another field?

nmcl
Giga Expert

I'd like to implement something where by if a certain priority is set (e.g. P1) that the 'Major Incident' template is inserted.

So that the description and short description fields are populated with template text.

 

What's the best way to go about implementing this?

15 REPLIES 15

I've tried using the below client script but it's applying to all priorities, not just those listed.


It also resets the priority field to 'none' even though this is configured within the template.



Any ideas?



---script---



function onChange(control, oldValue, newValue, isLoading) {


if (isLoading || newValue == '') {


return;


}


if(newValue == 6^7^8){


var grTemplate = new GlideRecord('sys_template');


grTemplate.addQuery('name','* Major Incident Template');


grTemplate.query(templateResponse);


}




function templateResponse(template) {


if (template.next()) {


applyTemplate(template.sys_id);


}


}


}


Thanks Marc.   On trying this script I get the below error...



JavaScript parse error at line (1) column (18) problem = missing ; before statement


WARNING at line 1: Missing semicolon.

ERROR at line 1: Expected to see a statement and instead saw a block.


Hi Marc, you can ignore my last message - I was missing the first line of script.



I now have the below but get an error when testing...



---script---


function onChange(control, oldValue, newValue, isLoading){


  if (newValue == 6){


          applyTemplate(ca1f35d52bcb61006601997e17da1522);


}


}



---error---


onChange script error: ReferenceError: 'ca1f35d52bcb61006601997e17da1522' is undefined function onChange_incident_priority_5(control, oldValue, newValue, isLoading){ if (newValue == 6){ applyTemplate(ca1f35d52bcb61006601997e17da1522); } }


put single quotes around the template SID


nmcl
Giga Expert

I've updated this script to work only for the required priorities. However it's still blanking out the priority field. I also wanted to add a line that it will only apply if the priority was previously --none--.



function onChange(control, oldValue, newValue, isLoading) {


if (isLoading || newValue == '') {


return;


}


if(newValue == 6 || newValue == 7 || newValue == 8){


var grTemplate = new GlideRecord('sys_template');


grTemplate.addQuery('name','* Major Incident Template');


grTemplate.query(templateResponse);


}



function templateResponse(template) {


if (template.next()) {


applyTemplate(template.sys_id);


}


}


}