Solved

How can I relate an asset to a ticket via API where I have only the ID and not display_ID?

  • 3 November 2022
  • 5 replies
  • 827 views

Userlevel 4
Badge +8

Hello

I wanted to add an asset via API to a Service Request Ticket which the user selects in a custom field in our Service Request Item.

When reading the requested items via https://XXX.freshservice.com/api/v2/tickets/XXX/requested_items.json I get this:

{
    "requested_items": [
        {
            "custom_fields": {
                "request_type""Replace old device and order a new one",
                "manger_new_employee"null,
                "date_of_entry_new_employee"null,
                "comment_new_employee"null,
                "select_your_old_computer"52000100785,
                "what_should_happen_with_the_old_device""Buy for private use",
                "manager_replace_device"52000148148,
                "comment_replace_device""TEST ORDER! do not delete!"
            },
            "id"52000078156,
            "created_at""2022-11-03T13:58:41Z",
            "updated_at""2022-11-03T13:58:41Z",
            "quantity"1,
            "stage"1,
            "loaned"false,
            "cost_per_request"0.0,
            "remarks"null,
            "delivery_time"null,
            "is_parent"true,
            "service_item_id"157
        }
    ]
}
 
As you can see “Select_your_old_computer” is giving me the ID of the asset.
Now if I understood the API documentation correctly, to update the ticket with this asset I need the Display_ID!
Now HOW can I search for this assets details to add it if I cannot search by ID but also only Display_ID??
  /api/v2/assets/[display_id]
also I am open if you have any other ideas on how to solve this..
icon

Best answer by zachary.king 11 November 2022, 15:32

View original

5 replies

Userlevel 4
Badge +4

Hello msconfig87,

 

I hope I can help you with my answer where I focus on the basics of the workflow nodes with v2 API calls. Below instruction will contains two workflows and a change in your asset data, where in this case the asset_tag will be overwritten with the asset_id. If you don't want to do this, I unfortunately don't have an alternative solution for you.

In case you get only the asset ID (and not the display_id) back from the API call (same behavior when you have a custom dropdown field in the incident form with the asset source) AND you don't have the Reader node available in the workflow), you have only one option here and that is search for an asset based name, tag or serialnumber. Since neither of the 3 attributes are available in your json, or available in an API call to get all details from a ticket, we have to make a workaround here.

 

First we create a new asset workflow to overwrite the asset_tag with the asset_id value.

Event: Asset is created or updated.

Web request: Request Type = GET ==> https://example.freshservice.com/api/v2/assets/{{asset.display_id}}

Add a Parser node to parse the Web request body data and generate the output.

Use another Web request node to overwrite the asset tag with the asset id: Request Type = PUT ==> https://example.freshservice.com/api/v2/assets/{{asset.display_id}}

Add the following lines in the Body section of the Web request node. Be ware that the placeholder with the asset id from the Parser has to be placed within double quotes:

{
  "asset_tag": "{{P1.root.asset.id}}"
}

Make sure your assets are getting updated. You can use the bulk feature in the asset inventory to initiate this manually, so i.e. update the Impact to another value and bulk update it again to the first value. Since the asset_tag is now containing the asset _id value, we can move on with the ticket workflow.

 

Create a new ticket workflow and add the following nodes:

EVENT: When (SR) ticket is raised

You might want to add a Condition node here to check if the associate asset is empty.

Web Request: Request Type = GET ==> https://example.freshservice.com/api/v2/tickets/{{ticket.id_numeric}}

Add a Parser node to parse the Webrequest body data and generate the output of the ticket data. Here we have to make sure the asset_id is getting returned.

Be aware that the placeholder in below Web request node is coming from the parsed data, which is the custom field from the ticket. In this example the custom field name = “select an asset”. Keep the full url as-is, and only replace the placeholder. You can run the url also in your browser on a 2nd tab next to your Freshservice tab to test the result of the API call.

Add another Web Request node to search for the asset, based on the asset ID that is returned from the first Web request node. Request Type = GET ==> https://example.freshservice.com/api/v2/assets?filter="asset_tag%3A%27{{P4.root.ticket.custom_fields.lf_select_an_asset}}%27"

Add another Parser node to parse this Web Request body data and generate the output. This should return all basic attributes from the asset, including the “display_id”.

