1

I have a restlet script that updates the customer. In the customer I want to loop through the subscription objects passed in but I only have the subscription names to match them on.

Does Anyone know how to access the subscriptions object in Netsuite?

quarks
  • 33,478
  • 73
  • 290
  • 513

1 Answers1

1

I got a response from Netsuite that you cannot access the subscriptions object in Netsuite so I had to write a little bit of a hack. What I did was grab an existing member and load them up. The member should have an array of all the active subscriptions.

I then loop through them and create an array object to pass back containing the subscription name and internalid.

I can then use that array to loop through the customer subscriptions passed in with only names and get the matching internalid.

Here is the code I wrote. Please feel free to streamline it if you think it can be written better.

var campaigns = new Array();
try {
    // NETSUITE DOES NOT EXPOSE THE CAMPAIGN SUBSCRIPTIONS
    var filters = new Array();
    filters[0] = new nlobjSearchFilter('internalid', null, 'is', 26); //ANON CUSTOMER
    var columns = new Array();
    columns[0] = new nlobjSearchColumn('internalid');
    //columns[1] = new nlobjSearchColumn('campaign', null, null);
    results = nlapiSearchRecord('customer', null, filters, columns);
    resultsTotal = (results != null) ? results.length : 0;
    if (resultsTotal > 0) {
        var customerid = results[0].getId();
        var customer = nlapiLoadRecord('customer', customerid, {
            customform: -2
        });
        var mystring = JSON.stringify(customer); //CONVERT TO STRING
        var data = JSON.parse(mystring); //THEN PARSE TO JSON
        for (var node in data) { //FOR EACH NODE IN DATA            
            for (attr in data[node]) { //FOR EACH ATTRIBUTE IN THE NODE
                if (node == "subscriptions") {
                    for (var node2 in data[node][attr]) { //FOR EACH NODE IN SUBSCRIPTIONS
                        if (node2 == 'subscription') { //GET SUBSCRIPTION NODES 
                            var campaign = new Object();
                            var name = '';
                            var internalid = 0;
                            for (var node3 in data[node][attr][node2]) {
                                if (node3 == 'name') {
                                    name = data[node][attr][node2][node3];
                                } else if (node3 == 'internalid') {
                                    internalid = parseInt(data[node][attr][node2][node3]);
                                }
                            }
                            //nlapiLogExecution('AUDIT', name, internalid); //NAME  
                            campaign[name] = internalid;
                            if (!campaigns.hasObject()) {
                                campaigns.push(campaign);
                            }
                            //nlapiLogExecution('AUDIT', node2, JSON.stringify(data[node][attr][node2]));   //SUBSCRIPTION                              
                        }
                        //nlapiLogExecution('AUDIT', node2, JSON.stringify(data[node][attr][node2]));
                    }
                    //nlapiLogExecution('AUDIT', node, JSON.stringify(data[node][attr])); //SUBSCRIPTIONS   
                }
            }
        }
        nlapiLogExecution('AUDIT', 'CAMPAIGNS', JSON.stringify(campaigns));
    }
} catch (e) {
    nlapiLogExecution('ERROR', 'Error', 'There was an error. ' + e.name + ' - ' + e.message + '.');
}
return campaigns;
Zachary Dahan
  • 1,419
  • 16
  • 26