For Each Not Processing record When Removing the Record At End of Workflow

Hello Everyone. I have been pulling my hair out on this one trying to figure out why its behaving this way. The following workflow uses a for each to pull all the records from a table. I also have my Reference set up to link to it when obtaining all the values I need for when using the out of the box AD Create service type. It first checks to see if the AD record already exists, if it does it updates the status to COMPLETED in the main table and removes the record in the processing table (the one the For Each and Reference are looking at). If no AD record exists, it creates the AD record and does the same thing, marking the main table to COMPLETED and removing the processing record. If, however, I have a record that when creating the AD record has an error, I capture it (length(DistinguishedName) <= 0), and I mark the the main table record as ERROR and still remove the record from the process table. If I only have the UpdateMainTableSmartObject and take out the RemoveProcessRecordSmartObject, it works fine. It updates the status of the main table accordingly, and I get an email suggesting that there is an issue creating the AD account. However, as soon as I add in the SMO to remove the record, the workflow seems to completely ignore the path of an issue occuring. I do not receive an email, the main table status field does not get updated with ERROR and the other two valid AD accounts that were created are removed, but the one with an error remains. Can someone help me understand what is going on here?


Thanks for your help.




Best answer by YP 12 July 2017, 22:41

View original

5 replies



Images you have attached are not showing up. I had a different issue with for each loop and it seems to be it does not work properly every time. Finally, I used orocess fields to make it work, like old ways.



Thanks for responding. In case your having issues with the embedded pics I attached a Word document. When you say you use Process fields can you elaborate on that for me? Sorry, new to K2.






Sorry to reply after a long time.


The following analysis may help you.

1. Cutting the branch you are sitting on. You are iterating through the table and deleting the record at the end. It seems that it is messing with index in for an each loop and not iterating records properly.

2. I do not understand why you are updating the status at first place as any way you are going to delete them at the end of the workflow. 



If you want to remove the recodes, you can delete them after completing the for each loop. 

After for each loop use an activity with advance destination with smartobject event to delete records. I have attached word document which demonstrates how to do it.

Please give a Kudo if this helps you.

one more thing I would like to mention here. For each does not seems to be working properly if you update the recodes on which you are iterating.

This might help somebody who comes across similar forecah issue.

Thanks for the information YPawar. Much appreciated. I did exactly what you suggested. I ended up moving the SMO responsible for removing the records once the For Each was finished. The workflow uses two tables, one table is the one the For Each is looking at to process and from that update the Status in another table. My RemoveProcessRecordsSmartObjectEvent calls an Oracle procedure which looks at the two tables to compare the data and if it sees that the status was updated in the other table it goes ahead and removes all applicable process records. Probably a better way to do all this but it works. I am learning. Thanks again for your help!