I have an action script that runs when an item's details are modified. I also have macros set up in SolidWorks that will create items (via the API). However, I just realized that PLM 360 doesn't run the "new item" script when the SolidWorks macros creates a new item. I looked in the documentation, but as far as I can tell, there isn't a documented means of using the API to run an action script. Is there a means of doing this, or do action scripts only get run in response to user actions ignoring API actions?
I'm considering possible workarounds, but I'm not sure what my options are. Any ideas for how I might be able to use a workflow item to modify an item's details when it gets created and every time it gets modified thereafter?
Solved! Go to Solution.
Solved by Tim.Lewis. Go to Solution.
Hi Tim,
An oncreate and onEdit script behaviors run in context of the createItem and editItem transactions, regardless how those items are created ; UI, API, or spawned via another script. the API call will fail if there is a failure in the associated script. If you are getting a successful resopnse from the API, it would mean that the script (if any) had alrady ran. Can you check few things please for completeness sake:
- that there is onCreate script to that workspace.
- that the action actually does something to confirm it ran succesfully.
Also share some insight what are you trying to achieve so we may look at this together. Let me know soon please,
@gasevsm, thanks for your help.
My SolidWorks macros seem to be creating the item just fine (i.e. the item doesn't exist before they're run, and afterwards it does exist with all the information that is supposed to be there). However, as far as I can tell, the action script is not getting executed. None of the actions it is supposed to perform are being done, and there are not any errors showing in the script editor. Furthermore, if I run the action script manually after the part has been added by the API call in SolidWorks, the action script runs fine without any errors and produces the expected result.
I have attached a screencast demonstrating me running my macros and then subsequently running the script manually to perform the actions that need to be done after the SW macros run.
Thanks for posting the steps. My recommendation is to try Postman, I would also try using another PLM script to spawn the item (as if created from API) so that you can debug it all within PLM to see what transpires once script is run. Last bet is contact ADN or PLM Support for I cannot reproduce using basic Postman createItem with onCreate script that creates dummy rows in grid tab; also tested making changes across multiple tabs of the API created item via same script with success -- The API responds with 201 whose response payload reflects my script changes on item details. I suspect the script logic is entering some codeblock that makes it 'do nothing' - likely dependincy on some field. Whereas the onDemand script finds what it condition relies on and runs as expected. Feel free to share your onCreate script in private for review.
I forgot to mention that I have checked the API response from running my macros, and it is indeed 201.
The script I'm using is nothing special, so I don't mind sharing it here:
item.SOURCE_DESCRIPTOR = '<a href="' + item.SOURCE_URL + '">' + item.SUPPLIER_PART + ' - ' + item.SUPPLIER + '</a>'; if ( item.TOTAL_PART_COST > 0 && item.UPDATE_SOURCING === 'Yes' ) { if (item.sourcing.length > 0) { var num_quotes = item.sourcing[0].quote.length; var quotes = []; for (var ind = 0; ind < num_quotes; ind++) { quotes[ind] = [item.sourcing[0].quote[ind].min, item.sourcing[0].quote[ind].max, item.sourcing[0].quote[ind].leadTime * item.sourcing[0].quote[ind].leadTimeType, item.sourcing[0].quote[ind].unitCost, item.sourcing[0].quote[ind].comment]; } } item.sourcing.clear(); item.sourcing.addSourcing({ SUPPLIER:item.SUPPLIER, SUPPLIER_PART_NUM:item.SUPPLIER_PART, MANUFACTURER:item.MANUFACTURER, MANUFACTURER_PART_NUM:item.MANUFACTURER_PART, CUSTOM_FIELDS:{ SOURCE_URL:item.SOURCE_URL } }); if (item.ADD_REPLACE_QUOTE === 'Replace') { item.sourcing[0].addQuote(item.MINIMUM_ORDER_SIZE, item.MAXIMUM_ORDER_SIZE, item.LEAD_TIME, 'DAYS', item.TOTAL_PART_COST, ''); } else { for (ind = 0; ind < num_quotes; ind++) { item.sourcing[0].addQuote(quotes[ind][0], quotes[ind][1], quotes[ind][2], 'DAYS', quotes[ind][3], quotes[ind][4]); } item.sourcing[0].addQuote(item.MINIMUM_ORDER_SIZE, item.MAXIMUM_ORDER_SIZE, item.LEAD_TIME, 'DAYS', item.TOTAL_PART_COST, ''); } }
I will check into this Postman software. It looks very useful.
Thanks.
Hmm... Trying to figure out Postman, but I can't seem to figure out how to authorize requests. Do I need to manually copy and paste the reply to the login request into subsequent requests, or is there a means of doing that automatically?
ah, simply login to your tenant normally in a separate tab, then refresh the postnam tab. the browser will pickup the logged in session and cookies automatically. simply write your api and run.
I use the 'postman rest client' Chrome extension that runs in-tab browser.
I do not use the stdlone Postman Chrome extsension
Since the item.SOURCE_DESCRIPTOR is getting set server side (not client side by my macros), it isn't returned in the response. I could try to figure out a way to pull down the value from PLM from within SolidWorks, but since it's blank when I refresh the PLM 360 page after the macro has run, it seems like that would be more work than it is worth.
I'm working on using Postman to try things from a different angle, but I've got several things going on at once here, so progress is slower than it would be otherwise.
Here is the body of the request, and the associated response. I've replaced our tenant name with "mytenant", but other than that, everything else is identicle.