Friday, September 14, 2018

SuiteScript > Auto-generate Record or Transaction ID using script

Sample Scenario: The customer would like to create a script that will auto-generate the Project ID based on the first 4 alphanumeric letters of the customer name appended with the recent Project Number plus 1.
 

1. Make sure that the Allow Override checkbox is checked by going to Go to Setup > Company > Auto-Generated Numbers.
2. Create a user event script that will be deployed on Before Submit Function.
3. Sample code below:
 
function generateProjectID(){
    var columns = new Array();
    nlapiSetFieldValue('autoname', 'F'); //Set this to false to allow overriding the Project ID
    columns[0] = new nlobjSearchColumn('entityid');
    columns[0].setSort(true);
    var searchRecords = nlapiSearchRecord('job',32,null,columns); //Search the project record to get the recent Project ID
    var projectID;
    if (searchRecords != null){
        var searchresult = searchRecords[0];
        projectID = searchresult.getValue('entityid'); //Get the recent project ID
    }
    var newProjectID = projectID.substring(4,projectID.length); //Gets the Project Numbers (truncates the first four characters of the recent Project ID)
    newProjectID = parseInt(newProjectID) + 1; //This will increment the Project ID by 1
    var custname = nlapiGetFieldText('parent'); //Get the text of the customer name dropdown field
    var tempid = custname.substring(0,4); //Gets the first four characters of the customer name
    tempid = tempid + newProjectID; //This will concatenate the value of the customer name and the Project Number
    nlapiSetFieldValue('entityid', tempid); //This will set the new Project ID to the Project ID field.
}  

1 comment: