Saturday, May 21, 2011

Helpful JavaScript functions you may want to include in your Library Script

We've MOVED!!!! www.codeboxllc.com/ksc

We all need help sometimes. For me, I am not ashamed to look for help on the NetSuite Forum. Through out my experience with NetSuite deployment and customizations, I've found following JavaScript functions to be extremely helpful and I hope it provides you with some help as well.

Contains function: I found this script from CSS-Tricks website.
It is most useful when you have array of objects, strings, numbers and you wish to check for existence of a value you pass it.
Array.prototype.contains = function(arg) {
  for (i in this) {
    if (this[i]==arg) return true;
  }
  return false;
};
Here is how you use it.
var myArray = new Array("123","555","888","999"); //internal id of items
var itm = nlapiGetCurrentLineItemValue('item','item'); //get item id of current line item
if (myArray.contains(itm)) {
  alert('This Item exists in the Array!');
}
It makes your code lot simpler and easy to read.

Empty filter value checker: While writing custom search, I came across some odd errors here and there. Turns out, when search filter value is null or empty, it throws an error. After reading documentation, it turns out that you have to use @NONE@ in place of empty or null value.
function emptyFilterCheck(_val) {
  if (_val) {
    return _val;
  } else {
    return '@NONE@';
  }
}
 Send Email Function: I'm sure most of you have this function separated out. This is my version of the send email. It takes 6 parameters. From ID, To ID, Subject, Message, Attach to Record ID, Record Type.
Script will attach email to a record if the value is passed in.
function sendNotificationEmails(fromId, toId, strSbj, strMsg, attachToRecId, recType) {
  //check for sandbox
  if (env == 'SANDBOX') {
    strSbj='[SANDBOX] - '+strSbj;
  }


  try {
    //attach this email to record if defined
    if (attachToRecId != null) {
      var rec = new Object();
      if (recType==null) {
        rec['entity']=attachToRecId;
      } else if (recType=='transaction' || recType=='activity') {
        rec[recType]=attachToRecId;
      } else {
        //This is custom record
        nlapiLogExecution('DEBUG','msg','This is custom record:');
        rec['recordtype']=recType;
        rec['record']=attachToRecId;
      }
      nlapiSendEmail(fromId, toId, strSbj, strMsg, null, null, rec);
    } else {
      nlapiSendEmail(fromId, toId, strSbj, strMsg);
    }
    return true;
  } catch(e) {
    if (e instanceof nlobjError) {
      //Do your error handling here for e.getCode()and e.getDetails()
    } else {
      //Do your error handling for JavaScript issue: e.toString()
    }
  }
}
There are few others I'm trying to put it. Just to give you an idea, here are list of potential helper functions I am creating:

  • Search function which takes array of filter fields, values, and records' internal ID.
    I found myself writing these codes over and over again.
  • Specialized Pardot API library sets.
    If you use Pardot along with NetSuite, it's a good idea to create common function calls to Pardot. For example, LookupProspect(email), AddOrUpdProspect(email, JSONfldValObj), UpdProspectList(email, newList), UpdProspectCamp(email, newCampaign), GetActivities(email) just to name a few.
    I plan to write some sample Pardot API calls from NetSuite that I've written later.
  • Sublist related functions. 
What are some of your helper functions you are using?

No comments:

Post a Comment