Workflow to update a custom field when either of two other custom fields are updated.

Hello, I am not a developer, I do not know enough of javaScript to be able to code my own workflows but I have to start somewhere. I appreciate your patience.

I am trying to get the following use cases to work, and here are the custom fields that I have created. 

1. Salesforce Contacts - Integer, default value 0
2. Forum Contacts - Integer, default value 0
3. Grand Total - Integer, default value 0

Use Case 1: 
Upon creation of an issue, the value of either Salesforce Contacts or Forum Contacts will be set to 1 manually by the author.
How do I get a workflow to automatically update the Grand Total to update to show the sum of the Salesforce Contacts and Forum Contacts given that the default value of both fields are 0. i.e. The author will set the Saleforce Contacts to 1 on creation, and the Forum Contacts is by default set to 0 or vice versa.

Use Case 2: 
Upon updating an issue, the updater is expected to add 1 to either the Saleforce Contacts or Forum Contacts field.
How do I get a workflow to automatically update the Grand Total to automatically update, and show the new sum of Salesforce Contacts and Forum Contacts? 

3 comments
Comment actions Permalink

Hello,

Thank you for contacting YouTrack community forums. Please find some hints below:

1. Your workflow needs to fire on updating either Salesforce Contacts or Forum Contacts field. You can find a corresponding example of how to fire on the field update here: https://www.jetbrains.com/help/youtrack/devportal/Workflow-Assignee-Visibility-Group.html#warn-assignee. The rule logic should take both fields' values and put the sum in the Grand Total field. To assign a new value to the field, please use ctx.issue.fields.<fieldname> = <new value>. You can find an example of assigning new values to the fields here: https://www.jetbrains.com/help/youtrack/devportal/Workflow-Pomodoro-Timer.html#pomodoro-states

2. Do you want to force the users somehow to add 1 to these fields? Or do you just want to recalculate the sum? If the latter, the logic is the same as in p.1. The workflow should fire on changing the Salesforce Contacts or Forum Contacts fields and recalculate the Grand Total field. 

For example, the following sample of code will fire on changing issue type to Epic, calculate the sum of the Importancy and the Story points fields and put it into the Importancy field.

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');

exports.rule = entities.Issue.onChange({
  title: 'check deleted',
guard: function(ctx) {
  return ctx.issue.fields.becomes(ctx.Type, ctx.Type.Epic);
  },
  action: function(ctx) {
  var issue = ctx.issue;
  ctx.issue.fields.Importancy += ctx.issue.fields['Story points'];
  },
requirements: {
    Type: {
      type: entities.EnumField.fieldType,
      Epic: {
        name: 'Epic'
      },
      Bug: {
        name: 'Bug'
      },
      Task: {
        name: 'Task'
      }
  }
  }
});

Please let me know if it helps. 

0
Comment actions Permalink

I have gotten the following to work, there is just one minor problem in the following use case.
If an updater makes a mistake of updating the wrong field, for example, updated the Salesforce Contacts instead of Forum Contacts, and changes it back to zero, the Grand Total Value does not change. For any other value but zero, the Grand Total computes normally. 

Example, 

If I update the custom field SF Contacts to 1 and leave the Forum Contacts at 0, the Total Contacts  updates correctly to 1.

If I revert the custom field SF Contacts to 0, and leave the Forum Contacts at 0, The Total Contacts stays at 1. 

 

var entities = require('@jetbrains/youtrack-scripting-api/entities');

exports.rule = entities.Issue.onChange({
   title: 'Compute Grand Total',
  guard: (ctx) => {
    return ctx.issue.fields.isChanged(ctx.SFContacts) && ctx.issue.fields.SFContacts || ctx.issue.fields.isChanged(ctx.ELHContacts) && ctx.issue.fields.ELHContacts;
  },
  action: (ctx) => {
    var issue = ctx.issue;
    var sfNum = ctx.issue.fields.SFContacts;
    var elhNum = ctx.issue.fields.ELHContacts;
    var grandNum = sfNum + elhNum;
    ctx.issue.fields.TotalContacts = grandNum;
  },
  requirements: {
    SFContacts: {
        type: entities.Field.integerType,},
    ELHContacts: {
          type: entities.Field.integerType,},
    TotalContacts: {
          type: entities.Field.integerType,},
  }
});
0
Comment actions Permalink

Hi,

I believe the root cause is that the workflow checks if the ctx.issue.fields.SFContacts and ctx.issue.fields.ELHContacts fields are set. In this case, the rule doesn't trigger when the field is reset (set to 0). Could you please try to modify your guard condition to the following and let me know if it helps?

return ctx.issue.fields.isChanged(ctx.SFContacts) || ctx.issue.fields.isChanged(ctx.ELHContacts);
0

Please sign in to leave a comment.