Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to add a Scope for Specific Project

Vitheya Monikha May 9, 2025

Hi ,

 

I’ve created custom field which is a dropdown.  So, for that I am adding the options dynamically using the below APIs

API URL'S 

1. /rest/api/3/field/${fieldId}/context

2. /rest/api/3/field/${fieldId}/context/${globalContext.id}/option

 

The options are added successfully, but it is not project specific. The options are visible under all the projects under my account. So i need a way to apply project scope for each option I create.

Any guidance or insights from the community on how to proceed functionality it would be greatly appreciated.

I’ve attached the relevant code snippet below.

 

resolver.define("addOptionToCustomField", async({payload}) => {

  const { fieldId, name } = payload
  try{

    const contextResponse = await api.asApp().requestJira(route`/rest/api/3/field/${fieldId}/context`, {
      method: "GET",
      headers: { "Accept": "application/json" },
    });
    const contextData = await contextResponse.json();

    const globalContext = contextData.values.find(ctx => ctx.isGlobalContext);

    const optionData = [{ value: name }];

    var res = await api.asApp().requestJira(route`/rest/api/3/field/${fieldId}/context/${globalContext.id}/option`, {
        method: "POST",
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
        },
        body: JSON.stringify({options: optionData}),
    });
      const data = await res.json()
   
      return data
  }
  catch(error){
    return {"code": 2000, "message": "Error while creating folder"}
  }

});


Thanks in advance for your help!

 

1 answer

1 vote
Jaime Escribano
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 9, 2025

Good Morning Vitheya!

 

I can see in this code snipet

const globalContext = contextData.values.find(ctx => ctx.isGlobalContext);

that you chose the global context of the field, which is the reason why it shows up in all projects.

You would need to define a new context for each set of projects you want your field to have different options in.

 

const targetContext = contextData.values.find(ctx => ctx.name === "specific Context"); 

 

You can read more about contexts here:  

https://support.atlassian.com/jira-cloud-administration/docs/configure-custom-field-context/

 

Hope that was helpful. Let me know if you need more help! 

 

Regards,

Jaime Escribano

Vitheya Monikha May 12, 2025

Hi Jaime,

 

As you Suggested I've set Specific context but still its fail, I've attached the code snippet below kindly take a look.

Code Snippet

resolver.define("addOptionToCustomField", async({payload}) => {
  const { fieldId, name, projectKey } = payload

  try{
    const contextResponse = await api.asApp().requestJira(route`/rest/api/3/field/${fieldId}/context`, {
      method: "GET",
      headers: { "Accept": "application/json" },
    });
    const contextData = await contextResponse.json();
  const targetContext  = contextData.values.find(ctx => ctx.name === projectKey);
   
    const optionData = [{ value: name }];
    var res = await
api.asApp().requestJira(route`
/rest/api/3/field/${fieldId}/context/${targetContext}/option`, {
        method: "POST",
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
        },
        body: JSON.stringify({options: optionData}),
    });
      const data = await res.json()
      return data
  }
  catch(error){
    return {"code": 2000, "message": "Error while creating folder"}
  }

});

 

Jaime Escribano
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 12, 2025

Good morning Vitheya! Hope you enjoyed your weekend.

 

Before I look into this, would you share what the failure is? API Error, result is not the same as expected, some other issue?

 

Regards,

Jaime

Vitheya Monikha May 12, 2025

Hi Jaime,

 

This is error I'm getting

"Error while creating folderTypeError: Cannot read properties of undefined (reading '0')"

I've  also attached UI code for your reference kindly take a look

Code snippet 1:

 const onSubmit = async (data) => {
      var bodyData;
      setIsLoading(true)
      const folderExists = tree.some(folder => folder.value === data.folderName || folder.value === `${data.folderName}(${parentFolderName})`);
      if (folderExists) {
         setIsLoading(false)
         setFlagTitle('Failure!');
         setFlagMessage('Error: Folder name already exists');
         addFlag();
         return;
      }
      if (parentFolderName === 'Test Repository') {
   

         bodyData = {
            name: data.folderName,
            fieldId: testCaseFieldId,
            projectKey: projectKey,
            projectId: projectId
           
         }
      }
      else {
         console.log("else..........", projectId)
         bodyData = {
            name: `${data.folderName}(${parentFolderName})`,
            fieldId: testCaseFieldId,
            projectKey: projectKey,
            projectId: projectId
           
         }
      }

      try {
         const response = await invoke('addOptionToCustomField', bodyData); // API call to get projects
       
         if (response.options[0].id) {
            setIsLoading(false)
            setFlagTitle("Success!")
            setFlagMessage('Folder created successfully')
            setIsOpen(false)
            setShowParentDrp(false);
            reset();
            addFlag();
            setTree([])
            setFolderLoader(true)
            getUpdatedComponentList()
         }

      } catch (error) {
         setIsLoading(false)
         setFlagTitle('Failure!')
         setFlagMessage(`Error while creating folder${error}`)
         addFlag();
         return [];
      }
   }

