Description: Sample Groovy Script to provide default values to Custom Attributes when creating a Placeholder. The script dynamically sets default values based on the folder structure and logged-in user details. It also controls whether the Custom Attributes are editable or read-only.
Context: Document
Document Type: Placeholder
Remark: Accessible in On-load task mode only.
Script:
def execute(){
/**************** Configurable Parameter Section *******************************************************/
String customAttributeNameProject = "Project";
String customAttributeNameOriginator = "Originator";
/******************************************************************************************************/
JsonArray attributeDetailsList = new JsonArray();
DocumentVO document = placeholderService.getPlaceholderDetails();
String selectedProject = "";
/**********************Project Attribute script****************************************************/
// Get Folder Path of current revision
String folderPath;
if(document.getFileFolderStructure() == null ||document.getFileFolderStructure().isEmpty()) {
folderPath = placeholderService.getFolderPath(document);
}else {
folderPath = document.getFileFolderStructure();
}
//Extract the root folder name from the current folder name and assign it to the selectedProject variable
selectedProject = folderPath.substring(0, folderPath.indexOf("\\"));
// Option 1: Set Project custom attribute value to the selected project and make it read only
attributeDetailsList.add(getAttributeJSON(customAttributeNameProject, IDocumentAttribute.CUSTOM, IDocumentAttribute.READ_ONLY, selectedProject));
// Option 2: Set Project custom attribute value to the selected project and make it editable
//attributeDetailsList.add(getAttributeJSON(customAttributeNameProject, IDocumentAttribute.CUSTOM, IDocumentAttribute.EDITABLE, selectedProject));
/**********************Originator Attribute script*************************************************/
// retrieve the details of the logged-in user.
UserVO user = placeholderService.getCurrentUserDetails();
// retrieve the name of the logged-in user organisation name.
String publisherOrganisationName = user.getOrgName();
// Option 1: if the organisation name in the Originator custom attribute does not match precisely the organisation's name in the cloud, then uncomment the following code.
if (publisherOrganisationName.toLowerCase() == "Client Org 01".toLowerCase())
publisherOrganisationName = "CO1 - Client Org 01";
//else if (publisherOrganisationName.toLowerCase() == "Client Org 02".toLowerCase())
// publisherOrganisationName = "CO2 - Client Org 02";
// else if (publisherOrganisationName.toLowerCase() == "CO3 - Client Org 03".toLowerCase())
// publisherOrganisationName = "CO3 - Client Org 03";
else if (publisherOrganisationName.toLowerCase() == "Subcontractor Org 01".toLowerCase())
publisherOrganisationName = "SC1 - Subcontractor Org 01";
// else if (publisherOrganisationName.toLowerCase() == "Subcontractor Org 02".toLowerCase())
// publisherOrganisationName = "SC2 - Subcontractor Org 02";
else if (publisherOrganisationName.toLowerCase() == "Subcontractor Org 03".toLowerCase())
publisherOrganisationName = "SC3 - Subcontractor Org 03";
else if (publisherOrganisationName.toLowerCase() == "Subcontractor Org 04".toLowerCase())
publisherOrganisationName = "SC4 - Subcontractor Org 04";
// Option 2: Set Originator to the logged-in user organisation and make is editable
attributeDetailsList.add(getAttributeJSON(customAttributeNameOriginator, IDocumentAttribute.CUSTOM, IDocumentAttribute.EDITABLE, publisherOrganisationName));
// Option 3: Set Originator to the logged-in user organisation and make is read only
//attributeDetailsList.add(getAttributeJSON(customAttributeNameOriginator, IDocumentAttribute.CUSTOM, IDocumentAttribute.READ_ONLY, publisherOrganisationName));
document.setResponseStatus(false);
document.setDynamicObject(attributeDetailsList.toString());
return document;
}
// This is a generic method to prepare JSON object. No need to do any change in this method.
private JsonObject getAttributeJSON(String attributeName, boolean isCustomAttribute, boolean editOption, String defaultValue) {
JsonObject attribute = new JsonObject();
attribute.addProperty("attributeName", attributeName);
attribute.addProperty("isEditable", editOption);
attribute.addProperty("isCustomAttribute", isCustomAttribute);
attribute.addProperty("defaultValue", defaultValue);
return attribute;
}