Unable to find Custom Field's configuration on useProductContext()

I would like to display the unit of measure of a field in both issue-edit and issue-view screens. This unit should be previously set on Field Configuration screen, so my idea is to create a new CustomFieldType and define a CustomFieldContextConfig like this:

import ForgeUI, {useProductContext, CustomFieldContextConfig, TextField} from "@forge/ui";

export const ContextConfig = () => {
  const {extensionContext: {configuration = {}}} = useProductContext();

  const onSubmit = formData => {
    return {
      configuration: {
        unit: 'formData.unit',
      }
    };
  };

  return (
    <CustomFieldContextConfig onSubmit={onSubmit}>
      <TextField name="unit" label="Unit of measure to be used" defaultValue={configuration.unit}/>
    </CustomFieldContextConfig>
  );
};

These configurations are usually used to create validations on manifest.yaml (This useless example limits the input to only characters if unit defined is usd)

[...]
      validation:
        expression: value == null || configuration?.unit =='usd' && !!value.match("^[A-Za-z]+$")
        errorMessage: The value must consist only numbers
[...]

Now I want to display both unit & value on issue View, but I couldn’t find unit defined on Field context. I see you can get the previous value of configuration form using:

  const {extensionContext: {configuration = {}}} = useProductContext();

we can check value is stored

 {
[...]
  extensionContext: {
    fieldId: 'customfield_10058',
    contextId: 10158,
    configurationId: 10158,
    configuration: { unit: 'usd' },
    entryPoint: 'contextConfig',
    type: 'customFieldType'
  },
  license: undefined,
  moduleKey: 'custom-field-type-hello-world',
  environmentId: undefined,
  environmentType: undefined
}

but when I try to do the same from others screens, configuration seems to be not defined on extensionContext:

{                                       
[...]                                                   
  extensionContext: {                                                           
    fieldValue: null,                                                           
    fieldId: 'customfield_10058',                                               
    fieldName: 'Cost',                                                          
    renderContext: 'issue-view',                                                
    type: 'customFieldType'                                                     
  },                                                                            
  license: undefined,                                                           
  moduleKey: 'section-field-type-hello-world',                                  
  environmentId: undefined,                                                     
  environmentType: undefined                                                    
}  

Is there any other way to obtain custom field configuration from these screens?

1 Like

Hello, @AgustinBarreto,

In contexts where configuration is not readily available, you can fetch in manually using the Get custom field configurations REST API.

2 Likes

Hi @kkercz ,

Thanks for your quick response. Using the rest api I can get all configuration contexts, but how can I get which one should be used? With multiples configurations how can I identify the one which context matches?

Is there any way to get only the desired configuration, or maybe get field context id to identify which one is the correct one ?