An Introduction to JavaScript in Sage CRM (Part 3)

4 minute read time.

This is the third article in a series provided for non-technical System Administrators who wish to understand more about using JavaScript to control workflow and to implement simple customizations using the language.

In this article, I will start looking at the syntax of the language and the different objects available.

JavaScript like any language has a basic syntax that can be learnt. The language allows us to create variables, to compare values and to perform calculations and actions on objects.

BUT — The objects that are available to us depend on where the code is run.

  • In an ASP Page
  • In the Browser
  • 'Inside' the Application server as Internal Script

This means that

  • Objects that exist in the browser will not exist in ASP pages
  • Objects that exist in ASP page will not exist in Internal script

Consider this code:

This shows that JavaScript is basically similar in syntax to C and C++.

It is important to be aware that JavaScript is Case Sensitive

All keywords are lowercase

It is "var", not "Var", not "VAR".
It is "if", not "If", not "IF".

All other variables, function names etc must be typed with consistent capitalization

consider

var id = CRM.GetContextInfo("company","comp_companyid");
var myBlock = CRM.GetBlock("opportunitylist");
Response.Write(myBlock.Execute("oppo_primarycompanyid="+id);

If we want to pass the context information correctly to Execute method then the variable 'id' has to be correctly referenced.

In Sage CRM parameters may not be case sensitive if passed directly to the database. E.g. Block names.

In the example above it doesn't matter if we pass 'OpportunityList' or 'opportunitylist' as the parameter as the Database is not case sensitive.

Whitespace

JavaScript ignores white space. You can use tabs, spaces and new lines to format your code.

Semicolons should be used to terminate statements but if each statement is on a new line then this may be omitted.

Comments
//This is a commented out line

/*
These lines
Are commented out
*/

Have a look at this example.

In this example, there are Variables that are defined and an object and method are used.

I declared two variables. I gave them identifiers. Identifiers are names in JavaScript. Identifiers are used to name variables (and keywords, and functions, and labels).

The rules for legal names are much the same in most programming languages.

  1. In JavaScript, the first character must be a letter, an underscore (_), or a dollar sign ($).
  2. Subsequent characters may be letters, digits, underscores, or dollar signs.
  3. Numbers are not allowed as the first character.

This way JavaScript can easily distinguish identifiers from numbers.

I can use this code in the Custom Content box of the CompanyBoxLong screen.

and this would cause the message to be written on the screen.

Any variables in JavaScript are "untyped". This means that they can be of any datatype and then can change type during their existence.

Consider

The 'var' keyword is used when variables are declared

var intCount;
var intTotal = 1 + 2 + 3;
var a, b, c, e

But this may be omitted in top-level code but it is also needed for local variables declared in Functions.

Have a look at the image below:

JavaScript has two scopes for variables. These are global and local.
If a variable is declared outside of function definition then it is considered a global variable. This means that the value is accessible and modifiable throughout the program.

If a variable is declared inside of a function then that variable is local. This means that it is created and destroyed every time the function is executed; it cannot be accessed by anything outside the function.

JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.

  • Primitive data type
  • Non-primitive (reference) data type

JavaScript is a dynamic type language, means you don't need to specify the type of the variable because it is dynamically used by JavaScript engine. And variables can hold any type of values such as numbers, strings etc.

This is our Hello World example again but this time written in an ASP page.

The JavaScript is very similar but also different from the custom content example because the code is executed in different places.

You can see that in our first example the way in which the value was written out into the interface was to use "document.write" but within the ASP page, we use "Response.Write".

ASP pages & Browser scripts both share the same syntax but they use different objects provide by the environment.

In the next article, I will start to explain the different types of JavaScript data types and how we use them in Sage CRM scripts.

An Introduction to JavaScript in Sage CRM

The links to the other articles in the series are listed below

  1. An Introduction to JavaScript in Sage CRM.
  2. The relationship that JavaScript has with other languages and where scripting can be used in practice.
  3. The syntax of the language and the different objects available.
  4. The different types of JavaScript data types and how we use them in Sage CRM scripts.
  5. The objects that are available within the Browser.
  6. Binding a script to an event.
  7. Statements in JavaScript.
  8. Conditional behaviour and compound statements.
  9. Another look at Strings, regular expressions and other objects.
  10. Arrays, Functions, and techniques for processing objects.