Hi everyone, I want to ask how to get all record o...
# suitescript
m
Hi everyone, I want to ask how to get all record of data with RESTlet, from the example in the website it can only get 1 record
m
Need more details.
m
The code is like this
Copy code
function _get(context) {
         doValidation([context.recordtype, context.id], ['recordtype', 'id'], 'GET');
         return JSON.stringify(record.load({
             type: context.recordtype,
             id: context.id
         }));
     }
m
Ah I have never tried that. There isn't anything documented, but you might want to try the following just in case there is an undocumented function.
Copy code
record.load({
             type: context.recordtype,
             id: context.id
         }).toJSON();
m
Thanks for getting my code to get a better result in json. But my need is to get all record not just 1 by id
m
So you want the reslet to return all inventory adjustments?
m
Yes, that's it
m
If that's the case then you will want to use 'N/Search' to create a search of the transactions.
Something like this.
Copy code
const lines = [];
search.create({
type: 'transaction',
columns: [
'tranid',
'trandate',
'item',
'amount'
],
filters: [
{name: 'type', operator: search.Operator.ANYOF, values: ['inventoryadjustment']}
]
}).run.each(result => {
lines.push({
tranid: result.getValue('tranid'),
trandate: result.getValue('trandate'),
item: result.getValue('item'),
amount: result.getValue('amount')
})
});
m
Sorry, I still can't get it I got this message when try to upload it to Netsuite :
Syntax error: TypeError: redeclaration of formal parameter lines.
My code is like this
Copy code
function _get(lines) {
        const lines = [];
        search.create({
        type: 'transaction',
        columns: [
        'tranid',
        'trandate',
        'item',
        'amount'
        ],
        filters: [
        {name: 'type', operator: search.Operator.ANYOF, values: ['inventoryadjustment']}
        ]
        }).run.each(result => {
        lines.push({
        tranid: result.getValue('tranid'),
        trandate: result.getValue('trandate'),
        item: result.getValue('item'),
        amount: result.getValue('amount')
        })
        });
     }
