creation of task record for users

Vedavalli
Tera Expert

Hi

I have a button on a particular table and I have users field in that form when I click on that , it should create records for users in task table and state should be changed to complete.

2 REPLIES 2

Maddysunil
Kilo Sage

@Vedavalli 

You can create a UI action button(client checkbox true) to run it on client side, and call the script include for task creation:

below is the both code:

UI action:

 

createTasksAndComplete();
function createTasksAndComplete() {
    var user = g_form.getValue('user_field_name'); // Replace 'user_field_name' with the actual name of your user field
    var ga = new GlideAjax('YourScriptIncludeName'); // Replace 'YourScriptIncludeName' with the name of your script include
    ga.addParam('sysparm_name', 'createTasksAndComplete');
    ga.addParam('sysparm_user', user);
    ga.getXMLAnswer(function(response) {
        if (response == 'success') {
            alert('Tasks created successfully.');
            // Reload the form or take other actions as needed
        } else {
            alert('Error: Unable to create tasks.');
        }
    });
}

 

Script Include:

 

var YourScriptIncludeName = Class.create();
YourScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    createTasksAndComplete: function() {
        var user = this.getParameter('sysparm_user');
        // Logic to create tasks for each user and update state to "complete"
        // Example:
        var taskGr = new GlideRecord('task_table_name');
        taskGr.initialize();
        taskGr.setValue('assigned_to', user);
        taskGr.setValue('state', 'complete');
        var taskId = taskGr.insert();

        if (taskId) {
            return 'success';
        } else {
            return 'error';
        }
    }
});

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

M Ismail
Tera Guru

Hi @Vedavalli,

try this 

Here's a detailed code snippet for creating a client callable UI action that creates tasks for users specified in a field on the form:

1. **UI Action Configuration**:
- Navigate to `System UI > UI Actions`.
- Click on **New** to create a new UI action.
- Configure the UI action with the following settings:
- Name: Enter a name for your UI action, e.g., "Create Tasks for Users".
- Table: Specify the table where you want to add the UI action.
- Type: Choose the appropriate type, such as 'Button'.
- Active: Check this box to activate the UI action.
- Client: Check this box to make the UI action client callable.
- Action name: You can leave this blank or specify a unique identifier.
- Onclick: Enter the JavaScript function that will be called when the UI action button is clicked.

2. **Client-Side Script**:
- Write a JavaScript function to be executed when the UI action button is clicked. This function will create tasks for users specified in a field on the form.
- The script retrieves the value of the user field, splits it into an array of user IDs, and iterates over each user to create a task for them.

```javascript
function createTasksForUsers() {
var userField = g_form.getValue('user_field_name'); // Replace 'user_field_name' with the actual name of your user field
var users = userField.toString().split(','); // Assuming user field is a comma-separated list of user IDs

users.forEach(function(user) {
var taskGr = new GlideRecord('task');
taskGr.initialize();
taskGr.setValue('assigned_to', user);
taskGr.setValue('state', 'complete'); // Assuming the state should be set to 'complete'
taskGr.insert();
});

alert('Tasks created successfully.');
// You can optionally reload the form or perform other actions here
}
```

3. **Attach Client Script to UI Action**:
- In the UI action configuration, set the `Onclick` field to `createTasksForUsers()`. This associates the UI action with the client-side script function.

4. **Field Configuration**:
- Ensure that you have a field on the form named `'user_field_name'` which contains a comma-separated list of user IDs.

With this setup, when a user clicks the UI action button on the form, the `createTasksForUsers()` function will be invoked. It will create tasks for each user specified in the `'user_field_name'` field and set their state to 'complete'. Finally, it will display an alert message confirming that the tasks were created successfully.

Please hit helpful and accept this as a solution if it solved your problem.
Thank you!