Within the Parser node variables (via Generate Output): we have to add a new variable, since the output of the “display_id” is an object (like “[123]”) and this can't be used in an url. 

In the Parser node, on the right side, where the generated output variables are visible, find the “display_id” one. Hoever above it and click on the right side on the 3 dots and select “Add a variable”.

Name the new variable as “display_id_string”. Set Type as String. Set Path as “$.assets[0].display_id” without the double quotes. You'll see that the output of this variable is now the actual “display_id” value without the [ ] brackets.

 

Finally, add another Web Request node, to associate the asset to the ticket:

Request Type = PUT ==> https://example.freshservice.com/api/v2/tickets/{{ticket.id_numeric}}

Add the below lines in the Body of this node, where the subject is my example the description (however you might want to overwrite it with the subject from the ticket, since you can't just add the asset only). Next to that, the placeholder will contain the variable that we created earlier in the Parser node with the parser object name “display_id_string”.

{
  "subject": "{{P4.root.ticket.description_text}}",
  "assets": [
    {
      "display_id": "{{P5.root.assets.assets_object.display_id.display_id_string}}"
    }
  ]
}

 

Let me know your results.

 

Kind regards,

Rutger Bockholts

Freshworks

Userlevel 4
Badge +8

Hello Rutger

Wow. Thanks you for your quite elaborated approach to solve such a simple problem. I appreciate your efforts.

But what follows now is some sort of a Friday rant….:

I did not choose this product, I was forced to work with it by my company. Please have mercy with poor admins like me and please don’t make our life harder than necessary!

There is no explanation for this and I am disappointed why making it extremely hard for us when it could be SO easy??

If any developer of FreshService reads this, please have a soul and consider this:

  • Why does the Action node in the Workflow Automator have NOT the possibility to associate the ticket to an asset? (only projects). We are forced to do so by API (and consume credits)
  • Why does the WebRequest node give us the possibility to only choose the ID of the custom field of the service request?
  • Why does the Asset API ONLY ACCEPT an artificial Display_ID to view the asset whereas requester, agents, locations, products, vendors, asset_types, contracts, departments, and basically everything else CAN be viewed by ID??
  • Why on earth if this is the case, you are NOT adding the possibility to SEARCH for the asset by API at least by ID and not only name, asset_tag, serial_number?
  • Why you NOT allow to FILTER the assets by API by ID but only by asset_type_id, department_id, location_id, asset_state, user_id, agent_id, name, asset_tag, created_at, updated_at?
  • This would ALL been even much easier at first place if your Reader node would accept ID! Instead it only accepts Asset ID, Display Name, Asset Tag, CI Type, Impact, Description, Location, Department, Managed By, Used By, Group End of Life
  • And finally, if your developers KNEW that the assets are only findable by display_ID, why on earth would you give out the ID instead on the custom fields??

I think that this is simply not acceptable to deliver the nodes and the API in such an unfinished state.

I will therefore create here an feature request where everyone you know needs to vote for. This needs to be implemented immediately. Its a core functionality missing. Or else you can delete the possibility to choose assets as a custom field in first place!

Please vote here:

 

Userlevel 7
Badge +16

Hello @msconfig87, my suggestion regarding discontent with the Freshservice APIs is to take this conversation over to the developer community. API maintenance and traction can be made much quicker in an area where developers congregate and monitor issues with consuming the product from an engineering perspective. There are a lot of brilliant people here in the Refresh Community and that is just so the case over in the Developer Community as well.

I feel your pain. With any software I find myself hitting my head against a wall wondering why, what feels likes, a simple solution to me is not being implemented in a user friendly manner and I often stumble into these findings in the middle of a project or some task that needs to be completed for some stakeholder. Making me look bad when I can’t meet that need as an administrator.

Hopefully we are able to give honest feedback and see our suggestions turn into features and functionality. Best of luck my friend!

Userlevel 4
Badge +8

Thanks zach, Will try my luck there

Userlevel 4
Badge +8

for traceability here my post in the dev forum: https://community.freshworks.dev/t/fix-custom-field-useage-for-assets-in-freshservice-display-id-is-not-available/7887

 

Also I accidentally set the this topic to solved but its not..

Reply