javascriptnetsuitesuitescriptsuitescript2.0user-event

How to avoid SSS_REQUEST_TIME_EXCEEDED on HTTPS.post


I have a user event script which executes on create of sales order and sends a HTTPS POST to our integration URL on Mulesoft. It seems NetSuite throws SSS_REQUEST_TIME_EXCEEDED error within 45 secs even if API call was successful. if there a way to increase these 45 sec and run it behind the scene? should i create a suitelet and call from userevent when record is created?

FYI. Post call is successful every time but script throws this error. user event code:

function afterSubmit(context) {
if (context.type !== context.UserEventType.CREATE)
return;
try{
    var envtype = JSON.stringify(runtime.envType)
    var accountId = runtime.accountId
    var isProduction = (accountId == '12345')
    log.debug({title: 'Before Post' }) 
    
    
    
    var tokenrecord =  record.load({
        type: 'customrecord_mulesoft_setup',
        id: 1,
        isDynamic: true,
    });
    
    var token = tokenrecord.getValue({
        fieldId: 'custrecord_sandbox_token_so_api'
    })
    log.debug({title : 'token' , details: token});
    
    var headers = {
        'Content-Type' : 'application/json',
        Authorization : isProduction ? 'Bearer ' + token : 'Bearer ' + token,
        'User-Agent': 'NetSuite/2022',
        Accept: '*/*',
        Host : isProduction ? 'test.com' : 'test.com',
        'Accept-Encoding': 'gzip, deflate, br',
        Connection :'keep-alive'            
    }
    
    var salesorderid = context.newRecord.id;
    if (isProduction)      {
        apiNum = 'xxxxxx';
        endPointUrl = 'https://test.com/' + salesorderid;               } 
        else {
            
            apiNum = 'yyyy';  // all updates
            endPointUrl = 'https://test.com/' + salesorderid;
        }
        
        
        var postedObj = { /*
        parameters : [
            {
                "Key": "ns_internal_id",
                "Value": v_intid
            } 
        ] */
    }                 
    
    var jsonify = JSON.stringify(postedObj);
    
    log.debug({title : 'url' , details: endPointUrl});
    log.debug({title : 'headers' , details: headers});
    log.debug({title : 'body' , details: jsonify});
    
    
    var resp=https.post({
        url: endPointUrl, 
        headers: headers,
        body: jsonify
    });
    
    var id = record.submitFields({
        type: record.Type.SALES_ORDER,
        id: salesorderid,
        values: {
            custbody_sent_to_mulesoft: true
        }
    });
}
catch(e){
    log.error({title : 'Error Message' , details: e.message});
    log.error({title : 'Error Code' , details: e.name});

log.debug({title: 'Headers' + 'resp.headers' })

    log.error({title : 'Could not send item to SF !!' , details: JSON.stringify(resp)});
    log.error({title : 'Could not send item to SF !!' , details: JSON.stringify(jsonify)})
    

  }
return;

Error: Message: The host you are trying to connect to has exceeded the maximum allowed response time.
Code: SSS_REQUEST_TIME_EXCEEDED


Solution

  • Firstly, I wouldn't recommend using an API call that could potentially take a long time (more than a couple of seconds) to come back in a UserEvent script because it would drastically impact user experience.

    To answer your question, you should implement a retry function to call the API multiple times if the first request fails. There is an supporting document that explains this and similar issues on the SuiteAnswers page. https://suiteanswers.custhelp.com/app/answers/detail/a_id/32729/loc/en_US

    function retry(expectedErrors, task, retries=1) {
        try {
            return task();
        } catch (err) {
            if (retries === 0) throw err;
            if (expectedErrors.length && !expectedErrors.includes(err.name)) throw err;
            return retry(expectedErrors, task, --retries);
        }
    }
    retry(["SSS_REQUEST_TIME_EXCEEDED"], function() {
        // your API request here
    });