Trying to apply project template in UI action

michaelsharp
Mega Expert

I am trying to use a UI action (button) to apply a project template to an existing (blank) project.  

I created a reference field "u_project_template" on the project form that allows the user to select the appropriate template from the Project Template form.

The UI action include a client & server side script.   The pop-up confirmation works, the field that I set in the server side script works and the info message appears at the end with the correct variable values (project template sys_id and project template name).   However, the template does not apply.   (No project tasks and no other field values are set as they are defined in the project template)

I've looked and looked and found other references to applying a template via the applyTemplate() function, but I can't get this to work with project templates.   Any help would be appreciated.

Thanks

Here's the script that I've been working on:

-----SCRIPT BEGIN-----

//client-side 'apply_click'function

function apply_click(){

if(confirm('This action will apply the template specified in the Project Template field.   Please confirm to proceed') == false){

return false;   //Abort submission

}

//call the UI action and skip the click function

gsftSubmit(null, g_form.getFormElement(),'apply_template'); //must call the action name above

}

//Code that runs without the click function

//Ensure there are no errors on submission

if(typeof window == 'undefined')

runApplyTemplate();

//Server-side function

function runApplyTemplate(){

(function executeRule(current, previous /*null when async*/) {

var tname = current.u_project_template.name;

var tsysid = current.u_project_template.sys_id;

var tdate = gs.getDateTime();

var url;

current.u_rank = "4";

current.applyTemplate('tname');

gs.addInfoMessage("Template "+tname+" applied "+tsysid);

current.update();

url = current.getTableName() + '.do?sys_id=' + current.sys_id.toString() + '&sysparm_view=';

action.setRedirectURL(url);

})(current, previous);

}

-----SCRIPT END-----

1 ACCEPTED SOLUTION

I should've read your original post carefully. I think applying Project Templates is different than a regular Template listed on the sys_template table.



This is a shot in the dark, but you could try the code below. The ProjectWorkbenchService Script Include appears to have a method that might meet your needs.



//client-side 'apply_click'function


function apply_click() {



      if (confirm('This action will apply the template specified in the Project Template field.   Please confirm to proceed') == false) {


              return false; //Abort submission


      }



      //call the UI action and skip the click function



      gsftSubmit(null, g_form.getFormElement(), 'apply_template'); //must call the action name above


}



//Code that runs without the click function



//Ensure there are no errors on submission



if (typeof window == 'undefined')


      runApplyTemplate();



//Server-side function


function runApplyTemplate() {


      (function executeRule(current, previous /*null when async*/ ) {



              var tname = current.u_project_template.name;


              var tsysid = current.u_project_template.sys_id;


              var tdate = gs.getDateTime();


              var url;


              current.u_rank = "4";


              new ProjectWorkbenchService().applyTemplateForProject(tsysid, current.sys_id, current.start_date);


              gs.addInfoMessage("Template " + tname + " applied " + tsysid);



              current.update();



              url = current.getTableName() + '.do?sys_id=' + current.sys_id.toString() + '&sysparm_view=';


              action.setRedirectURL(url);



      })(current, previous);


}


View solution in original post

5 REPLIES 5

Justin Abbott
Giga Guru

Where you're applying the template, shouldn't the tname variable not be in quotes? Check out line 37 below.



//client-side 'apply_click'function


function apply_click() {




      if (confirm('This action will apply the template specified in the Project Template field.   Please confirm to proceed') == false) {


              return false; //Abort submission


      }




      //call the UI action and skip the click function




      gsftSubmit(null, g_form.getFormElement(), 'apply_template'); //must call the action name above


}




//Code that runs without the click function




//Ensure there are no errors on submission




if (typeof window == 'undefined')


      runApplyTemplate();




//Server-side function


function runApplyTemplate() {


      (function executeRule(current, previous /*null when async*/ ) {




              var tname = current.u_project_template.name;


              var tsysid = current.u_project_template.sys_id;


              var tdate = gs.getDateTime();


              var url;


              current.u_rank = "4";


              current.applyTemplate(tname);


              gs.addInfoMessage("Template " + tname + " applied " + tsysid);




              current.update();




              url = current.getTableName() + '.do?sys_id=' + current.sys_id.toString() + '&sysparm_view=';


              action.setRedirectURL(url);




      })(current, previous);




}


It doesn't work with or without quotes.   I've been playing with different syntax to try and figure this out to no avail.



I'm not a developer, but have hacked my way into several other UI actions/scripts, etc, but this one evades me.


I should've read your original post carefully. I think applying Project Templates is different than a regular Template listed on the sys_template table.



This is a shot in the dark, but you could try the code below. The ProjectWorkbenchService Script Include appears to have a method that might meet your needs.



//client-side 'apply_click'function


function apply_click() {



      if (confirm('This action will apply the template specified in the Project Template field.   Please confirm to proceed') == false) {


              return false; //Abort submission


      }



      //call the UI action and skip the click function



      gsftSubmit(null, g_form.getFormElement(), 'apply_template'); //must call the action name above


}



//Code that runs without the click function



//Ensure there are no errors on submission



if (typeof window == 'undefined')


      runApplyTemplate();



//Server-side function


function runApplyTemplate() {


      (function executeRule(current, previous /*null when async*/ ) {



              var tname = current.u_project_template.name;


              var tsysid = current.u_project_template.sys_id;


              var tdate = gs.getDateTime();


              var url;


              current.u_rank = "4";


              new ProjectWorkbenchService().applyTemplateForProject(tsysid, current.sys_id, current.start_date);


              gs.addInfoMessage("Template " + tname + " applied " + tsysid);



              current.update();



              url = current.getTableName() + '.do?sys_id=' + current.sys_id.toString() + '&sysparm_view=';


              action.setRedirectURL(url);



      })(current, previous);


}


Justin, that did it!   Thank you so much!