Restlet suite script 1 — Sales Order creation.

A Restlet script which is written in Suite script 1 that creates a sales order from external sources. Here it is from the Magento website the API calls are occurring.

/*******************************************************************************
 * 
 * Developer: SURAVI LAHIRI Development Head: Mr. Sumeet Kulkarni Company Name:
 * Ceino Purpose: Create a Sales Order Updated: Date:15/09/17 Change Summary:
 * Added Item rate in place of item amount. Date:18/09/17 Change Summary:
 * Multiplied Item rate with item quantity. by anukaran Date:21/09/2017 Change
 * Summary: set memo field data by anukaran. Date:26/09/2017 Change Summary:
 * added customer contact id by suravi lahiri. Date:26/09/2017 Change Summary:
 * added paymentevent method details: a. Card Type(Visa, Master, etc) b. Card
 * Number c. Expiry d. Name on card e. Street f. Zip code g. CVV by anukaran &
 * suravi lahiri Date:04/10/2017 Change Summary: Added PO# & PO doc field by
 * anukaran & suravi lahiri
 * 
 * © Copyright All Right
 * 
 ******************************************************************************/
function POST_data(datain) {
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'IN POST');
    var customerObj = datain.data;
    var responseObj = new Array();
    responseObj['message'] = '';
    responseObj['responseCode'] = '';
    responseObj['rec_id'] = '';
    responseObj['Sales_Id'] = '';
    responseObj['memo'] = ''; // memo no
    responseObj['contact_id'] = '';
    responseObj['po_hash'] = ''; // PO#
    responseObj['card_type'] = ''; // card type
    responseObj['attention'] = '';
    // nlapiLogExecution('DEBUG','RESTLET
    // POST','customerObj=='+customerObj.length);

    // Start:LMS Dropbox
    var cust = datain.cust;
    nlapiLogExecution('DEBUG', 'RESTLET POST', '	==' + cust);
    var sub = datain.sub;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'subsidiary==' + sub);
    var contact = datain.contact;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'contact==' + contact);
    var po_hash = datain.po_hash;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'po_hash==' + po_hash);
    var po_doc = datain.po_doc;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'po_doc==' + po_doc);
    var card_type = datain.card_type;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'card type==' + card_type);
    var card_number = datain.card_number;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'card number==' + card_number);
    var card_name = datain.card_name;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'card name==' + card_name);
    var card_expiry = datain.card_expiry;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'card expiry==' + card_expiry);
    var card_street = datain.card_street;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'card street==' + card_street);
    var card_zip_code = datain.card_zip_code;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'card zipcode==' + card_zip_code);
    var card_cvv = datain.card_cvv;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'card cvv==' + card_cvv);
    var attentionTo = datain.attention;
    var quotenumber = datain.quote;
    nlapiLogExecution('DEBUG', 'RESTLET POST quotenumber', 'quotenumber==' +
        quotenumber);
    // Bill Address

    var billCountry = datain.billcountry; // billcountry
    var billAttention = datain.billattention; // billattention
    var billaddressee = datain.billaddressee; // billaddressee
    var billPhone = datain.billphone; // billphone
    var billadd1 = datain.billadd1; // billadd1
    var billadd2 = datain.billadd2; // billadd2
    var billcity = datain.billcity; // billcity
    var billstate = datain.billstate; // billstate
    var billzip = datain.billzip; // billzip
    var shippingtaxcode = datain.shippingTaxCode;
    // Ship Address

    var shipCountry = datain.shipcountry; // shipcountry
    var shipAttention = datain.shipattention; // shipattention
    var shipaddressee = datain.shipaddressee; // shipaddressee
    var shipPhone = datain.shipphone; // shipphone
    var shipadd1 = datain.shipadd1; // shipadd1
    var shipadd2 = datain.shipadd2; // shipadd2
    var shipcity = datain.shipcity; // shipcity
    var shipstate = datain.shipstate; // shipstate
    var shipzip = datain.shipzip; // shipzip

    // Ship Address
    var item = new Array();
    // var dept = datain.dept;
    var ship = datain.ship_cost;
    var memo = datain.memo; // add memo for postman
    nlapiLogExecution('DEBUG', 'Anukaran RESTLET POST', 'memo=' + memo);

    var item = datain.item;
    // item['item_id']=datain.item.item_id;
    // item['item_price']=datain.item[item_price];
    var len = item.length;
    nlapiLogExecution('DEBUG', 'RESTLET POST', 'item_len==' + len);
    /*
     * if(po_hash == null && card_type == null) { responseObj['message'] =
     * 'Record not added as PO# or card_type field is empty'; }
     */
    // Add Address to customer record
    var custRec = nlapiSearchRecord('customer', null, [new nlobjSearchFilter(
        'internalid', null, 'anyof', cust)], null);
    var CustRecId = custRec[0].getId();
    var addInternalId = addAddress(CustRecId, billCountry, billAttention,
        billaddressee, billPhone, billadd1, billadd2, billcity, billstate,
        billzip);
    var addShipInternalId = addShipAddress(CustRecId, shipCountry,
        shipAttention, shipaddressee, shipPhone, shipadd1, shipadd2,
        shipcity, shipstate, shipzip);
    // Add Address to customer Record
    nlapiLogExecution('DEBUG', 'addShipInternalId-log', 'addShipInternalId = ' +
        addShipInternalId);
    nlapiLogExecution('DEBUG', 'addInternalId-log', 'addInternalId = ' +
        addInternalId);

    var rec = nlapiCreateRecord('salesorder');
    nlapiLogExecution('DEBUG', 'Suravi-log', 'rec = ' + rec);
    rec.setFieldValue('entity', cust);

    // rec.setFieldValue('createdfrom',quotenumber);

    nlapiLogExecution('DEBUG', 'Suravi-log', 'cus = ' + cust);
    rec.setFieldValue('department', 16);
    // nlapiLogExecution('DEBUG','Suravi-log','Dept = '+dept);
    rec.setFieldValue('shippingcost', ship);
    nlapiLogExecution('DEBUG', 'Suravi-log', 'Ship = ' + ship);
    rec.setFieldValue('subsidiary', 1);
    // nlapiLogExecution('DEBUG','Suravi-log','sub = '+sub);
    rec.setFieldValue('memo', memo); // set field value memo in record
    nlapiLogExecution('DEBUG', 'Anukaran-log', 'MEMO = ' + memo);
    rec.setFieldValue('custbody_nugen_contact', contact);
    nlapiLogExecution('DEBUG', 'Suravi-log', 'contact = ' + contact);
    rec.setFieldValue('otherrefnum', po_hash);
    nlapiLogExecution('DEBUG', 'Suravi-log', 'PO# = ' + po_hash);
    rec.setFieldValue('custbody_nugen_po_document', po_doc);
    nlapiLogExecution('DEBUG', 'Suravi-log', 'PO_document = ' + po_doc);
    // custbody_nugen_is_online
    rec.setFieldValue('custbody_nugen_is_online', 'T');
    /*
     * var paymentevent = rec.selectNewLineItem('paymenteventevent');
     * nlapiLogExecution('DEBUG','Anukaran-Log','tabselected=' +paymentevent);
     * var paymentevent_type =
     * rec.setLineItemValue('paymenteventevent','paymenteventmethod','1',card_type);
     * nlapiLogExecution('DEBUG','Anukaran-Log','paymentevent type='
     * +paymentevent_type); var card_no =
     * rec.setLineItemValue('paymenteventevent','custbody_ceino_card_number','1',card_number);
     * nlapiLogExecution('DEBUG','Anukaran-Log','card_number=' +card_no); var
     * card_exp =
     * rec.setLineItemValue('paymentevent','ccexpiredate','1',card_expiry);
     * nlapiLogExecution('DEBUG','Anukaran-Log','Card Exp=' +card_exp); var
     * card_name = rec.setLineItemValue('paymentevent','ccname','1',card_name);
     * nlapiLogExecution('DEBUG','Anukaran-Log','Card Name=' +card_name); var
     * card_zip =
     * rec.setLineItemValue('paymentevent','cczipcode','1',card_zip_code);
     * nlapiLogExecution('DEBUG','Anukaran-Log','Pin Code=' +card_zip); var
     * card_street =
     * rec.setLineItemValue('paymentevent','ccstreet','1',card_street);
     * nlapiLogExecution('DEBUG','Anukaran-Log','Card Street=' +card_street);
     * var card_cvv =
     * rec.setLineItemValue('paymentevent','custbody_ceino_cvv','1',card_cvv);
     * nlapiLogExecution('DEBUG','Anukaran-Log','CVV No=' +card_cvv);
     */

    // Add Billing & SHipping Address List
    rec.setFieldValue('billaddresslist', addInternalId);
    rec.setFieldValue('shipaddress', addShipInternalId);
    // Add Billing & Shipping Address List

    // CC Payment Details
    rec.setFieldText('paymentmethod', card_type);
    nlapiLogExecution('DEBUG', 'Anukaran-log', 'card_type = ' + card_type);
    rec.setFieldValue('ccnumber', card_number);
    nlapiLogExecution('DEBUG', 'Anukaran-log', 'card_number = ' + card_number);
    rec.setFieldValue('ccexpiredate', card_expiry);
    nlapiLogExecution('DEBUG', 'Anukaran-log', 'card_expiry = ' + card_expiry);
    rec.setFieldValue('ccname', card_name);
    nlapiLogExecution('DEBUG', 'Suravi-log', 'card_name = ' + card_name);
    rec.setFieldValue('cczipcode', card_zip_code);
    nlapiLogExecution('DEBUG', 'Suravi-log', 'card_zip_code = ' + card_zip_code);
    rec.setFieldValue('ccstreet', card_street);
    nlapiLogExecution('DEBUG', 'Suravi-log', 'card_street = ' + card_street);
    rec.setFieldValue('custbody_ceino_cvv', card_cvv);
    nlapiLogExecution('DEBUG', 'Suravi-log', 'card_cvv = ' + card_cvv);

    rec.setFieldValue('custbody3', attentionTo);
    rec.setFieldValue('shippingtaxcode', shippingtaxcode);


    var j = 1;
    for (var i = 0; i < len; i++) {

        rec.selectNewLineItem('item');
        rec.setCurrentLineItemValue('item', 'item', datain.item[i].item_id);
        // rec.setCurrentLineItemValue('item','price',4);
        rec.setCurrentLineItemValue('item', 'rate', datain.item[i].item_rate);

        rec.setCurrentLineItemValue('item', 'taxcode',
            datain.item[i].item_taxcode);
        rec.setCurrentLineItemValue('item', 'quantity',
            datain.item[i].item_quantity);

        var amt = (datain.item[i].item_rate) * (datain.item[i].item_quantity);
        rec.setCurrentLineItemValue('item', 'amount', amt);

        nlapiLogExecution('DEBUG', 'RESTLET POST', 'item[i][item_id]==' +
            datain.item[i].item_id);
        nlapiLogExecution('DEBUG', 'RESTLET POST', 'item[i][item_price]==' +
            datain.item[i].item_price);
        j++;
        rec.commitLineItem('item');

    }

    var rec_id = nlapiSubmitRecord(rec, false, true);
    nlapiLogExecution('DEBUG', 'Suravi-log', 'rec_id = ' + rec_id);
    if (po_hash == "" && card_type == "" || po_hash == null &&
        card_type == null) {
        nlapiLogExecution('DEBUG', 'Anukaran-Log', 'under null');
        responseObj['message'] = 'Record not added as PO# or card_type field is empty';
    } else

    {
        nlapiLogExecution('DEBUG', 'Anukaran-Log', 'under else');
        if (rec_id != null || rec_id != '') {
            responseObj['message'] = 'Record added';
            responseObj['responseCode'] = '1';
            responseObj['rec_id'] = rec_id;
            responseObj['Sales_Id'] = nlapiLookupField('salesorder', rec_id,
                'tranid');
            responseObj['memo'] = memo;
            responseObj['contact_id'] = contact;
            responseObj['po_hash'] = po_hash; // PO#
            responseObj['card_type'] = card_type; // Card type
        }
    }

    var JSONObj = {
        "message": responseObj['message'],
        "responseCode": responseObj['responseCode'],
        "data": [{
            "rec_id": responseObj['rec_id']
        }, {
            "Memo": responseObj['memo']
        }, {
            "Contact Id": responseObj['contact_id']
        }, {
            "PO##": responseObj['po_hash']
        }, {
            "Card type": responseObj['card_type']
        }, {
            "Sales_Id": responseObj['Sales_Id']
        }]
    }

    return JSONObj;
}

