Behaviors plugin in create screen for jira cloud

Sudarsana G
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 2, 2024
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);
}


 

1 answer

0 votes
Sean Chua _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 24, 2024

Hey @Sudarsana G ,

I assume you are trying to set or restrict the country field, and the unique company portion is working? If this is wrong understanding, let me know.

My version will use "Location"..

I didn't check if they are not part of jira-users, so just directly assess group name like "TEST_CompanyB-Canada", which is similar to yours:

const companyLocationPairs = groupNames
.filter(name => name.startsWith('TEST_'))
.map(name => {
const parts = name.replace("TEST_", "").split('-');
return { company: parts[0], location: parts[1] };
});

Here is where there are 2 things which I don't know what you want to do.

1. If I assume you are trying set the value of country directly - you probably can add locations below uniqueCompany like:

const uniqueCompany = [...new Set(companyLocationPairs.map(pair => pair.company))][0];
const locations = companyLocationPairs.map(pair => pair.location);

then set the locations like:

const locationToSet = locations[0];

// map location/country to the context/option ID
const optionId = locationToOptionId[locationToSet];

if (optionId) {
await getFieldById("customfield_XXXXX").setValue(optionId); }

2. If I assume you are trying to restrict the visibility of selection to a set of countries (because of the setOptionsVisibility), then maybe you want to map a the logical groups of countries/locations based on criteria, maybe like:

const countryToGroup = {
"Canada": { "Canada" : "26202", "United States" : "26201"},
"Thailand": { "Thailand": "25966", "India" : "26203"} // or whatever grouping logic..

or just directly restrict everything else by adding the hide other options filter and set.OptionsVisibility(NotcountryOptionsIds, false), as you already have something to set what to show:

getFieldById("customfield_1334").setOptionsVisibility(countryOptionIds, true)

I hope I somehow make sense...if I don't make sense, open a Support Ticket with us and our support engineers can give a much better and clearer explanation  *hides the pain*.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events