Forum Discussion

angela's avatar
angela
Catalyst II
1 year ago
Solved

Editing an item via REST API without item id. Can we specify a WHERE attribute?

Hi community,

I want to edit a custom item in Innovator via REST API. Typically the command looks like this:

PATCH {base url}/Part(’16AA95A80DEB4B56BD8BA9BC509EAF0C’)

My 2nd software that shall do the REST call doesn´t know the target item id. I only have the serial number (item_number) of the target item in Innovator.

For regular AML queries we can use the WHERE attribute to perform these kinds of edit operations.
Is something similar available in the REST API?

An alternative solution would be to do a regular GET request first to get the ID. Or maybe a custom Method that does the edit with a standard AML query.

Any ideas? What would you recommend?

Thanks!

Angela

  • Thanks Bryan and Daan for your feedback! Are you really sure this approach works when performing an edit? Yes, of course, using a filter sounds absolutely reasonable - but here´s the reality….😄

    I get the same error when using PUT. I don´t have a collection. It´s a blank database with exactly one CAD item. 
    GET works fine.

    My current workaround is a custom Method. In general I started to prefer custom Methods for REST call. They allow much more control and validation.
    And it´s maybe a good thing that PATCH behaves this way. This way people only can destroy one item at a time. That´s a big plus! :)

6 Replies

  • It is possible Rest always returns a collection... You could try adding a '$top=1' in there to see if you can work around it. 

    A good trick I've found was calling a method from Rest, I really wish I could find a way to pass variables into the Method...  

    http://~/InnovatorServer/server/odata//method.CreateNewGUID

     

    • eli_donahue's avatar
      eli_donahue
      Community Manager
      bryan-riley wrote:

      I really wish I could find a way to pass variables into the Method

      Quick FYI - you can pass variables to methods when calling a CWS or InnovatorEdge API! 🙂 If you're interested in learning more, here's a blog post about calling server methods via CWS: Calling Methods via Configurable Web Services. We'll have an updated version featuring InnovatorEdge APIs soon.

  • Thanks Bryan and Daan for your feedback! Are you really sure this approach works when performing an edit? Yes, of course, using a filter sounds absolutely reasonable - but here´s the reality….😄

    I get the same error when using PUT. I don´t have a collection. It´s a blank database with exactly one CAD item. 
    GET works fine.

    My current workaround is a custom Method. In general I started to prefer custom Methods for REST call. They allow much more control and validation.
    And it´s maybe a good thing that PATCH behaves this way. This way people only can destroy one item at a time. That´s a big plus! :)

    • eli_donahue's avatar
      eli_donahue
      Community Manager

      After a bit of digging into the OData 4.0 spec and playing around in Postman, it seems like this may be a limitation of OData itself. I even tried using a batch request to get and update an item, but that's not a viable option due to the nature of OData batch/change sets. That leaves two options:

      1. Two requests from the client - one to get the id and another to make the PATCH
      2. One request to execute a server method that performs all the logic you need

       

      I'll ask around tomorrow to see if our internal OData experts have any other ideas, but at the moment it sounds like you're already on the best track.

      Edit: Does your client system allow you to define a callback function/operation? If so, maybe you could have the callback use the response from the inital request to send your PATCH request.

  • Hi Angela,

    A small addition to what bryan-riley​ already pointed out. In JavaScript, the notation and use of quotes is important. We never had any issues with the notation as follows:

    https://baseUrl/Server/odata/[ItemType]?$filter=(state eq 'Released')

    Or a more complex example:

    https://baseUrl/Server/odata/Simple ECO?$filter=[Simple ECO Affected Item]/related_id/affected_id eq '${itemId}'

    Or more generic:

    let response = await axios.get(`https://baseUrl/Server/odata/${sourceType}('${sourceId}')/${relationshipType}?$expand=related_id`, {
    
                headers: {
    
                    'Content-Type': 'application/json',
    
                    'Authorization': 'Bearer ' + token
    
                }
    
    });
    
    let relatedItem = response.data.value.map(item => item.related_id);

     

  • Hey Angela, you can use the $filter property in the URL to narrow the number of results,

    I've only used it for get requests so far, but I don't see why it wouldn't work for Patch requests, as long as you filter down to one result... 

    https://~/odata/Part?$filter=item_number eq xclasstest01