function addAddress(CustRecId, billCountry, billAttention, billaddressee,
    billPhone, billadd1, billadd2, billcity, billstate, billzip) {

    // Identify a customer.

    var customerId = CustRecId;

    // Load the customer record.

    var record = nlapiLoadRecord('customer', customerId, {
        recordmode: 'dynamic'
    });

    // Create a new address for the customer

    var addrSubrecord = record.createCurrentLineItemSubrecord('addressbook',
        'addressbookaddress');

    // Set the appropriate address subrecord fields.

    addrSubrecord.setFieldValue('country', billCountry);
    addrSubrecord.setFieldValue('isresidential', 'F');
    addrSubrecord.setFieldValue('attention', billAttention);
    addrSubrecord.setFieldValue('addressee', billaddressee);
    addrSubrecord.setFieldValue('addrphone', billPhone);
    addrSubrecord.setFieldValue('addr1', billadd1);
    addrSubrecord.setFieldValue('addr2', billadd2);
    addrSubrecord.setFieldValue('city', billcity);
    addrSubrecord.setFieldValue('state', billstate);
    addrSubrecord.setFieldValue('zip', billzip);

    // Commit the new address subrecord.

    addrSubrecord.commit();
    record.commitLineItem('addressbook');

    // Update the customer record.

    var custRecId = nlapiSubmitRecord(record);
    var readrecord = nlapiLoadRecord('customer', custRecId);
    var lastCount = readrecord.getLineItemCount('addressbook');
    var addressInternalId = readrecord.getLineItemValue('addressbook',
        'internalid', lastCount);
    /*
     * for(var x = 1; x <= readrecord.getLineItemCount('addressbook'); x++) { if
     * (readrecord.getLineItemValue('addressbook', 'addressee', x) ===
     * billaddressee) { addressInternalId =
     * readrecord.getLineItemValue('addressbook', 'internalid', x) break; } }
     */
    return addressInternalId;

}

