Archives for the ‘Javascript’ Category

8 Oct 2013
6

Connect to a Notes Database on a remote Domino Server via SSJS

The other day I found myself needing to access a Notes Database on a Remote Domino Server via SSJS, but for some reason every one of my attempts failed miserably. No error messages written to the Console, no warnings, nothing.

Let’s quickly create a scenario

  • You have 2 Domino Servers: Server1/ACME and Server2/ACME.
  • On both Servers you have a Notes Database called “Test.nsf”.
  • On Server1/ACME you have a Notes Database with an XPage that calls the following SSJS:
1
2
3
4
5
6
7
var db:NotesDatabase = session.getDatabase("Server1/ACME", "Test.nsf");
 
if(db.isOpen()){
   print(db.getTitle());
}else{
   print('Cannot Open Notes Database');
}
  • Now, in the above example, you are trying to access a Notes Database on the same Server. The above code will successfully connect to the Notes Database and print the Database Title to the Server Console.
  • Next up, you have the following code that runs on the same Server:
1
2
3
4
5
6
7
var db:NotesDatabase = session.getDatabase("Server2/ACME", "Test.nsf");
 
if(db.isOpen()){
   print(db.getTitle());
}else{
   print('Cannot Open Notes Database');
}
  • In this example, you are trying to access “Test.nsf” on Server2/ACME, from a Script that’s running on Server1/ACME. This code will fail and will not provide you with any error message. The NotesDatabase Object will simply remain null.

The Reason

In all my years of developing Notes Applications, I never came across having to connect to a Database on a different Server. The reason the connection fails, is because Server1/ACME is not listed as a Trust Server in Server2/ACME’s Server Document in the Domino Directory.

The Solution

To resolve this issue is simple. The difficult part was finding the problem. If you have access to the Domino Directory on Server2/ACME, do the following:

  • Open the Domino Directory (Server Address Book) on Server2/ACME.
  • Under “Configuration”, click “Servers”, then click on “All Server Documents”.
  • Double click on the Server Document you want your code to connect to. (Server2/ACME in this example)
  • Under the Security Tab, in the bottom left section, there is a field called “Trusted Servers”.
  • In my scenario, this field is empty. Following this example, I would add “Server1/ACME” to this field.

Blog1

  • Save and Close.
  • Restart Server2/ACME.

Once you’ve restarted, your code will successfully connect to “Test.nsf” on Server2/ACME.

Till next time….cheers :)
John
25 Jun 2013
2

Javascript Tip: Test to see if your variable’s value is valid

Invalid values in JavaScript come in all shapes and sizes. Sometimes you just want to know if a variable has a valid value so that you can execute the necessary code.

I had a pretty hard time with this, but I found that testing a variable for a ‘Truthy’ value works very well for me. When I say test for a ‘Truthy’ value, I mean a legit value that will return true. See below example:

1
2
3
4
5
6
7
var myVariable = 'Test';
 
if(myVariable){
   alert("True");
}else{
   alert("False);
}

The example above would return true because myVariable contains a valid value. If myVariable returned one of the following, the above example would return False:

- Undefined
- Null
- <null>
- NaN
- +0
- -0
- Empty String where length is 0.

Hope this helps :)

20 Mar 2013
0

Tip: Understand how to compare values in JavaScript

Hi everyone. In this post I want to point out a not so common way to compare variables and their values in JavaScript.

For many out there, the common way to compare 2 values is to use the == or != operators. If we look at the example below however, you’ll notice an interesting result that is returned:

Example:

var boolFalse = false;

var boolTrue = true;

var myString = “”;

if(boolFalse == 0) //This returns True

if(boolTrue == 1) //This returns True

if(myString == 0) //This returns True

The reason why these values are returning true is because as a default, when using == or !=, JavaScript only compares the values and not their variable types.

If you wanted to perform a strict comparison between 2 variables using their Value as well as Type, then you will need to use === or !==.

Example:

if(boolFalse === 0) //This returns False

if(boolTrue === 1) //This returns False

if(boolTrue !== 1) //This returns True

if(myString === 0) //This returns False

 

I now use this as a Standard when coding in JavaScript. I hope this makes sense.

Happy coding :)

John.

17 Jan 2013
2

XPages Tip: Pass NotesDateTime to the Javascript Date Object

Hi all.

I spent this morning creating a massive SSJS Validation Rule that had to do with Date and Time values that were provided by the User online. I think Date Objects will forever remain my Everest, because I just can’t seem to get it right without wasting hours on it.

In SSJS, the problem with the NotesDateTime Object is that properties like “getDateOnly”, “getTimeOnly” and “getLocalTime” return the values as strings. If you want to compare values, you’re in for a tough time.

Now the Javascript Date Object offers you way more in terms of comparing values. Click here to check an awesome function you can use in your code.

I had a scenario where I was using NotesDateTime Objects to manipulate Date/Time Values, but I wanted to pass these Date/Time Values to the JavaScript Date Object to perform compares, etc. My first attempt failed when I tried to use the “getLocalTime” property of the NotesDateTimeObject:

Note: This example is not best practice, but allows me to show you a function available in SSJS

var date1:NotesDateTime = session.CreateDateTime(“2013/01/17 06:00:00″);

var date2:Date = new Date(date1.getLocalTime());

The above-mentioned will fail because date1.getLocalTime() is an illegal Date value according to the JavaScript Date Object. Having said that, there is an awesome NotesDateTime Method called .toJavaData() that converts the value to a JavaScript Date Object:

var date1:NotesDateTime = session.CreateDateTime(“2013/01/17 06:00:00″);

var date2:Date = new Date(date1.toJavaDate());

From here use you can use the Date Object for comparing Date and Time Values.

I trust this helps :)

John.

6 Nov 2012
6

Enabling Intelli-sense in Firebug for Dojo, jQuery and Javascript

Hi everyone. There are a couple of awesome posts that were published by Johan Meyer, Ukuvuma’s senior Microsoft Developer. I thought it best to re-blog some of his posts that I feel would add a lot of value to those who follow this particular blog.

Enjoy

John.

 

From Johan Meyer at Ukuvuma Solutions

I found a nifty plugin for Firefox that enables intelli-sense in the Firebug Console for certain Javascript Frameworks.

To enable intelli-sense for Dojo, jQuery and Javascript, install the following plugin for Firefox. This will enable the intelli-sense in Firebug’s Console Window.

Get the Firebug auto-completer Plugin

12