Import context menu as UI action

Alejandro Fraga
Tera Contributor

Hello All,

So here's the situation I'm having trouble with.

I have created a table (u_planning) to allow Project managers to upload an excel with estimation info regarding their projects. I'm using the "Import" context menu to test the data is been imported properly and everything works fine.

I'm just trying to find a way to place the "import" option available as a UI action on the top of the list

find_real_file.png

But I haven't been able to mimic the "Import" context menu on the UI Action, is there a way to implement this?

1 ACCEPTED SOLUTION

Geoffrey2
ServiceNow Employee
ServiceNow Employee

Try copying the code from the Context Menu record into your UI Action: .../sys_ui_context_menu.do?sys_id=8be9fda7bf460100e628555b3f07397b


View solution in original post

5 REPLIES 5

June1
Kilo Contributor

Hi,

I don't know if you still need this but this is what I have to take and excel file as an attachment and parse it to put entries into multiple related tables... this queries the first column in the excel to identify which related list to insert the record.  Hope it helps.

importChildren();

function importChildren() {
    // Description: Takes attachment and processes the contents via import and transforms.
    var sourceDSid = '821448d81b1ed4500322ebd56e56'; //Location child Import
    var showMessages = true;

    //Get attachment
    var attGR = new GlideRecord('sys_attachment');
    attGR.addQuery('table_sys_id', current.sys_id.toString());
    attGR.query();
    if (attGR.next()) {

        gs.addInfoMessage('Processing attachment: ' + attGR.file_name);

        //Grab a reference to the data source.
        var sourceDS = new GlideRecord('sys_data_source');
        sourceDS.addQuery('sys_id', sourceDSid);
        sourceDS.query();

        //If we have found the data source, start doing some work!
        if (sourceDS.next()) {

            if (showMessages)
                //	gs.addInfoMessage('Using data source: '+sourceDS.name);

                //Copy the DS
                var newDS = new GlideRecord('sys_data_source');
            newDS.initialize();
            var elements = sourceDS.getElements();
            var numElements = elements.size();
            for (var i = 0; i < numElements; i++) {
                var element = elements.get(i);
                newDS.setValue(element.getName().toString(), sourceDS.getValue(element.getName().toString()));
            }
            // Give the new name - "yourID'
            var currentUser = gs.getUser();
            newDS.name = newDS.name + '-' + currentUser.getName();
            newDS.insert();

            // Copy the attachment to the new Data Source
            GlideSysAttachment.copy('main_table', current.sys_id, 'sys_data_source', newDS.sys_id);

            // Start fresh with a Object of this attached file on your dataSource and add the ! to the end, touching it/refreshing it
            var yourDS = new GlideRecord('sys_data_source');
            yourDS.get(newDS.sys_id);
            yourDS.name = yourDS.name + '!';
            yourDS.update();
            if (showMessages)
                //	gs.addInfoMessage('Your new data source with attachment: '+yourDS.name);

                //Start loading the data in to an import set
                var loader = new GlideImportSetLoader();
            var importSetGr = loader.getImportSetGr(yourDS);
            var ranload = loader.loadImportSetTable(importSetGr, yourDS);

            if (showMessages)
                //	gs.addInfoMessage('ISET: '+importSetGr.getDisplayValue());

                // Lookup the ISET's table ref where the data recs are loaded, update all "u_parent' values with 'current'.
                var rCount = 0;
            var igr = new GlideRecord(importSetGr.table_name);
            igr.addQuery("sys_import_set", importSetGr.sys_id.toString());
            igr.addQuery("sys_import_state", "pending");
            igr.query();
            while (igr.next()) {
                rCount = rCount + 1;
                //	igr.u_parent = current.sys_id.toString();
                igr.main_table = current.sys_id.toString();
                igr.update();

                //	if (showMessages)				
                //		gs.addInfoMessage('Inserted: ' + current.getDisplayValue() + ' as parent into: ' + importSetGr.getDisplayValue() + ' row: ' + igr.getDisplayValue() + ' Name:' + igr.u_table);
            }

            //Start the transform on the import set
            var t = new GlideImportSetTransformer();
            t.transformAllMaps(importSetGr);
            importSetGr.state = 'processed';
            importSetGr.update();

            // Remove temp user's Data source
            yourDS.deleteRecord();

            if (showMessages)
                //		gs.addInfoMessage('Removed temp data source: '+yourDS.name);

                gs.addInfoMessage('Records successfully added: ' + rCount);


        }

        // Delete attachment
        var gsa = new GlideSysAttachment();

        var att = new GlideRecord('sys_attachment');
        att.addQuery('table_name', 'main_table');
        att.addQuery("table_sys_id", current.sys_id);
        att.orderByDesc('sys_created_on');
        att.setLimit(1);
        att.query();
        while (att.next()) {
            gsa.deleteAttachment(att.sys_id);
        }

        gs.addInfoMessage('Attachment successfully deleted. ');

    } else {
        gs.addErrorMessage('No attachment found');
    }
    action.setRedirectURL(current);
}