How to Retrieve a Custom Device Name Attached to Client in a Trigger


#1

When working in a trigger, sometimes it’s necessary to retrieve the Custom Device that is associated with a client. Often, this will be for either remote sites or for systems with multiple IP Addresses assigned. Some example code is included below.

Note that clients can be a part of multiple custom devices. Although this is not best practice, there may be times when this is necessary. This code assumes only one Custom Device, and pulls out the first one found.

var deviceName = Flow.client.device.dnsName[0];
var customName = deviceName; // if there isn't a custom device, just use the DNS name

if (Flow.client.customDevices[0]) { // check if custom devices are associated with client
    var customDevice = Flow.client.customDevices[0].id;

    // Only get the part of the custom device name up to the dash
    customName = customDevice.substring(1,customDevice.indexOf('-'));
}

// At the very least, if customName doesn't exist due to no DNS name or Custom Device name, use the IP Address

if (!customName) {
    customName = Flow.client.device.ipaddr.toString()
}

// From here on out, you can use the variable "customName"

#2

In general, we recommend using assignments to manage trigger behavior, rather than inspecting the device names. Doing so makes it easier to enable the trigger’s behavior on additional devices without making source code modifications.

If committing custom detail metrics, you can pass the relevant IPAddress instance directly to the key argument of the method; for example:

let app = Application("myApp");
app.metricAddDetailCount("my_proto.req.by_client", Flow.client.ipaddr, 1);

Once you’ve done this, in the Metric Explorer (the “Create Chart” UI) you can choose to show the device’s name as the series legend, rather than the IP address - see the “Labels” option on the left in the screenshot below.

This has the added benefit of always showing the correct name for the device, even if it changes after the metric was committed.