UI Action to create a case task when clicked ok.

SK41
Giga Guru

Hi,

 

I have a requirement where I have to display a confirm message when user clicks on the UI action "Add". When user clicks a message should appear " Do you need to create a task?". If the user clicks "ok" then it should cancel the normal workflow and manually create a task. The UI Action will be on "Financial services base[ sn_bom_case] table and on click of "ok", it should create a case task in [sn_bom_financial_task] table.

 

I am able to create a popup message, below is the code for that. Not sure how to create a task when click "ok". Need help there.

 

function changeToEdit(){ // this is function called on the "onClick" field

if(confirm(getMessage("Warning: It will override the normal case flow.Do you want to proceed?"))){



   // run Server Action



   gsftSubmit(null, g_form.getFormElement(), 'manual_exception'); // this refers to the "Action name"



 }
 else{
   return false; // cancel action if confirm response is false/cancel
 }
}

 

Kindly, assist how can I create a task through UI action when click "ok".

 

Thanks!

4 REPLIES 4

Allen Andreas
Administrator
Administrator

Hi,

From here, you'd want to considering using GlideRecord and initialize an empty record to then create your new task.

Examplehttps://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc... 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Danish Bhairag2
Tera Sage
Tera Sage

Hi @SK41 ,

 

Currently your UI action is Client checked which means only client side scripting will work. In order to create task u need server side scripting as u need to do a Glide Record. So in order to achieve that you can call a script include using Glide Ajax call n then try to create a task.

 

Sample example of calling script include from client side

var ga = new GlideAjax('asu_GetLocationData');
      ga.addParam('sysparm_name', 'getCampus');
      ga.addParam('sysparm_buildingid', g_form.getValue("u_building"));
      ga.getXML(updateCampus);

}
function updateCampus(response) {

      var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);

}

 

Thanks,

Danish

Shaqeel
Mega Sage

Hi @SK41 

 

Here is what I found regarding your requirement from one of my colleague.

 

1. Create a UI Action on the "Financial services base[sn_bom_case]" table.
- Navigate to the table by typing "sn_bom_case.list" in the filter navigator.
- Click on "New" to create a new UI Action.
- Fill in the necessary fields such as Name, Action name, etc. Make sure to select the "Client" checkbox to make this a client-side UI Action.

2. In the "Script" field of the UI Action, write the following JavaScript code:

if (confirm('Do you need to create a task?')) {
var gr = new GlideRecord('sn_bom_financial_task');
gr.initialize();
gr.short_description = 'Task created from UI Action';
gr.insert();
action.setRedirectURL(gr);
} else {
gs.addInfoMessage('Task creation cancelled');
action.setRedirectURL(current);
}

This script does the following:
- It displays a confirmation dialog with the message "Do you need to create a task?".
- If the user clicks "OK", it creates a new record in the "sn_bom_financial_task" table with a short description of "Task created from UI Action". It then redirects the user to the newly created task.
- If the user clicks "Cancel", it displays an informational message "Task creation cancelled" and redirects the user back to the current record.

3. Save the UI Action.

4. Test the UI Action by navigating to a record in the "sn_bom_case" table and clicking on the UI Action. You should see the confirmation dialog, and depending on your choice, a new task should be created or not.

Please note that this is a basic example and you might need to adjust the script to fit your exact requirements, such as setting additional fields on the task or handling errors.

 

Mark Helpful/Solution 🙂

 

Regards

Shaqeel


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel

This is not working, task is not getting created