n
Not sure if it doesn't like you pushing values into a constant or it's out of scope and each time you push it thinks it's re-declaring "lines". I'd try changing const to let / var since well, it's declared as a constant that is an empty array and then you're manipulating it's value, not very "constant" alike. If that doesn't work it could be scope and maybe run your search to an array then process the search results.
m
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType Restlet
 */
 define([
  'N/record',
  'N/search'
], 
function(search){
  function _get(lines) {
    let lines = [];
    search.create({
    type: 'transaction',
    columns: [
    'tranid',
    'trandate',
    'item',
    'amount'
    ],
    filters: [
    {name: 'type', operator: search.Operator.ANYOF, values: ['inventoryadjustment']}
    ]
    }).run.each(result => {
    lines.push({
    tranid: result.getValue('tranid'),
    trandate: result.getValue('trandate'),
    item: result.getValue('item'),
    amount: result.getValue('amount')
    }
    )
    });
  };
  return {
    get: _get
};
})
;
Still error
c
@Muhammad Hafiz have you looked at your code in an IDE?
message has been deleted
m
I use visual studio code, but it show no error
c
what error is NS giving you?
To be fair, I'm not sure if that error is a big deal or just a soft warning. You can't have a variable name the same as a funciton parameter name.
m
It says syntax error
c
try changing line 11 to let lineItems = [];
then change line 24 to lineItems.push({
m
message has been deleted
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType Restlet
 */
 define([
  'N/record',
  'N/search'
],
function(search){
  function _get(lines) {
    let lineItems = [];
    search.create({
    type: 'transaction',
    columns: [
    'tranid',
    'trandate',
    'item',
    'amount'
    ],
    filters: [
    {name: 'type', operator: search.Operator.ANYOF, values: ['inventoryadjustment']}
    ]
    }).run.each(result => {
    lineItems.push({
    tranid: result.getValue('tranid'),
    trandate: result.getValue('trandate'),
    item: result.getValue('item'),
    amount: result.getValue('amount')
    }
    )
    });
  };
  return {
    get: _get
};
})
;
It still show the same error
c
Well, the fix I gave has just fixed your second issue.. you still have the first issue of the syntax error though
m
c
use the SDF template for your reslet
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */
define(['N/search'],

    (search) => {

        const get = (requestParams) => {
            let lines = [];
            search.create({
                type: 'transaction',
                columns: [
                    'tranid',
                    'trandate',
                    'item',
                    'amount'
                ],
                filters: [
                    {name: 'type', operator: search.Operator.ANYOF, values: ['inventoryadjustment']}
                ]
            }).each(result => {
                lines.push({
                        tranid: result.getValue('tranid'),
                        trandate: result.getValue('trandate'),
                        item: result.getValue('item'),
                        amount: result.getValue('amount')
                    }
                )
            });
        }

        return {get}

    });
m
It successfully uploaded
j
for me, the "missing ; before statement" is almost always fixed by making sure the script type is set to 2.1 at the top instead of 2.x.
m
If I use the previous code and change the script type, it successfully uploaded too but return error while I try open the url : error code: TypeError error message: TypeError: Cannot read property 'ANYOF' of undefined [at Object._get (/SuiteScripts/restlet-getall.js2130)]
m
At the top of your script. You are calling 2 modules, but only passing one into the function. So
search
was in fact calling the
N/record
module instead of the
N/search
module.
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType Restlet
 */
 define([
  'N/record',
  'N/search'
],
function(search)
Should be
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType Restlet
 */
 define([
  'N/record',
  'N/search'
],
function(record, search)
c
@Marvin I fixed that for him in my rewrite of his script. I don't think he used my version otherwise that ENUM wouldn't have thrown an error due to calling the wrong module.
m
@Craig yeah I think he said in his followup comment that he just changed the script version in his and the error seemed to point that he didn't use your version.
m
I try using craig's code and this error appeared :
m
It was missing the
.run()
. You can do the search a couple ways and I prefer the way Craig wrote it unless there is a good reason to do another way.
Copy code
const transaction_search = search.create({...});

transaction_search.run().each(result => {...});
or
Copy code
search.create({...}).run().each(results => {...});
Try below.
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */
// eslint-disable-next-line no-undef
define(['N/search'],
    /**
     * @param{search} search
     */
    (search) => {
        /**
         * Defines the function that is executed when a GET request is sent to a RESTlet.
         * @param {Object} requestParams - Parameters from HTTP request URL; parameters passed as an Object (for all supported content types)
         * @returns {string | Object} HTTP response body; returns a string when request Content-Type is 'text/plain'; returns an
         *     Object when request Content-Type is 'application/json' or 'application/xml'
         * @since 2015.2
         */
        // eslint-disable-next-line no-unused-vars
        const get = (requestParams) => {
            const lines = [];
            search.create({
                type: 'transaction',
                columns: [
                    'tranid',
                    'trandate',
                    'item',
                    'amount'
                ],
                filters: [
                    { name: 'type', operator: search.Operator.ANYOF, values: ['inventoryadjustment'] }
                ]
            })
                .run()
                .each(result => {
                    lines.push({
                        tranid: result.getValue('tranid'),
                        trandate: result.getValue('trandate'),
                        item: result.getValue('item'),
                        amount: result.getValue('amount')
                    });
                    return true;
                });

        };

        return { get };

    });
m
I try your code and now it return blank
m
Add this to before the closing parentheses of the
get
function.
Copy code
return JSON.stringify(lines);
m
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */
// eslint-disable-next-line no-undef
define(['N/search'],
    /**
     * @param{search} search
     */
    (search) => {
        /**
         * Defines the function that is executed when a GET request is sent to a RESTlet.
         * @param {Object} requestParams - Parameters from HTTP request URL; parameters passed as an Object (for all supported content types)
         * @returns {string | Object} HTTP response body; returns a string when request Content-Type is 'text/plain'; returns an
         *     Object when request Content-Type is 'application/json' or 'application/xml'
         * @since 2015.2
         */
        // eslint-disable-next-line no-unused-vars
        const get = (requestParams) => {
            const lines = [];
            search.create({
                type: 'transaction',
                columns: [
                    'tranid',
                    'trandate',
                    'item',
                    'amount'
                ],
                filters: [
                    { name: 'type', operator: search.Operator.ANYOF, values: ['inventoryadjustment'] }
                ]
            })
                .run()
                .each(result => {
                    lines.push({
                        tranid: result.getValue('tranid'),
                        trandate: result.getValue('trandate'),
                        item: result.getValue('item'),
                        amount: result.getValue('amount')
                    });
                    return true;
                });
                return JSON.stringify(lines);
        };
        
        return { get };

    });
Now it return an empty array
m
I am not entirely sure that the value in the filter is correct. What I usually do is comment out that filter and return
type
in the columns. That way I can find the enum value.
m
Oh it success. Like you said, there's mistake in type. It must be : InvAdjst. Thank you, Marvin Roman, Craig, JessicaL and NElliott
👍 2