Code snippet 2:

resolver.define("addOptionToCustomField", async({payload}) => {

  const { fieldId, name, projectKey } = payload


  try{
    console.log("Payload to create options...", fieldId)
    const contextResponse = await api.asApp().requestJira(route`/rest/api/3/field/${fieldId}/context`, {
      method: "GET",
      headers: { "Accept": "application/json" },
    });
    const contextData = await contextResponse.json();

    const targetContext  = contextData.values.find(ctx => ctx.name === projectKey);
   
    const optionData = [{ value: name }];
 
    var res = await api.asApp().requestJira(route`/rest/api/3/field/${fieldId}/context/${targetContext}/option`, {
        method: "POST",
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
        },
        body: JSON.stringify({options: optionData}),
    });
      const data = await res.json()

      return data
  }
  catch(error){
    return {"code": 2000, "message": "Error while creating folder"}
  }

});

 

Vitheya Monikha May 15, 2025

Hi ,

 

How can I set scope for a specific project? In case you haven't seen the code snippets, I've sent them again. Please take a look at them

UI Code 

 const onSubmit = async (data) => {
      var bodyData;
      setIsLoading(true)
      const folderExists = tree.some(folder => folder.value === data.folderName || folder.value === `${data.folderName}(${parentFolderName})`);
      if (folderExists) {
         setIsLoading(false)
         setFlagTitle('Failure!');
         setFlagMessage('Error: Folder name already exists');
         addFlag();
         return;
      }
      console.log("ParentfoldernameID..........", projectId)
      if (parentFolderName === 'Test Repository') {
         console.log("ParentfoldernameID..........", projectId)

         bodyData = {
            name: data.folderName,
            fieldId: testCaseFieldId,
            projectKey: projectKey,
            projectId: projectId
           
         }
         console.log("bodyDataID..........", projectId)
      }
      else {
         console.log("else..........", projectId)
         bodyData = {
            name: `${data.folderName}(${parentFolderName})`,
            fieldId: testCaseFieldId,
            projectKey: projectKey,
            projectId: projectId
           
         }
         console.log("projectid..........", projectId)
      }

      console.log("Data...", bodyData)
      try {
         const response = await invoke('addOptionToCustomField', bodyData); // API call to get projects
         console.log("comp Response....", response)
         if (response.options[0].id) {
            setIsLoading(false)
            setFlagTitle("Success!")
            setFlagMessage('Folder created successfully')
            setIsOpen(false)
            setShowParentDrp(false);
            reset();
            addFlag();
            setTree([])
            setFolderLoader(true)
            getUpdatedComponentList()
         }

      } catch (error) {
         setIsLoading(false)
         setFlagTitle('Failure!')
         setFlagMessage(`Error while creating folder${error}`)
         addFlag();
         return [];
      }
   }


Backend Code

 

resolver.define("addOptionToCustomField", async({payload}) => {
  console.log("addOptionToCustomField")
  const { fieldId, name, projectKey } = payload

  console.log("projectkey.........",projectKey)
  try{
    console.log("Payload to create options...", fieldId)
    const contextResponse = await api.asApp().requestJira(route`/rest/api/3/field/${fieldId}/context`, {
      method: "GET",
      headers: { "Accept": "application/json" },
    });
    const contextData = await contextResponse.json();
    console.log("contextData......", contextData)
    const targetContext  = contextData.values.find(ctx => ctx.name === projectKey);
   
    const optionData = [{ value: name }];
    console.log("Option Data........", optionData)
    var res = await api.asApp().requestJira(route`/rest/api/3/field/${fieldId}/context/${targetContext}/option`, {
        method: "POST",
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
        },
        body: JSON.stringify({options: optionData}),
    });
      const data = await res.json()
      console.log(`✅ Dropdown options added for ${fieldId}!...`, data);
      return data
  }
  catch(error){
    return {"code": 2000, "message": "Error while creating folder"}
  }

});

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events