Retrieve Jira Service Management DC most recent assets from Slack command


Get Started

Not the template you're looking for? Browse more.

About the template


This template demonstrates how to retrieve 10 (configurable) most recent assets created in the default schema (Service objects) from Jira Service Management Assets (Data Center or server) when a Slack slash command is triggered, and then how to send back the assets to the Slack channel where the command originated from. Get started to learn more.

About ScriptRunner Connect


What is ScriptRunner Connect?

ScriptRunner Connect is an AI assisted code-first (JavaScript/TypeScript) integration platform (iPaaS) for building complex integrations and automations.

Can I try it out for free?

Yes. ScriptRunner Connect comes with a forever free tier.

Can I customize the integration logic?

Absolutely. The main value proposition of ScriptRunner Connect is that you'll get full access to the code that is powering the integration, which means you can make any changes to the the integration logic yourself.

Can I change the integration to communicate with additional apps?

Yes. Since ScriptRunner Connect specializes in enabling complex integrations, you can easily change the integration logic to connect to as many additional apps as you need, no limitations.

What if I don't feel comfortable making changes to the code?

First you can try out our AI assistant which can help you understand what the code does, and also help you make changes to the code. Alternatively you can hire our professionals to make the changes you need or build new integrations from scratch.

Do I have to host it myself?

No. ScriptRunner Connect is a fully managed SaaS (Software-as-a-Service) product.

What about security?

ScriptRunner Connect is ISO 27001 and SOC 2 certified. Learn more about our security.

Template Content


README

Scripts

TypeScriptGetAssetsObjectSchemas
TypeScriptOnSlackSlashCommand
Slash Command

README


๐Ÿ“‹ Overview

This template demonstrates how to retrieve 10 (configurable) most recent assets created in the default schema (Service objects) from Jira Service Management Assets (Data Center or server) when a Slack slash command is triggered, and then how to send back the assets to the Slack channel where the command originated from.

๐Ÿ–Š๏ธ Setup

  • Configure API Connections and Event Listeners by creating connectors for JSM On-Premise Assets and Slack, or use existing ones.
  • (Optional) Go to Parameters and change MAX_ASSETS parameter to control the amount of objects returned.
  • (Optional) Manually trigger the GetAssetsObjectSchemas script to print ouit all available Object Schemas.

๐Ÿš€ Usage

  • Trigger a Slack slash command (/command_name <ASSETS_SCHEMA_NAME>), after successful processing you should receive a message containing the 10 latest assets.

API Connections


TypeScriptGetAssetsObjectSchemas

import JSMOnPremiseAssets from "./api/jira/on-premise/assets";

/**
 * This function retrives all object schemas and prints them out
 */
export default async function(event: any, context: Context): Promise<void> {
    const schemas = await JSMOnPremiseAssets.Object.Schema.getSchemas();
    
    console.log('Script triggered', schemas.objectschemas);
}
TypeScriptOnSlackSlashCommand

import JSMOnPremiseAssets from "./api/jira/on-premise/assets";
import Slack from "./api/slack";
import { SlashCommandEvent } from '@sr-connect/slack/events';
import { ObjectEntity } from '@managed-api/jira-service-management-on-premise-assets-core/definitions/Object';

/**
 * This function fetches JSM On-Premise 10 most recent Assets created for the requested schema by a Slack's slash command, 
 * and then sends back information to Slack channel where the message originated from.
 * Command syntax: /command_name <ASSETS_SCHEMA_NAME>
 * 
 * @param event Object that holds Slash Command event data
 * @param context Object that holds function invocation context data
 */
export default async function (event: SlashCommandEvent, context: Context): Promise<void> {
    if (Object.keys(event).length === 0) {
        console.error('This script is designed to be triggered externally or manually from the Event Listener. Please consider using Event Listener Test Event Payload if you need to trigger this script manually.');
        return;
    }

    const { MAX_ASSETS } = getEnvVars(context);

    try {
        // Get the specified schema name from the event
        const schemaName = event.text;

        // Check if schemaName was set
        if (!schemaName) {
            // If not, then send approproate message back to Slack
            await Slack.Chat.postMessage({
                body: {
                    channel: event.channel_id,
                    text: 'Missing schema name'
                }
            });

            // And halt the script execution
            return;
        }

        // Retrieve the assets
        const assets = (await JSMOnPremiseAssets.Aql.getObjects({
            resultPerPage: MAX_ASSETS,
            qlQuery: `objectSchema = "${schemaName}" ORDER BY created DESC`,
        })).objectEntries;

        // Check if there are any assets
        if (assets.length > 0) {
            // If there are then send a message to Slack with all the assets found
            await Slack.Chat.postMessage({
                body: {
                    channel: event.channel_id,
                    blocks: [{
                        type: 'section',
                        text: {
                            type: 'mrkdwn',
                            text: `${assets.map(a => getAssetsInfo(a)).join('\n')}`
                        }
                    }]
                }
            });
        } else {
            // If no assets are found, sent appropriate message
            await Slack.Chat.postMessage({
                body: {
                    channel: event.channel_id,
                    text: 'No assets found'
                }
            });
        }

    } catch (e) {
        // If something went wrong, then log it out
        console.error('Error while processing slash command', event, e);

        // And send appropriate message back to Slack
        await Slack.Chat.postMessage({
            body: {
                channel: event.channel_id,
                text: 'Something went wrong while retrieving assets'
            }
        });
    }
}

/**
 * Function that transforms information into Slack's compatible markdown format
 */
function getAssetsInfo(assets: ObjectEntity) {
    return `โ€ข Key: ${assets.objectKey} | Type: ${assets.objectType.name} | Name: ${assets.label}`
};

interface EnvVars {
    MAX_ASSETS: number;
}

export function getEnvVars(context: Context) {
    return context.environment.vars as EnvVars;
}
Documentation ยท Support ยท Suggestions & feature requests