I need help with (JS)

I need help with:
N subtables are automatically generated due to a certain field.
THANK

1 Like

If you want a certain JS to be executed to generate subtable entries, I suppose you can use Post-Workflow that check the “a certain field” value and determine whether or not to generate the subtable entries.

Thank you very much for your response!
I need to get “1002373” and add N sub-data
I tried using “Javascript Workflow Generator” but it failed
param.getOldValues(“<FIELD_ID>”); //Get field value N

//Add a new subtable data
var query = db.getAPIQuery(“/forms/26”);
var entry = query.getAPIEntry(“<RECORD_ID>”);

entry.setSubtableFieldValue(“<SUBTABLE_FIELD_ID>”, “<ROW_INDEX>”, “<NEW_VALUE>”);
// repeat this line of code for each subtable field
entry.setSubtableFieldValue(“<SUBTABLE_FIELD_ID>”, “<ROW_INDEX>”, “<NEW_VALUE>”);
entry.save();

Hi,
There’s something wrong with this code generated by the Javascript Workflow Generator.
It should be entry.setSubtableFieldValue(“<SUBTABLE_FIELD_ID>”, “<SUBTABLE_ROOT_NODE_ID>”, “<NEW_VALUE>”);
So you should write as following:
entry.setSubtableFieldValue(subtableFieldId_1, -1, newValue_1);
entry.setSubtableFieldValue(subtableFieldId_2, -1, newValue_2);

Use negative value on the <SUBTABLE_ROOT_NODE_ID> creates a new subtable entry.
But be aware that you should use different negative one to create another new subtable entry.
So if I want to create the second new entry, I should write:
entry.setSubtableFieldValue(subtableFieldId_1, -2, newValue_3);
entry.setSubtableFieldValue(subtableFieldId_2, -2, newValue_4);

Hi, Laiyou,

  1. You might need to provide further info on the scenario you are tackling.
    Does field 1002373 store numeric value of how many subtable entries to be created? Or… field: 1002373 stores some other text value that need to be determined?
    This would determine how you generate the subtable fields.

  2. The code generated by JS Workflow Generator is only a rough template. Have you subtituted and filled in all the related field info and values?
    Is the above example what you are currently running as the code? or you have the actual modified code?

Sorry, I didn’t explain it clearly. The above part is generated from “Javascript Workflow Generator” and I am modifying it.
The field “1002373” in the main table is a numerical value. After getting this ‘N’ value, add an ‘N’ pen subtable.
And sort the pen data from “1” to “N” in the serial number field “1002374”,

  • Key Field: 1002380
  • subtable key: 1002381

But currently I am trying to generate a new subtable and an error occurred

I am wondering what doe the ‘N’ pen subtable actually mean?
Are you only creating “empty” entries in the subtable, or you also need to insert any type of the value into the subtable?
What fields are in this subtable?

Showing the Error message and the related line of code might help clarify the issue.

For example: “1002373” field = 3 to get 3,
Added subform serial number field “1002374”
1
2
3

Hi,
you can try something like this to fill in No. 1~3 in subtable, if the N = 3.

image

function loadNItems(nodeId) {
  var query = db.getAPIQuery("/devtest/15");
  var entry = query.getAPIEntry(nodeId);
  
  //Get value from field "N" (id:1023116)
  var nValue = entry.getFieldValue("1023116");
  
  for(var i = 0;i < nValue; i++)
  {
    //Set value for subtable field "No" (id:1023117)
    entry.setSubtableFieldValue("1023117", -i-1, i+1);
  }
  
  entry.save();  
}

image

1 Like

Thank you very much for your help!
But I don’t know where I am doing wrong.
Can you take a look at it for me? I pressed save and it didn’t automatically say 1…2…3…

Did you use loadNItems({id}) in the JS Action button?

Thank you very much!! I added the button and it worked successfully after clicking it
But I’m sorry, what I want to do is to automatically run out N pieces of data after saving.
Don’t know if you can help me, thank you very much for your help!!

You can put similar code into Post-Workflow where it will run after the record is saved.
However, you might need to think about the scenario that this will be used, and how you want to design this workflow

For example, you might consider:

  1. Prevent it from running every time:
    You will need a way to determine whether it has been ran before. Otherwise, it will run every time the record is changed and saved.

  2. Can the user change the N value?
    What should happen when the N value is changed?
    … etc.

You can try something like this below:

var rootNodeId = param.getNewNodeId("1017702");
var query = db.getAPIQuery("/devtest/15");
var entry = query.getAPIEntry(rootNodeId);

//Get: Loaded N? field id: 1023232
var isLoaded = entry.getFieldValue("1023232");

if (isLoaded !== 'Yes') {
  //Get value from field "N" (id:1023116)
  var nValue = entry.getFieldValue("1023116");
  
  for(var i = 0;i < nValue; i++)
  {
    //Set value for subtable field "No" (id:1023117)
    entry.setSubtableFieldValue("1023117", -i-1, i+1);
  }
}

//Set: Loaded N? field id: 1023232 = Yes
entry.setFieldValue("1023232","Yes");

entry.save();

image

OK, thank you so much for your help!
I succeeded, I will try again with the changes~
Thank you very much for your help^_^

You are welcome.

1 Like