i have a scheduled script that stops processing wh...
# suitescript
l
i have a scheduled script that stops processing when it encounters an error. how do i prevent that from happening?
d
What is the error and why can you not change the script logic so the context is no longer an error condition?
l
the script is going through some custom records and doing an API call. but when i get an error like, Cannot read property "statusByLocale" from undefined, script stops executing
d
The obvious solution would be to check the existence of the response object (or value within it) with an if statement before referencing the property. If you cant do that, wrap the problem code block in a try/catch statement.
l
thats the crazy thing. i have it in a try/catch and it still stops executing.
b
what does the code look like
l
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */
define(["N/search", "N/record", "N/email", "N/runtime", "N/https", "N/format"],
    function(search, record, email, runtime, https, format) {
        function Fedex_Tracking_Status(context) {
            try {
              
              
                // Authorization
              
                    //API Authorization
                var Auth_Headers = {};
                Auth_Headers['Content-Type'] = 'application/x-www-form-urlencoded';

                var req_body = {
                    "grant_type": "client_credentials",
                    "client_id": "l7323984b6ecf04b81afa701d1f19358b4",
                    "client_secret": "3b36737c3b9942e89075d00cc29fb4e6"
                }


                var auth_response = https.request({
                    method: <http://https.Method.POST|https.Method.POST>,
                    url: '<https://apis.fedex.com/oauth/token>',
                    body: req_body,
                    headers: Auth_Headers
                });

                var auth_token = JSON.parse(auth_response.body).access_token
                auth_token = 'Bearer ' + auth_token;

              
              
                //Run a search 

            var customrecord_pacejet_package_infoSearchObj = search.create({
   type: "customrecord_pacejet_package_info",
   filters:
   [
      //["custrecord_pacejet_transaction_link.shipmethod","anyof","137298","240365","137299","139407","137300","240175","168351","7313","168352","7205","186636","76181","186631","139727","241931","241920","89083","7316","168353","240458","139174","7315","170297"], 
     // "AND", 
      ["custrecord_pacejet_transaction_link.type","anyof","ItemShip"], 
      "AND", 
      [["custrecord_tracking_status","is","initiated"],"OR",["custrecord_tracking_status","isempty",""]], 
      "AND", 
      ["created","on","today"]
   ],
   columns:
   [
      search.createColumn({name: "internalid", label: "Internal ID"}),
      search.createColumn({name: "created", label: "Date Created"}),
      search.createColumn({
         name: "name",
         sort: search.Sort.ASC,
         label: "Name"
      }),
      search.createColumn({name: "custrecord_tracking_status", label: "Tracking Status"}),
      search.createColumn({name: "custrecord_pacejet_package_tracking", label: "Tracking Number"}),
      search.createColumn({name: "custrecord_pacejet_package_tracking_link", label: "Tracking Link"}),
      search.createColumn({name: "custrecord_pacejet_package_weight", label: "Weight"}),
      search.createColumn({name: "custrecord_pacejet_package_contents", label: "Contents"}),
      search.createColumn({name: "custrecord_pacejet_transaction_link", label: "PJ Package Transaction Link"})
   ]
});

               var results = customrecord_pacejet_package_infoSearchObj.run();
               var resultsRange = results.getRange(0, 1000);
              
               for(var i=0; i<resultsRange.length; i++){
                
                 var id = resultsRange[i].getValue({
                   name: "internalid",
                   label: "Internal ID"
                 });
                 
                  var tracking_number = resultsRange[i].getValue({
                   name: "custrecord_pacejet_package_tracking",
                  label: "Tracking Number"
                 });
                 
                   var curr_rec = record.load({
                         type: 'customrecord_pacejet_package_info',
                         id: id,
                         isDynamic: true
                  });
                 
                
                // Get Tracking Status
                Auth_Headers['Content-Type'] = 'application/json';
                Auth_Headers['Authorization'] = auth_token;
                Auth_Headers['Content-Length'] = '0';
                Auth_Headers['Host'] = '<calculated when request is sent>';
                Auth_Headers['User-Agent'] = 'PostmanRuntime/7.28.4';
                Auth_Headers['Accept'] = '*/*';
                Auth_Headers['Accept-Encoding'] = 'gzip, deflate, br';
                Auth_Headers['Connection'] = 'keep-alive';

                var track_status_body = {
                    "includeDetailedScans": true,
                    "trackingInfo": [{ // - 285909110949
                        "trackingNumberInfo": {
                            "trackingNumber": tracking_number,
                            "carrierCode": "FDXE"
                        }
                    }]
                }


                var track_status_response = https.request({
                    method: <http://https.Method.POST|https.Method.POST>,
                    url: '<https://apis.fedex.com/track/v1/trackingnumbers>',
                    body: JSON.stringify(track_status_body),
                    headers: Auth_Headers
                });


                 var status = JSON.parse(track_status_response.body).output.completeTrackResults[0].trackResults[0].latestStatusDetail.statusByLocale

                curr_rec.setValue({
                  fieldId: 'custrecord_tracking_status',
                  value: status
                });

                 curr_rec.save({
                    enableSourcing: true,
                    ignoreMandatoryFields: true
                });
                 
               }


            } catch (e) {
                log.error({
                    title: "Error",
                    details: e
                });

            }
        }
        return {
            execute: Fedex_Tracking_Status
        };
    });
its line "var status = ....."
b
your code catches the error, logs it and then does nothing afterwards
so there is basically nothing else to do after catching the error so the script is done
l
how do i tell it to skip that record, and move on to the next one
b
try catch inside the loop, not outside
l
Thank you