function addShipAddress(CustRecId, shipCountry, shipAttention, shipaddressee,
    shipPhone, shipadd1, shipadd2, shipcity, shipstate, shipzip) {

    // Identify a customer.

    var customerId = CustRecId;

    // Load the customer record.

    var record = nlapiLoadRecord('customer', customerId, {
        recordmode: 'dynamic'
    });

    // Create a new address for the customer

    var addrSubrecord = record.createCurrentLineItemSubrecord('addressbook',
        'addressbookaddress');

    // Set the appropriate address subrecord fields.

    addrSubrecord.setFieldValue('country', shipCountry);
    addrSubrecord.setFieldValue('isresidential', 'F');
    addrSubrecord.setFieldValue('attention', shipAttention);
    addrSubrecord.setFieldValue('addressee', shipaddressee);
    addrSubrecord.setFieldValue('addrphone', shipPhone);
    addrSubrecord.setFieldValue('addr1', shipadd1);
    addrSubrecord.setFieldValue('addr2', shipadd2);
    addrSubrecord.setFieldValue('city', shipcity);
    addrSubrecord.setFieldValue('state', shipstate);
    addrSubrecord.setFieldValue('zip', shipzip);

    // Commit the new address subrecord.

    addrSubrecord.commit();
    record.commitLineItem('addressbook');

    // Update the customer record.

    var custRecId = nlapiSubmitRecord(record);
    var readrecord = nlapiLoadRecord('customer', custRecId);
    var lastCount = readrecord.getLineItemCount('addressbook');
    var addressInternalId = readrecord.getLineItemValue('addressbook',
        'addressbookaddress_text', lastCount);

    // nlapiLogExecution('debug','addressInternalId')
    /*
     * for(var x = 1; x <= readrecord.getLineItemCount('addressbook'); x++) { if
     * (readrecord.getLineItemValue('addressbook', 'addressee', x) ===
     * shipaddressee) { addressInternalId =
     * readrecord.getLineItemValue('addressbook', 'internalid', x) break; }
     */
    // }
    return addressInternalId;

}

Leave a comment

Your email address will not be published. Required fields are marked *