Beginner's guide to ejScript. Part One: How To's

By Simen Mostuen Iversen, Updated on 17 Nov 2014
push_pin
star

There is a lot of possibilities for adaptations in SuperOffice Customer Service. While upgrading to 7.5 enables you to easier make automations through event handlers and macros, it is still a good idea to learn the basics of ejScript, so that you have the ability to understand the functionality of scripts and functions in screens.

This guide will only show you the basics of ejScript and some examples, if your skill is above that of a novice, this probably isn't for you.

I'll try not to include too much mumbo jumbo technical talk here, and keep it as simple as possible.

Let's start with the basic classes that are used the most:

// Class:
String

// Usage:
String name = "Some text here";

The String class is used for text, it can be as long as you want it to be and can be empty. "name" is the variable name for this class, it can be whatever you want but it doesn't allow space. Also, it will only allow String types; which means that it has to be between two "'s.

// This works:
String firstname = "Ola";
String first_name = "Ola";

// This does not work
String first name = "Ola";
String firstname = Ola;
String firstname = 10;

If you get a number of the type Integer, you are allowed to convert to number to a String type by adding a .toString() at the end. 

// Convert an Integer to String
Integer age = 30;
String sAge = age.toString();

 

// Class: 
Integer

// Usage:
Integer name = 0;

The Integer class is used for numbers, and should only be used for mathematical use (Note! Whenever an object is saved, it returns the id as an integer)

Integers won't allow String types in it though, but can be converted aswell, though here you should check if the conversion was successfull to avoid receiving possible errors:

// This will work
Integer age = 30;

// This will not work:
Integer age = "30";

// Converting a String to an Integer:
String sAge = "30";
Integer age = sAge.toInteger();

 

Now that you've mastered the most used basic classes, we can use them in our different objects. The most common used is the Ticket, Person and Company objects.

// Class:
Ticket

// Usage:
Ticket t;
t.setValue("title", "The title of this request");
t.setValue("category", "1"); // Category is mandatory
Integer ticketId = t.save();

Now we suddenly got a little more advanced. Ticket is the object, and t is the name of the created instance. Every time you write <object> <name> you create a new instance of that object (ie: Ticket t;). You can load as many objects as you want, but you should have in mind that loading many objects may slow your application, so you should be careful with what you do in your script and try to avoid doing unncessary loads.

Note! Variable names must be unique, although there are some exceptions - I won't get into them right now.

I will now create an example that creates a company, then a person that are connected to the company, and 10 tickets that are created through a loop. This doesn't necessarily have any practical use, but is an example to show how you can use it to create entities in Customer Service.

// Creates the company
Company comp;
comp.setValue("name", "SuperCompany AS");
Integer compId = comp.save();

// Creates the customer and binds it to the company
Customer cust;
cust.setValue("firstname", "Ola");
cust.setValue("lastname", "Nordmann");
cust.setValue("company", compId.toString());
Integer custId = cust.save();
// Loops through 1 to 10 and creates a ticket.
for(Integer i = 1; i <= 10; i++)
{
  Ticket t;
  t.setValue("title", "This is ticket number: " + i.toString());
  t.setValue("custId", custId.toString());
  t.setValue("category", "1");
  t.save();
}
// At the end, prints out our created customer name and company name
print("Created the customer " + cust.getValue("firstname") + " " + cust.getValue("lastname") + ", in the company " + comp.getValue("name"));

 

This should output:

Created the customer Ola Nordmann, in the company SuperCompany AS

Also, if you check the "Last requests", you'll see that you now have 10 new tickets

 

This is the start of the beginner's guide to ejScript. If you master this you will be able to understand the rest pretty quickly. The logic is the same, and you can use our API to see what each function returns.

 

It should be noted that whenever you do an adaptation, you should always test out small parts of your code before saving a larger piece. There have been done some major blunders (from me included) by scripting something without thinking of the consequences, so you should limit the persons having access to this to as few as possible.

Hi Simen!


Excellent work! This gives a solid base on getting to know eJscript. 


 Explained a lot to me! :) Thankx!


 /Niels

Niels van Broekhoven 26 Nov 2014

Good read! More of this Simen :-) I know the community wants it!

Øivind Urdal 19 Nov 2014