I want to set read-only for a component in a UI builder client script.

mochildbel2
Tera Contributor

I have added a Checkbox component and an Attachment component in the UI builder.
In the client script, I want to control the following, but it does not work.

 

【Action】

・Set Attachment's read-only setting to False when the checkbox is checked.
・When the checkbox is unchecked, set Attachment's read-only setting to True.

 

I created the following script, but nothing happened, so I would like to know if anyone has done something similar.

 

function handler({api, event, helpers, imports}) {
    const isChecked = event.target.value;
  
    const attachmentComponentId = 'attachment_1';
 
    if (isChecked) {  
        api.updateComponent(attachmentComponentId, { readOnly: false });  
    } else {  
        api.updateComponent(attachmentComponentId, { readOnly: true });  
    }  
}
1 REPLY 1

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You can't directly manipulate a component from code. You'd want to create a boolean client state parameter and bind it to the isReadOnly property on the attachment component. Then your script can set the CSP to true or false and that will be reflected in the component.