want to make state field editable till one month after ticket is closed only for two users

siva14
Tera Contributor

"we want to make state field editable till one month after ticket is closed only for two users" in incident form, anyone please help how to achieve this 

 

#client script #ACL #business rule

6 REPLIES 6

Akshay Gupta2
Kilo Sage

Hi @siva14 ,

 

Yes, you're right. Creating a single Write ACL with a script to check the user's permissions and the conditions on the incident record can achieve the desired behavior effectively. Here's a detailed guide on how to implement this:

 

 Steps to Create the Write ACL:

 

1. Create the Write ACL on the Incident Table

a. Navigate to **System Security > Access Control (ACL)** and click on **New**.
b. Configure the ACL as follows:
- **Type**: `record`
- **Operation**: `write`
- **Name**: `incident`
- **Condition**: [Leave empty or use a simple condition if needed]

2. Set the Condition:

- In the **Condition** builder, set a basic condition like `State is Closed` if you want to enforce this ACL only on closed incidents.

3. Advanced Script:

- Click on the **Advanced** checkbox to enable scripting.

- In the script section, add the following script:

 

 

// Define the sys_ids of the users who should have write access
var allowedUsers = ['user_sys_id_1', 'user_sys_id_2']; // Replace with actual sys_ids

// Function to check if the current user is allowed
function isUserAllowed() {
return allowedUsers.indexOf(gs.getUserID()) !== -1;
}

// Function to check if the incident is within one month of closure
function isWithinOneMonthOfClosure(current) {
var closedAt = current.closed_at;
if (!closedAt) {
return false;
}

var oneMonthLater = new GlideDateTime(closedAt);
oneMonthLater.addMonths(1);
var now = new GlideDateTime();

return now.compareTo(oneMonthLater) <= 0;
}

// Check both conditions
if (isUserAllowed() && isWithinOneMonthOfClosure(current)) {
answer = true;
} else {
answer = false;
}

 



Explanation:

1. **User Check:
- `allowedUsers` contains the sys_ids of the users who should have the write permission.
- `isUserAllowed()` checks if the current user's sys_id is in the list of allowed users.

2. Time Check:
- isWithinOneMonthOfClosure(current) checks if the current incident's `closed_at` date is within one month from now.

3. Condition Evaluation:
- The script returns `true` for `answer` if both conditions are met, making the incident editable for the specified users within one month of closure. Otherwise, it returns `false`, making the incident read-only.

 

Please mark this as helpful 😉

 

Thanks

Akshay

Hi @Akshay Gupta2 , it's working, thanks for the help