Fusion Manage Forum
Welcome to Autodesk’s Fusion Manage (formerly Fusion 360 Manage) Forum. Share your knowledge, ask questions, and explore popular Fusion Manage topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How do I run scripts in response to API actions?

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
Tim.Lewis
935 Views, 12 Replies

How do I run scripts in response to API actions?

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?

12 REPLIES 12
Message 2 of 13
gasevsm
in reply to: Tim.Lewis

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,


Martin Gasevski | Fusion 360 Team Product Manager
Message 3 of 13
Tim.Lewis
in reply to: gasevsm

@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.

Message 4 of 13
gasevsm
in reply to: Tim.Lewis

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.


Martin Gasevski | Fusion 360 Team Product Manager
Message 5 of 13
Tim.Lewis
in reply to: gasevsm

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.

Message 6 of 13
Tim.Lewis
in reply to: gasevsm

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?

Message 7 of 13
gasevsm
in reply to: Tim.Lewis

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.

 

postman rest client.png

 

I do not use the stdlone Postman Chrome extsension

postman.png 


Martin Gasevski | Fusion 360 Team Product Manager
Message 8 of 13
gasevsm
in reply to: Tim.Lewis

Thanks I'll take a look today. Could you confirm if you are seeing correct value for item.SOURCE_DESCRIPTOR in the API response payload since getting 201?

Martin Gasevski | Fusion 360 Team Product Manager
Message 9 of 13
Tim.Lewis
in reply to: gasevsm

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.

Message 10 of 13
gasevsm
in reply to: Tim.Lewis

Thank you Tim, Can you send me the API request and response payloads for review please?

Martin Gasevski | Fusion 360 Team Product Manager
Message 11 of 13
Tim.Lewis
in reply to: gasevsm

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.

Message 12 of 13
Tim.Lewis
in reply to: gasevsm

By the way, I finally got around to testing my API request with Postman, and got the same results. Specifically, I execute the request, and as far as I can tell, Postman says the request went through just fine, but when I go to PLM 360, it's clear that the action script that was supposed to run did not run. I'll send you the downloaded collection (which includes the response) privately since it appears to include additional data that may be more sensitive than the XML files I sent previously.
Message 13 of 13
Tim.Lewis
in reply to: gasevsm

Hmm... I think I just figured out the problem. The action script only gets run when items are modified, not when they are created. When I create an item via the API and then subsequently modify that same item, the action script gets executed. I just looked and didn't have the action script attached to the creation event. Just to the modified event.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report