Tuesday, October 23, 2018

Suitelet returns Error 206 Partial Content when referenced in an External Form

The 206 partial content error will be returned by a Suitelet called in a Online Customer form if the following settings on the script deployment are not properly set:

Deployed = T
Status = Released
Execute as Role = Administrator
Available Without Login = T

Audience tab:
All Roles =
T or pick select roles

Below is a sample Suitelet and Online Form HTML template to test this:

1. Create a Suitelet //To look up for Company name if it matches an existing email address

/*************************************
*The Suitelet function searches for customer records
* that match a supplied email parameter
* and display company name to an online form
*/

function lookupCompanyName(request, response)
{
    if (request.getMethod() === 'GET')
    {
        if (request.getParameter('email') != null)
        {
         var company = '';
            var entityName;    // Result set of query
            var filters = [];     
            filters[0] = new nlobjSearchFilter('email', null, 'is', request.getParameter('email'));
   
   var columns = [];
            // Set columns to return in result set
            columns[0] = new nlobjSearchColumn('entityid');
            columns[1] = new nlobjSearchColumn('companyname');
           
            entityName = nlapiSearchRecord('customer', null, filters, columns); 
           
            if (entityName !== null)
            {
             entityName = entityName[0];
                company = entityName.getValue('companyname');
                nlapiLogExecution('DEBUG', 'Company name1: ', company);                           
            }
            response.write(company);
       }
    }
}

2. Deploy the script and set the following.
Deployed = T
Status = Released
Execute as Admin = T
Available Without Login = T

Audience tab:
All Roles =
T or pick select roles

* The External URL of the Suitelet will be used on the Online Form.

3. Create an Online Customer Form. Use Form Type - Custom HTML template (Setup > Marketing > Online Customer Form > New)

< html xmlns="http://www.w3.org/1999/xhtml">
< head>
< script type='text/javascript'>
    function createXHR() {
        var xhr;
        try {
            xhr = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            try {
                xhr = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (E) {
                xhr = false;
            }
        }

        if (!xhr && typeof XMLHttpRequest != 'undefined') {
            xhr = new XMLHttpRequest;
        }

        return xhr;
    }           

    // Function to check for existing emails.
    function displayCompanyName() {

        var xhr;    // XMLHttpRequest
        var custemail;  // email value from form
        var url;    // URL of XMLHttpRequest
        custemail = document.getElementById('email').value;     
        url  = 'https://forms.netsuite.com/app/site/hosting/scriptlet.nl?script=50&deploy=1&compid=TSTDRV594044&h=c00ecbe7ea8f49739c6a&email=' + custemail;
        xhr = createXHR();
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4)
   {
    alert("status: "+xhr.status); //alert to check status
                if (xhr.status == 200)
    {
     alert("status: "+xhr.status); //alert to check status     
     document.getElementById('companyname').value = xhr.responseText;
                }  
            }
        }        
        xhr.open("GET", url,true);
        xhr.send(null);
    }
< /script>
< /head>
< body>
< NLFORM>
< form>
Company name:
< NLCOMPANYNAME>
< br />
< label for="email">* Email Address: <span id="emailFound"></span></label>
< NLEMAIL>
<br />
< input type="button" name="Verify" value="Display Company Name" id="submitForm" onclick="displayCompanyName();" />
< /form>
 < /body>
 < /html>
 < /NLFORM>


*This is for SuiteScript version 1.0



DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Netsuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.

Netsuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Netsuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.

Netsuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.

No comments:

Post a Comment