netsuitepurchase-ordersuitescript

How to create Purchase Order in Netsuite with ScriptSuite?


I am new to Netsuite and I was asked to perform a script that was launched from an application programmed in java. The script with a function to generate a Purchase Order in Netsuite and other function to list the Purchase Order created earlier. It turns out that for this I am using the api SuiteScript but when creating the Purchase Order run the java application and launches the script but it gives the following error:

Aug 03, 2015 2:49:00 PM com.gargoylesoftware.htmlunit.WebClient printContentIfNecessary INFO: {"error": {"code": "user_error", "message": "Please enter value (s) for: Vendor"}}

Javascript function to create is:

function CreatePurchase_Orders(datain){

var output = '';
nlapiLogExecution('DEBUG','createRecord','ingreso la consulta' ) ;
    nlapiLogExecution('DEBUG','createRecord', 'Ingresa: '+ datain);
//var msg = validateTimeBills(datain);
var msg = null;

if (msg){

    var err = new Object();
    err.status = "failed";
    err.message= msg;
    return err;
}

var Purchase_Orders = datain.Purchase_Order;
nlapiLogExecution('DEBUG','createRecord', 'obtuvo el objeto: '+ Purchase_Orders);

for (var Purchase_Orderobject in Purchase_Orders){

    var Purchase_Order = Purchase_Orders[Purchase_Orderobject];
    var transdate = Purchase_Order.Transdate;
    var Form = Purchase_Order.Form;
    var Vendor = Purchase_Order.Vendor;
    var Currency = Purchase_Order.Currency;
    var Item = Purchase_Order.Item;
    nlapiLogExecution('DEBUG','campos','transdate: '+ transdate+'/Form: '+Form + ' /Vendor: ' + Vendor + ' /Currency: ' + Currency
                                                            + ' /Item: ' + Item);
    var Purchase_Order = nlapiCreateRecord('purchaseorder');
    var nlobjAssistant =   nlapiCreateAssistant ( 'asistente' , false ) ;

            var Purchase_Orderid =  1;//nlapiSubmitRecord( Purchase_Order , true, true);
            if(Purchase_Order){

                nlapiLogExecution('DEBUG', 'Purchase_Order ' + Purchase_Orderid + ' successfully created', '');
                nlapiLogExecution('DEBUG', 'createRecord', 'creo el record');

            }

    Purchase_Order.setFieldValue('transdate', transdate);
    Purchase_Order.setFieldValue('inpt_customform1', Form);
    Purchase_Order.setFieldValue('vendor', Vendor);
    Purchase_Order.setFieldValue('inpt_currency7', Currency);
    Purchase_Order.setFieldValue('inpt_item', Item);
    Purchase_Order.setFieldText('quantity_formattedValue', '1');
    Purchase_Order.setFieldText('rate_formattedValue', '1');
    Purchase_Order.setFieldText('amount_formattedValue', '1');
    Purchase_Order.setFieldText('inpt_taxcode', 'VAT_MX:UNDEF_MX');
    Purchase_Order.setFieldText('grossamt_formattedValue', '1');
    Purchase_Order.setFieldText('tax1amt_formattedValue', '0');
    Purchase_Order.setFieldText('expectedreceiptdate', '24/6/2015');

            //var Purchase_Orderid =  1;//nlapiSubmitRecord( Purchase_Order , true, true);                   
            var submitRecord = nlapiSubmitRecord(Purchase_Order);//,true);
            nlapiLogExecution('DEBUG', 'submirRecord ' + submitRecord);

}

var mesg = new Object();
mesg.status = "OK";
mesg.message= nlobjAssistant.getAllFields();
return mesg;

}

And the function code in Java is:

WebClient client = new WebClient(BrowserVersion.FIREFOX_31);
        client.getOptions().setJavaScriptEnabled(false);
        client.getOptions().setThrowExceptionOnScriptError(false);

        WebRequest requestSettings = new WebRequest(new URL(url),HttpMethod.POST);

        requestSettings.setAdditionalHeader("Host", "rest.na1.netsuite.com");
        requestSettings.setAdditionalHeader("User-Agent", "SuiteScript-Call");
        requestSettings.setAdditionalHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        requestSettings.setAdditionalHeader("Accept-Language", " es-cl,es;q=0.8,en-us;q=0.5,en;q=0.3");
        requestSettings.setAdditionalHeader("Accept-Encoding", "gzip, deflate");
        requestSettings.setAdditionalHeader("Content-Type", "application/json");
        requestSettings.setAdditionalHeader("Pragma", "no-cache");
        requestSettings.setAdditionalHeader("Cache-Control", "no-cache");
        requestSettings.setAdditionalHeader("Referer", "http://localhost:8084");
        requestSettings.setAdditionalHeader("Cookie", "");
        requestSettings.setAdditionalHeader("Connection", "keep-alive");

        requestSettings.setAdditionalHeader("Authorization", "NLAuth nlauth_account=" + account + ", nlauth_email=" + mail + ", nlauth_signature=" + pass + ", nlauth_role=" + role + "");

        Gson gson = new Gson();

        //objeto llenado estaticamente de forma momentanea, se debe leer desde archivo externo                    
        Purchase_Order purchaseOrder = new Purchase_Order("25/06/2015","formTest","vendorTest","CurrencyTest","itemTest");

        String cuerpo = gson.toJson(purchaseOrder);

        System.out.println(cuerpo);

// Set the request parameters requestSettings.setRequestBody(cuerpo);

        Page page = client.getPage(requestSettings);
        WebResponse response = page.getWebResponse();

        String json = response.getContentAsString();
        System.out.println(json);

With this javascript function you should create me a record Purchase Order but I can not find the error and the solution if someone could please help me I would appreciate it a lot. PS: if you have to create a customized form could tell me how? thanks!


Solution

  • Here are some points :

    As your error message says Please enter value (s) for: Vendor, you're missing the value for vendor field, which is mandatory. In your piece of code you're passing wrong internalid value for vendor. You should use entity instead of vendor

     Purchase_Order.setFieldValue('entity', Vendor); // where vendor is the internal id of the vendor record
    

    For setting custom form you can use

    Purchase_Order.setFieldValue('customform', Form); // where Form is the id of the custom form
    

    I also noticed that you're setting some values in purchase order which I suspect are to be a kind of custom one. If that is the case, then your custom field internal id should be prefixed with custbody. For all the standard fields internal id you can refer to the Suite script Records Browser.