1

I'm trying to delete a record that is related to the current record loaded using a workflow action script. I've seen other answers to similar questions saying that I can find the internal ID of the related record with a search using the value in the key field as the search parameter. Problem: The N/search module is not available in a workflow action script, which is where I'm currently coding.

To make the question more concrete, my user has pulled up a general ledger account record ('account'), and they're going to delete it. I have an NS bundle that functions as an integrator with a 3rd party piece of software. The NS bundle has a value transformation table that maps the NS account codes to the 3rd party software codes. Each value transformation lives in a custom record ('icvt') created by the bundle, and I need the value transformation record deleted. I was hoping to use N/search to look up the icvt record, but I CAN'T USE SEARCH in a workflow action script.

Help.

Edit: Just realized I think I misunderstood the documentation, and that N/search is available, so my error must be somewhere else.

Edit to add code. This is the search code I was using before I realized the module wasn't available to me:

/**
*@NApiVersion 2.x
* @NScriptType WorkflowActionScript
*/

// Deletes Value Transform related to account deleted.

define(['N/record', 'N/search', 'N/log'], function (record, search, log) {

function wfDeleteICVT(scriptContext) {

var deleted_account = scriptContext.newRecord;
var da_internalID = deleted_account.getValue("id");

var v_da_internalID = da_internalID.toString();

var mySearch = search.create({
    type: 'icvt',
    filters: [
        search.createFilter({
        name: 'sourcevalue',
        operator: search.Operator.IS,
        values: v_da_internalID
})
]
    });

//log.debug(v_da_internalID)

var myResultSet = mySearch.run();
var myResultRange = myResultSet.getRange(0,1);

log.debug({title: myResultRange.length, details: myResultSet})

var deleted_icvt = mySearch[0];

var di_icvt = deleted_icvt.id;

deleted_icvt.delete({
     type: 'icvt',
     id: di_internalID
});
}

return {
            onAction : wfDeleteICVT
        }

});
jerry
  • 31
  • 5

2 Answers2

1

If you are able to fetch the ID of the record that needs to be deleted, you can use N/record module with record.delete(options) to delete the desired record.

// Delete a sales order.
var salesOrderRecord = record.delete({
 type: record.Type.SALES_ORDER,
 id: 88,
});

// Delete an instance of a custom record type with the ID customrecord_feature. 
var featureRecord = record.delete({
 type: 'customrecord_feature',
 id: 3,
});

EDIT-

N/search module works on Workflow Action Scripts. I myself have used it many times. Why don't you create the Saved Search in UI on Netsuite and then Export it as script and use it in your script?

To export - You can use the Chrome extension below.

NetSuite Search Export

Also, I can see you have created filters in your search.create method but you didn't add any columns. That might be the issue.

Let me know.

Sayeesh
  • 208
  • 1
  • 3
  • 13
  • I don’t have a problem with the delete, I have a problem with finding the ID. The method that I would normally use is not available to me because I’m writing a workflow action script. – jerry Jan 09 '22 at 06:26
  • @jerry Is the ID of desired record present on current record? That is the record on which Workflow Action Script is deployed? – Sayeesh Jan 09 '22 at 06:57
  • No. The current record is a GL account record, and the record I need deleted is a 3rd party bundle record tied to the GL account record. My problem is finding the ID of the 3rd party bundle record. – jerry Jan 09 '22 at 07:14
  • @jerry Are you able to access it using Saved Search in Netsuite UI ? – Sayeesh Jan 09 '22 at 07:16
  • Yes. I don’t believe it is an access issue, or the 3rd party bundle not being exposed. – jerry Jan 09 '22 at 08:36
  • Yes thats might be the issue. – Sayeesh Jan 09 '22 at 08:59
0

Here's the final solution that worked. I was completely wrong in my initial assumptions that I couldn't use search, so that was the ultimate solution, although I did take @sayeesh's advice and use a search created through the UI:

/**
*@NApiVersion 2.x
* @NScriptType WorkflowActionScript
*/

// Deletes Value Transform related to account deleted.

define(['N/record', 'N/search', 'N/log'], function (record, search, log) {

function icvtDelete(scriptContext) {

var deleted_account = scriptContext.newRecord;  //gets the account record of the current account
var da_internalID = deleted_account.getValue("id");  //gets the internal id of the current account

var v_da_internalID = da_internalID.toString();  //converts the internal id to a string for comparison later

var mySearch = search.load({
   id: "customsearch_icvt_search"});  //loads a saved search

var mySearchResult = mySearch.run();  //runs the saved search
var myResults = mySearchResult.getRange(0,1000);  //gets the range of results
    for(var i in myResults){  //iterates through the results
        var result = myResults[i];  //load a result
        if(result.getValue('custrecordictransformationvalue') == v_da_internalID)  //if the result is the one we're looking for
            {
               var icvt_to_delete = record.load({
                 type: 'mycustomrecord',
                 id: result.getValue('id')
                                  });
               log.debug(v_da_internalID, result.getValue('id'));
               icvt_to_delete.setValue({fieldId: 'deletionreason', value: 1});
               icvt_to_delete.setValue({fieldId: 'deletionreasonmemo', value: 'SS2.0 Delete reason saved using Load/Save'});
               icvt_to_delete.save();

               record.delete({
                    type: 'mycustomrecord',
                    id: result.getValue('id')
                        });
            }
        }
}

return {
            onAction : icvtDelete
        }

});
jerry
  • 31
  • 5