Transform Quote to Sales Order — Restlet

Restlet to create a sales order from Quote. The Quote document number and the other field which need to be updated will be received in the NetSuite side. In the NetSuite side, the Quote internal id will be found using saved search and then using transform function, Quote is been transformed into the Sales order.

/*******************************************************************************
 * 
 * Developer: Suravi Lahiri & Anukaran Development Head: Mr. Sumeet Kulkarni
 * Company Name: CEINO Purpose: Getting value from an existing Quotation &
 * transforming it to sales order & setting the value of shipping
 * address,billing address and quotation id into sales order
 * 
 * © Copyright All Right
 * 
 ******************************************************************************/

function POST_data_transform(datain) {
    nlapiLogExecution('DEBUG', 'RESTLET GET', 'IN POST');
    var quoteObj = datain.data;
    var responseObj = new Array();
    responseObj['message'] = '';
    responseObj['responseCode'] = '';
    responseObj['customer_name'] = '';
    responseObj['date'] = '';
    responseObj['status'] = '';
    responseObj['probablity'] = '';
    responseObj['expect_date'] = '';
    responseObj['shiping_carrier'] = '';
    responseObj['shipaddress'] = '';
    responseObj['billaddress'] = '';
    responseObj['line_item'] = '';
    responseObj['line_item_amt'] = '';
    responseObj['department'] = '';
    responseObj['shipping_cost'] = '';

    // Start:LMS Dropbox
    var quote_id = datain.quote_id; // (send by other company)

    var cust = datain.cust;
    nlapiLogExecution('DEBUG', 'RESTLET POST', '	==' + cust);

    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 memo = datain.memo; // add memo for postman

    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 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

    nlapiLogExecution('DEBUG', 'RESTLET GET', 'quote id==' + quote_id);
    if (quote_id != null || quote_id != '') {
        var filter = new Array();
        filter[0] = new nlobjSearchFilter('tranid', null, 'is', quote_id);
        var quote_search_result = nlapiSearchRecord('estimate', null, filter,
            null); // creating save search for customer
        nlapiLogExecution('DEBUG', 'PBTI-log', 'Search result = ' +
            quote_search_result);
        if (quote_search_result != "" || quote_search_result != null) {
            var quote_int_id = quote_search_result[0].getId(); // getting
            // existing
            // quote
            // internal id
            nlapiLogExecution('DEBUG', 'PBTI-log', 'Quote ID = ' + quote_int_id);

            var rec_quote = nlapiLoadRecord('estimate', quote_int_id); // load
            // record
            nlapiLogExecution('DEBUG', 'PBTI-log', 'LoadRecord =' + rec_quote);
            var quote_cust_name = rec_quote.getFieldText('entity'); // get
            // customer
            // name
            nlapiLogExecution('DEBUG', 'PBTI-log', 'Customer Name =' +
                quote_cust_name);
            var quote_date = rec_quote.getFieldValue('trandate'); // get date
            nlapiLogExecution('DEBUG', 'PBTI-log', 'Existing Date =' +
                quote_date);
            var quote_status = rec_quote.getFieldValue('entitystatus'); // get
            // status
            nlapiLogExecution('DEBUG', 'PBTI-log', 'Existing Status =' +
                quote_status);
            var quote_probablity = rec_quote.getFieldValue('probability'); // get
            // probability
            nlapiLogExecution('DEBUG', 'PBTI-log', 'Existing Probablity =' +
                quote_probablity);
            var quote_expect_date = rec_quote
                .getFieldValue('expectedclosedate'); // get
            // expectedclosedate
            nlapiLogExecution('DEBUG', 'PBTI-log', 'Existing quote date =' +
                quote_expect_date);
            var quote_shiping_carrier = rec_quote.getFieldValue('shipcarrier'); // get
            // shipcarrier
            nlapiLogExecution('DEBUG', 'PBTI-log', 'ShipCarrier =' +
                quote_shiping_carrier);
            var quote_ship_address = rec_quote.getFieldValue('shipaddress'); // get
            // ship
            // address
            nlapiLogExecution('DEBUG', 'PBTI-log', 'quote_ship_address =' +
                quote_ship_address);
            var quote_bill_address = rec_quote.getFieldValue('billaddress'); // get
            // Bill
            // address
            nlapiLogExecution('DEBUG', 'PBTI-log', 'quote_bill_address =' +
                quote_bill_address);
            var quote_lineitem_count = rec_quote.getLineItemCount('item');
            nlapiLogExecution('DEBUG', 'PBTI-log', 'LineCount =' +
                quote_lineitem_count);
            var line_number = quote_lineitem_count;
            nlapiLogExecution('DEBUG', 'PBTI-log', 'LineNumber =' + line_number);
            var department = rec_quote.getFieldValue('department');;
            nlapiLogExecution('DEBUG', 'PBTI-log', 'department =' + department);
            var shipping_cost = rec_quote.getFieldValue('shippingcost');;
            nlapiLogExecution('DEBUG', 'PBTI-log', 'shipping_cost =' +
                shipping_cost);
            var j = 1;
            var item_id = new Array();
            var amt = new Array();
            for (var i = 1; i <= line_number; i++) {

                // item_id = rec_quote.getLineItemValue('item','item',i);
                item_id.push(rec_quote.getLineItemValue('item', 'item', i));
                nlapiLogExecution('DEBUG', 'RESTLET POST', 'Item_id==' +
                    item_id[i]);
                amt.push(rec_quote.getLineItemValue('item', 'amount', i));
                nlapiLogExecution('DEBUG', 'RESTLET POST', 'Amount==' + amt[i]);
                j++;
                rec_quote.commitLineItem('item');

            }
            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);

            nlapiLogExecution('DEBUG', 'addShipInternalId-log',
                'addShipInternalId = ' + addShipInternalId);
            nlapiLogExecution('DEBUG', 'addInternalId-log', 'addInternalId = ' +
                addInternalId);
            var transform_from_quote_to_sales_order = nlapiTransformRecord(
                'estimate', quote_int_id, 'salesorder'); // transforming
            // Quotation to
            // Sales Order
            nlapiLogExecution('DEBUG', 'PBTI-log',
                'transform_from_quote_to_sales_order = ' +
                transform_from_quote_to_sales_order);
            transform_from_quote_to_sales_order.setFieldValue('shipaddress',
                'ANU_TEST kolkatta WEST BANGAL'); // setting the shipping
            // address in sales
            // order
            transform_from_quote_to_sales_order.setFieldValue('billaddress',
                'ANU_TEST kolkatta WEST BANGAL'); // setting the billing
            // address in sales
            // order
            transform_from_quote_to_sales_order.setFieldValue('createdfrom',
                quote_int_id); // setting the quotation ID in sales order
            transform_from_quote_to_sales_order.setFieldValue('memo', memo); // set
            // field
            // value
            // memo
            // in
            // record
            nlapiLogExecution('DEBUG', 'Anukaran-log', 'MEMO = ' + memo);
            transform_from_quote_to_sales_order.setFieldValue(
                'custbody_nugen_contact', contact);
            nlapiLogExecution('DEBUG', 'Suravi-log', 'contact = ' + contact);
            transform_from_quote_to_sales_order.setFieldValue('otherrefnum',
                po_hash);
            nlapiLogExecution('DEBUG', 'Suravi-log', 'PO# = ' + po_hash);
            transform_from_quote_to_sales_order.setFieldValue(
                'custbody_nugen_po_document', po_doc);
            nlapiLogExecution('DEBUG', 'Suravi-log', 'PO_document = ' + po_doc);
            // custbody_nugen_is_online
            transform_from_quote_to_sales_order.setFieldValue(
                'custbody_nugen_is_online', 'T');
            // Add Billing & SHipping Address List

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

            transform_from_quote_to_sales_order.setFieldValue('custbody3',
                attentionTo);

            var record_id = nlapiSubmitRecord(
                transform_from_quote_to_sales_order, false, true);

            var tranid = nlapiLookupField('salesorder', record_id, 'tranid');

            nlapiLogExecution('DEBUG', 'PBTI-log', 'Record Sbubmited =' +
                record_id);
        } else {
            responseObj['message'] = 'Record not added';
        }
    }

    responseObj['message'] = 'Record added';
    responseObj['responseCode'] = '1'; // manual code any thing instead of 1
    // responseObj['record_id'] = record_id; // when create record then its
    // mendatory
    // responseObj['Sales_Id']=nlapiLookupField('estimate', record_id,
    // 'tranid');
    responseObj['customer_name'] = quote_cust_name; // take any variable name
    // this side = take upper
    // variable name this side
    responseObj['date'] = quote_date; // take any variable name this side =
    // take upper variable name this side
    responseObj['status'] = quote_status; // take any variable name this side
    // = take upper variable name this
    // side
    responseObj['probablity'] = quote_probablity // take any variable name
    // this side = take upper
    // variable name this side
    responseObj['expect_date'] = quote_expect_date; // take any variable name
    // this side = take upper
    // variable name this side
    responseObj['shiping_carrier'] = quote_shiping_carrier; // take any variable
    // name this side =
    // take upper
    // variable name
    // this side
    responseObj['shipaddress'] = quote_ship_address; // take any variable
    // name this side = take
    // upper variable name
    // this side
    responseObj['billaddress'] = quote_bill_address; // take any variable
    // name this side = take
    // upper variable name
    // this side
    responseObj['line_item'] = item_id; // take any variable name this side =
    // take upper variable name this side
    responseObj['line_item_amt'] = amt;
    responseObj['department'] = department; // take any variable name this side
    // = take upper variable name this
    // side
    responseObj['shipping_cost'] = shipping_cost;
    // take any variable name this side = take upper variable name this side

    // Making JSONobj -> var JSONObj =
    // {"message":responseObj['message'],"responseCode":responseObj['responseCode'],"data":[{"Label:responseObj['VariableName
    // Those taken above']},{"Label2:responseObj['VariableName Those taken
    // above2']}]}

    var JSONObj = {
        "message": responseObj['message'],
        "responseCode": responseObj['responseCode'],
        "data": [{
            "rec_id": record_id
        }, {
            "salesorder": tranid
        }]
    };

    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',
        'addressbookaddress_text', 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 *