How to access field IDs by looping

How to access field IDs by looping

Anonymous
Not applicable
996 Views
5 Replies
Message 1 of 6

How to access field IDs by looping

Anonymous
Not applicable

I'm trying to clear a large number of fields. What is the proper syntax to create a loop so I don't have to individually type each field ID?

 

For Example:

item.B1_NOTIFICATION_1 = null;

item.B1_NOTIFICATION_2 = null;

item.B1_NOTIFICATION_3 = null;

 

item.B2_NOTIFICATION_1 = null;

item.B2_NOTIFICATION_2 = null;

item.B2_NOTIFICATION_3 = null;

 

I would want to create a loop where I replace the number.

for (var i=1; i<=10; i++){

  item.B1_NOTIFICATION_[i] = null;

}

0 Likes
Accepted solutions (1)
997 Views
5 Replies
Replies (5)
Message 2 of 6

philipfrench
Collaborator
Collaborator
Accepted solution

Try one of these. The first is a double loop to build up your field name.

The second is to loop through every field in the item, and test whether that field id contains a string eg _NOTIFICATION_.

 

Which approach is best depends on whether you have other fields containing _NOTIFICATION_ that you don't want to set to null.

 

IF you find there are excessive entries in your Change Log (things being set to null when they are already null), then wrap your setting to null, with something like this:

if (item[f]!==null && f.indexOf('_NOTIFICATION_')>0) item[f]=null; // only set to null if not already null.

 

p3.png

Message 3 of 6

Anonymous
Not applicable
Awesome, thank you!
0 Likes
Message 4 of 6

Anonymous
Not applicable

@Anonymous Is it possible to do the same to access items from another workspace? And for creating variables?

 

I have something like:

var SUP1 = item.SUP1_SUPPLIER_1;  // Link to 1st item in another workspace

var SUP2 = item.SUP2_SUPPLIER_1;  // Link to 2nd item

 

SUP1.TOOL_COST_1

SUP1.TOOL_COST_2

SUP1.TOOL_COST_3

 

SUP2.TOOL_COST_1

SUP2.TOOL_COST_2

SUP2.TOOL_COST_3

 

I wasn't sure how to do the variable, so I tried  item['SUP'+i]['TOOL_COST_'+j] But that didn't work

 

 

0 Likes
Message 5 of 6

sven.dickmans
Autodesk
Autodesk

Yes, this also works when accessing items of other workspaces - this should work to set the first value of the first supplier:

item['SUP' + i + '_SUPPLIER_1']['TOOL_COST_' + j]

0 Likes
Message 6 of 6

philipfrench
Collaborator
Collaborator

As Sven says, you can indeed refer to the other item, and read/write properties from it.

Each item is treated as an associative array (=map, hashtable, dictionary... in other languages).

 

I'm not exactly sure what you are trying to do. The following simple example imagines you are trying to loop through fields in the item, searching for fields starting with SUP, containing _SUPPLIER and with a value. Adjust this logic as your naming convention needs.

 

Note indexOf() tests whether the string exists, and its position. So indexOf() === 0 means "starts with".

 

 

Then we take the other item, and store it as temp variable (line 7).

 

Then I loop over the fields in the other item (using f2) and for those fields starting with TOOL_COST_ and having a value, I am just adding their value to subTotal.

 

Line 14 is example how to set some imaginary field (TOOLING_COST) in the other item to this subtotal.

 

Finally line 19 is setting some imaginary field on the original item.

 

This is trivial example, but hopefully shows all the things you might want to do when navigating the items.

 

This is flexible coding. But code should be understandable by your colleagues too.

 

If you are more comfortable writing indiviaul lines of code refering to fields explicitly... even if you have to adjust that code when the fields change... that is perfectly OK too.

 

total_example.jpg

0 Likes