How to get id of deleted records in Netsuite using Suitescript 2.0?

1.2k Views Asked by At

I could get deleted records from Netsuite using below script. But I could not get id.

var customSearch = search.create({
     type: "deletedrecord",
     columns: ["context", "deletedby", "deleteddate", "externalid", "name"],
     filters: [
         ["recordtype", "is", "customer"]
     ]
});
var resultSet = customSearch.run().getRange({
    start: 0,
    end: 5
});

How can I get unique id of deleted records from Netsuite using Suitescript 2.0?

2

There are 2 best solutions below

0
On

As of 2024.1 you can get some of that various ways so updating my answer from 3 years ago.

per @Martha you can now get this via suitescript. The recordid and recordtype fields are not specified in the records browser and so may break but with some inspiration from my SuiteQL example the following works in the console:

require(['N/search'], search => {
    var deletedRecords = search.create({
        type: "deletedrecord",
        filters: [
            ["deleteddate", "within", "today"]
        ],
        columns: [
            'recordid',
            'recordtype',
            search.createColumn({
                name: "externalid"
            }),
            search.createColumn({
                name: "deleteddate"
            }),
            search.createColumn({
                name: "deletedby"
            }),
            search.createColumn({
                name: "context"
            }),
            search.createColumn({
                name: "name"
            })
        ]
    });

    deletedRecords.run().each(function(result) {
        const obj = {
            id: result.id
        };
        result.columns.forEach(c => {
            obj[c.name] = result.getValue(c);
        });
        console.log(JSON.stringify(obj));

        return true;
    });
})

Other ways:

Using SuiteQL though the external Id is not available

SELECT
DeletedRecord.isCustomTransaction,
    DeletedRecord.context,
    DeletedRecord.deletedDate,
    to_char(deletedDate, 'dd-mm-yyyy hh24:mi'),
    BUILTIN.DF(DeletedRecord.deletedBy),
    DeletedRecord.isCustomList,
    DeletedRecord.isCustomRecord,
    DeletedRecord.name,
    DeletedRecord.recordId,
    DeletedRecord.recordTypeId,
    DeletedRecord.scriptId,
    DeletedRecord.type
FROM
    DeletedRecord
where deletedDate > '3/13/2024' and recordTypeId in ('journalentry', 'advintercompanyjournalentry')
and BUILTIN.DF(DeletedRecord.deletedBy) not like 'Brett%'
 order by BUILTIN.DF(DeletedRecord.deletedBy), name

It's available in SuiteTalk using the getDeleted operation

It's available via an external database query using SuiteConnect (ODBC, JDBC or ADO.net)

If you can start from now an easy hack is as follows:

create an After Submit User Event script that runs on record creation. If the record doesn't have an external id push the namespaced netsuite internal id into that field. I've found it's generally a good idea to prefix your external ids with the system of origin.

Now you can derive your internal ids from the external id. This doesn't help if you also have actual externalids but may suit your use case.

You can also fairly easily roll your own and create an after submit user event script that populates a custom record with the type and internalid of the deleted record. It's generally the way I go. Particularly if you are driving other suitescripts or workflows in your Netsuite account from the deletion data

Finally, though I haven't done this in years, it's quite possible to run a SuiteTalk query from SuiteScript. You just have to save the credentials (not in the clear) and construct the appropriate SOAP message.

1
On

Suite Answer Id: 69458 suggests the following will work:

var deletedRecords = search.create({
    type: "deletedrecord",
    filters:[
        ["deleteddate","within","today"]
    ],
    columns:[
        search.createColumn({name: "externalid"}),
        search.createColumn({name: "deleteddate"}),
        search.createColumn({name: "deletedby"}),
        search.createColumn({name: "context"}),
        search.createColumn({name: "name"})
    ]
});

var searchResultCount = deletedRecords.runPaged().count;
log.debug("deletedRecords result count",searchResultCount);

deletedRecords.run().each(function(result){
    // .run().each has a limit of 4,000 results
    return true;
});

Per the 2020.2 Records Browser only "externalid" is available though. Sometimes the external and internal ids are the same in my system; not sure about your specific set up for NetSuite.