Clarification needed: Copying values of a field hidden behind a condition

I have two types of mutually exclusive orders, Sales Orders and Work Orders, that I want to have separate IDs for. I current have set up Sales Order ID (integer field) and Work Order ID (integer field) to conditionally show up behind a Order Type (enum field) that allows a user to select between a Sales or Work Order.

I want to preserve the Sales or Work Order ID whenever a copy of an issue is made. However, I'm finding that the conditional field is cleared on copy while the Order Type seems to be preserved. The documentation says:

If a custom field is hidden behind a condition, you cannot set the value for the field with a workflow. If you use a workflow to manage the values in a conditional custom field, make sure that your workflows don't update fields that are hidden. You can customize the workflow to check for the condition before the change is applied.

The sentence makes me think what I want to do is not possible. The second sentence implies this is possible so long as the the conditional field's requirements to be shown are met. Example: Issue A has Order Type "Work" which shows Work Order ID "123". Copying Issue A to Issue B has Order Type "Work" which shows a blank Work Order ID.

This is the Javascript workflow I wrote to try to get around this limitation. Please bear with my code. It is exported workflow constructor code that I modified using snippets from stackoverflow and reading the documentation. 

  action: function(ctx) {
    const logger = new Logger(ctx.traceEnabled);


    // --- #1 Issue.copy and make copy a subtask of original ---
    logger.log("Running scripts for the \"Copy the issue\" block");
    const issue_1 = ctx.issue;
    const project_0 = ctx.issue.project;
    let copyIssueResult_0;
    
    const order_type = issue_1.fields.OrderType.name;
    console.log(`Order type is ${order_type}`);
        
    const SO_num = issue_1.fields.SO;
    const WO_num = issue_1.fields.WO;
    console.log(`WO # is ${WO_num}`);

    const IssuecopyFn_0 = () => {
      if (issue_1 === null || issue_1 === undefined) throw new Error('Block #1 (Copy the issue): "Issue" has no value');
      if (project_0 === null || project_0 === undefined) throw new Error('Block #1 (Copy the issue): "Project" has no value');
      
      copyIssueResult_0 = issue_1.copy(project_0);
      
      copyIssueResult_0.fields.OrderType = order_type;
      console.log(`Copy order type is ${copyIssueResult_0.fields.OrderType}`);      

      if (order_type === 'Sales') {
        copyIssueResult_0.fields.SO = SO_num;
      } else {
        copyIssueResult_0.fields.WO = WO_num;
      }
      console.log(`Copy WO # is ${copyIssueResult_0.fields.WO}`);
      
      copyIssueResult_0.links['subtask of'].add(issue_1);
    };

    IssuecopyFn_0();

 

The console output suggests to me that copying the Work Order ID was successful but it remains blank on the front end. 

vlau@ 24 Mar 2022 06:22 PM
Order type is Work
vlau@ Mar 2022 06:22 PM
WO # is 123
vlau@ 24 Mar 2022 06:22 PM
Copy order type is Work
vlau@ Mar 2022 06:22 PM
Copy WO # is 123

 

Help understanding the issue would be appreciated. 

0
3 comments

Hi!

What does console.log(ctx.WO.isVisibleInIssue(ctx.issue)) show? 

0

> What does console.log(ctx.WO.isVisibleInIssue(ctx.issue)) show? 

It output: true

0

Vlau

I'm terribly sorry for the radio silence here.

The correct code should be as follows:

copyIssueResult_0.fields[ctx.OrderType.name] = order_type;   
      if (order_type === 'Sales') {
       copyIssueResult_0.fields[ctx.SO.name] = SO_num;        
     } else {
        copyIssueResult_0.fields[ctx.WO.name]= WO_num;
     }
0

Please sign in to leave a comment.