My intention is to make using behaviors plugin if user is not part of Jira-users group and part of CPC_Like-Canada group then it has to separate and display only those values in the custom filed Like as a CP custom filed and Canada in country custom filed I am not sure where i am doing mistake but its displaying rest of the values which it should not In the logs its separated and displayed
Event: On Load
Message: uniqueCompany=Like | countries=Thailand
Level: info
View: Create view
// Asynchronously fetch the current user's data
const user = await makeRequest("/rest/api/2/myself");
const { accountId } = user.body;
// Fetch user's group memberships
const getGroups = await makeRequest(`/rest/api/2/user/groups?accountId=${accountId}`);
const groupNames = getGroups.body.map(({ name }) => name);
// Check if user is a member of 'jira-users'
const isJiraUserGroupMember = groupNames.includes('jira-users');
if (!isJiraUserGroupMember) {
// Extract companies and countries from group names
const companyCountryPairs = groupNames
.filter(name => name.includes('CPC_'))
.map(name => {
const parts = name.replace("CPC_","").split('-');
return { company: parts[0], country: parts[1] };
});
// Assuming unique company names and preparing country names for field restriction
const uniqueCompany = [...new Set(companyCountryPairs.map(pair => pair.company))][0]; // Assumes user belongs to one company
const countries = companyCountryPairs.map(pair => pair.country);
logger.info("uniqueCompany=" + uniqueCompany + " | countries=" + countries)
// Function to set the Company field
function setCompanyField(company) {
getFieldById("customfield_2335").setValue(company);
}
// Function to restrict Country field options
function restrictCountryFieldOptions(countries) {
const allCountryOptionIds = {
"Thailand": "25966", //
"Saudi Arabia": "26110",
"Kuwait": "26114",
"Qatar": "26115",
"Canada" : "26202",
"India" : "26203",
"South Africa" : "25965",
"United States" : "26201",
};
const countryOptionIds = countries.map(country => allCountryOptionIds[country]).filter(id => id);
logger.info("countryOptionIds=" + countryOptionIds)
getFieldById("customfield_1334").setOptionsVisibility(countryOptionIds, true);
}
// Set the company field based on user's group
setCompanyField(uniqueCompany);
// Restrict the options in the country field based on user's groups
restrictCountryFieldOptions(countries);
}