Thursday, 7 February 2013

Login and Logout a session in suitetalk



In order to login and create a session , you need to do something like this

//create an object of Netsuite service   
NetSuiteService session = new NetSuiteService();
//add a cookie container to it
session.CookieContainer = new System.Net.CookieContainer();
//adding login credentials
Passport passport = new Passport();
passport.account = your account id
passport.email = your email id
RecordRef role = new RecordRef();                   
role.internalId = your role(write 3 if administrator account)
passport.role = role;                   
passport.password = your password
//logging in
Status status = session.login(passport).status;
//returning status
return status.isSuccess;


In order to logout of the session, run the following code.

Status status = session.logout().status;
return status.isSuccess;

Creating Assistant in netsuite using suitescript


Here is a sample code of a web assistant made in a suitelet.

function DemoAssistant(request, response) {
    try {
        //create an object (nlobjassistant) and store that in ‘assistant’ variable
        var assistant = nlapiCreateAssistant("Employee Registration Form");

        //set title of the assistant
        assistant.setTitle('Employee Details');

        // indicate that all steps must be completed sequentially
        assistant.setOrdered(true);

        //adding steps
        assistant.addStep('PersonalDetails', 'Set Personal Details').setHelpText("Enter your personal details");
        assistant.addStep('ContactDetails', 'Enter Contacts Details').setHelpText("Enter your contact details");
        assistant.addStep('EmployementDetails', 'Enter Employement Details').setHelpText("Enter your employement details");
        assistant.addStep('Finished', 'Registration Completed');

        //setting actions
        if (request.getMethod() == 'GET') {

            if (!assistant.isFinished()) {

                if (assistant.getCurrentStep() == null) {

                    assistant.setCurrentStep(assistant.getStep("PersonalDetails"));

                    //add splash screen
                    assistant.setSplash("Welcome!", "Enter your details");

                }

                var step = assistant.getCurrentStep();

                //display part
                if (step.getName() == 'PersonalDetails') {

                    assistant.addField("custpage_firstname", "text", "First Name");
                    assistant.addField("custpage_lastname", "text", "Last Name");

                }

                else if (step.getName() == 'ContactDetails') {

                    assistant.addField("custpage_phone", "phone", "Phone");
                    assistant.addField("custpage_email", "email", "E-mail");
                    assistant.addField("custpage_address", "textarea", "Address");

                }

                else if (step.getName() == 'EmployementDetails') {

                    assistant.addField("custpage_empid", "text", "Employee ID");
                    assistant.addField("custpage_companyname", "text", "Company Name");

                }

                else if (step.getName() == 'Finished') {

                    var personalDetails = assistant.getStep('PersonalDetails');
                    var contactDetails = assistant.getStep('ContactDetails');
                    var employementDetails = assistant.getStep('EmployementDetails');                   

                    //fetching values from previous steps
                    var firstName = personalDetails.getFieldValue('custpage_firstname');
                    var lastName = personalDetails.getFieldValue('custpage_lastname');

                    var phone = contactDetails.getFieldValue('custpage_phone');
                    var email = contactDetails.getFieldValue('custpage_email');
                    var address = contactDetails.getFieldValue('custpage_address');

                    var empId = employementDetails.getFieldValue('custpage_empid');
                    var companyName = employementDetails.getFieldValue('custpage_companyname');

                    //displaying
                    assistant.addField('custpage_displayfirstname', 'Label', "First Name : " + firstName);
                    assistant.addField('custpage_displaylastname', 'Label', "Last Name : " + lastName);
                    assistant.addField('custpage_displayphone', 'Label', "Phone : " + phone);
                    assistant.addField('custpage_displayemail', 'Label', "Email : " + email);
                    assistant.addField('custpage_displayaddress', 'Label', "Address : " + address);
                    assistant.addField('custpage_displayempid', 'Label', "Employee ID : " + empId);
                    assistant.addField('custpage_displaycompanyname', 'Label', "Company Name : " + companyName);

                    assistant.addField('custpage_displaymessage', 'Label', "Click Finish to submit");

                }

            }

            //writing to page
            response.writePage(assistant);

        }

        else {

            if (assistant.getLastAction() == "finish") {
                assistant.setFinished("You have completed");
                assistant.sendRedirect(response);
            }

            else {
                assistant.setCurrentStep(assistant.getNextStep());
                assistant.sendRedirect(response);
            }

        }

        //writing to page
        response.writePage(assistant);

    }
   
    catch (er) {

        nlapiLogExecution('DEBUG','Error',er.message);

    }

}

Hope that this helps you.

Friday, 4 January 2013

Calling a restlet from a suitelet


I guess you are trying to call a restlet from a suitelet and pass some json object to the restlet. Here we go

Suitelet part

//url of the restlet
var url = copy the url of the restlet here...

//creating a Json object
var jsonObj = new Object();
jsonObj.fruit1 = "apple";
jsonObj.fruit2 = "mango";
var myJson = JSON.stringify(jsonObj);

//credentials
var headers = new Array();
headers['Content-Type'] = 'application/json';
headers['Authorization'] = 'NLAuth nlauth_email=your netsuite email id, nlauth_signature=password, nlauth_account=account id, nlauth_role=role';

To know your account id, just click here. For role, just write there '3' (for adminstrator account) , for the time being.

//calling restlet
var restletResponse = nlapiRequestURL(url, myJson, headers, null, "POST");

//getting response
var responseText = restletResponse.getBody();

you can use this responseText to get the response from restlet.
Now as we are done with suitelet, how to read this data in the restlet. Here is the code.

Restlet
//getting data from datain
var fruit1=datain.fruit1;
var fruit2=datain.fruit2;

Hope this was helpful.
Any suggestion to improve the blog are most welcomed.

Dont Know your Account ID

Ahhh, so you dont know your account ID. No problem , just go through these few simple steps and you will know it.

setup >>> integration >>> web service preferences


fwwww, so simple.

CSV Import in Netsuite

Lets see how this CSV thing works in Netsuite.

This is a sample code to load a CSV file (already present in your file cabinet) and submit it.CSV works only in restlet, schedule script and bundle installation.


var csvMap = '6'; //saved CSV import id(read below to set this)
var csvFile = nlapiLoadFile("948"); //csv file id from file cabinet                
var csvImport = nlapiCreateCSVImport();//creating a new csv import
csvImport.setMapping(csvMap);//setting import map
csvImport.setPrimaryFile(csvFile.getValue());//file to be imported
csvImport.setOption("jobName", "job1");//setting job status' job name       
var csvSubmitId = nlapiSubmitCSVImport(csvImport);//submitting the CSV



You can use this code , but first you need to set the CSV map.To do this,see below

Setup >>> Import/Export >>> Import CSV Record

Import Assistant page opens up.

Step 1 :
select the record type from the import type list (customer, employee,etc.)
Click on select button , browse and upload your csv file for mapping.

Step 2: look carefully and select the options of your choice.

Step 3:

Step 4: The main step

Select a field from field mapping window and map it to a netsuite field(remember to edit the mapping)

Step 5: done


Wednesday, 2 January 2013

The Inception

Well , this is my first post and so i would like to tell what this blog is all about.

I am new to netsuite and i am enjoying scripting. While i am going through this learning phase, i thought to share whatever i learn as i never found a good website that tells all about netsuite.

I hope that you will find my blog helpful.