Haithem D_
Giga Expert

This is a Article is about how to Sync Google Calendar event with servicenow calendar (user_calendar_event)

First use the Google Developer Cosole to set up an OAuth 2.0 provider, you can find more information here.

Then you need to Create an OAuth provider and profile

Procedure:

  1. Navigate to System OAuthApplication Registry.
  2. Click New.
  3. Select Connect to a third party OAuth Provider.
  4. Enter a Namefor the OAuth provider. For this example, use Google.
  5. Enter the Client IDand Client Secret that you obtained from Google.
  6. Set the Default Grant typeto Authorization Code.
  7. In the Authorization URLfield, enter https://accounts.google.com/o/oauth2/auth.
  8. In the Token URLfield, enter https://www.googleapis.com/oauth2/v3/token.
  9. In the Redirect URLfield, enter https://<instance>.service-now.com/oauth_redirect.do.

This URL must match the redirect URL provided to Google.

  1. In the Token Revocation URLfield, enter https://accounts.google.com/o/oauth2/revoke.
  2. Right-click the form header and select Save.

A new OAuth Entity Profile record is created.

12.In the OAuth Entity Scopes embedded list, add a new row with the Name and OAuth scope values set to https://www.googleapis.com/auth/calendar.addons.execute

https://www.googleapis.com/auth/calendar.events
https://www.googleapis.com/auth/calendar
https://www.googleapis.com/auth/calendar.settings.readonly
https://www.googleapis.com/auth/calendar.events.readonly
https://www.googleapis.com/auth/calendar.readonly

  1. Right-click the form header and select Save.
  2. In the OAuth Entity Profilesembedded list, select the automatically-created profile.
  3. In the OAuth Entity Profile Scopesembedded list, add a new row and select the Google contacts API read-only scope.
  4. Click Update.

    Third you need to Create a REST message

    Procedure

    1. Navigate to System Web ServicesREST Message.
    2. Click New.
    3. Enter a descriptive Name.
    1. In the Endpoint field, enter https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events.

    By using default instead of a specific username, the Google API uses the OAuth credentials to determine which account to get information from.

    1. In the Authenticationtab, set the Authentication type to wOAuth 2.0.
    2. In the OAuth profilefield, select the Google contacts OAuth profile.
    3. Right-click the form header and select Save.
    4. Click the Get OAuth Tokenrelated link to request an authorization token from Google using the configured client ID and secret.
    5. In the Request for Permission window that appears, click Acceptto grant access to your Google contacts.

    The token aquired is not directly accessible in your instance.

    1. In the HTTP Methodsrelated list, select the GET
    2. Leave the HTTP method Authentication typeas -- None -- to use the OAuth profile from the parent REST message record.
    3. On the HTTP Requesttab, add a new row to the HTTP Headers related list with a Name of GData - Versionand a Value of 0.
    4. Right-click the form header and select Save.
    5. Click the Test related link.

Finally you create a Fix Script for the Sync ( create, delete, update) events from google.

The test result should display an HTTP Status of 200, and the result of the contacts API call.

 

Create a Script to Sync Google event with servicenow (user calendar event)

  1. Navigate to System Definition -> fix Script
  2. Click New.

Copy this code to Get event from Google Calendar, try to create some events in your calendar then run the script you will get those event in: my calendar in servicenow.


gs.log("**** Scritp to Add Update google calendar event with service now ****");

gs.log("Parsing Google Calendar Events");

 

 

var arr = []; // table to save all events IDs

InsertUpdate();

 

function InsertUpdate() {    

try {

 var r = new sn_ws.RESTMessageV2('Google Calander Rest', 'Get Events from Google Calendar');

 r.setStringParameterNoEscape('calendarId', 'Put your email @ here');

 var response = r.execute();

 var responseBody = response.getBody();

 var httpStatus = response.getStatusCode();

   

// parse the response data     

var parser = new JSONParser();  

var parsedData = parser.parse(responseBody);

for(var i=69;i<parsedData.items.length;i++)

{

// declare variable to be used

            // summary is the event Name in Snow

var summary = parsedData.items[i].summary;

var organizer = parsedData.items[i].organizer.displayName;

var email = parsedData.items[i].organizer.email;

var description = parsedData.items[i].description;

var start = parsedData.items[i].start.dateTime;

var end = parsedData.items[i].end.dateTime ;

            // this variable : googleid will be used as a unique id for inserting updating or deleting events

var googleid = parsedData.items[i].id ;

arr.push(googleid.toString());

            // use gs.log to track the script activities

gs.log("summary :["+i+"]"+summary);

gs.log("organizer :["+i+"]"+organizer);

gs.log("description :["+i+"]"+description);     

gs.log("start :["+i+"]"+start);

gs.log("end :["+i+"]"+end);     

var sec=Date.parse(start);

            // convert google date time to servicenow format

 

var dateStr = start.substring(0,start.indexOf('+'));

var dateEnd = end.substring(0,end.indexOf('+'));  

var       dateStr1=dateStr.toString().concat("","Z");dateStr1=dateStr1.replace(/-/g,"");dateStr1=dateStr1.replace(/:/g,"");

var       dateEnd1=dateEnd.toString().concat("","Z");dateEnd1=dateEnd1.replace(/-/g,"");dateEnd1=dateEnd1.replace(/:/g,"");

            // convert timedate format to GlideDateTime

var calendar_start=new GlideDateTime();

            calendar_start.setValue(dateStr2);

var calendar_end=new GlideDateTime();

            calendar_end.setValue(dateEnd2);

 

            // convert timedate format to GlideScheduleDateTime

var calendar_Schedule_start= new GlideScheduleDateTime(calendar_start.getDisplayValue());

var calendar_Schedule_end= new GlideScheduleDateTime(calendar_end.getDisplayValue());

           

 

gs.log("calendar_Schedule_start :"+calendar_Schedule_start.getDisplayValue());

gs.log("calendar_Schedule_end :"+calendar_Schedule_end);

gs.log("Parsing list of attendees from Google Calendar for each event");

            if (parsedData.items[i].attendees.length>0)

            {

            for(var j=0;j<parsedData.items[i].attendees.length;j++)

                        {

                                    

                                    var attendee = parsedData.items[i].attendees[j].email;

                                    gs.log("attendees :["+j+"]"+attendee);

                        }

            }

           

 

 

            // insert into service now Data base

            // before inserting we should verify if the record already exist in the servicenow

            // database

var calendarbase = new GlideRecord('user_calendar_event');        

calendarbase.addQuery('u_number',googleid);

calendarbase.query();

var count = calendarbase.getRowCount();

            if (count > 0)

            {

                        gs.log("Uppdating an existing Event fron Google Calendar");

                        gs.log("Uppdating Event Name: "+summary+"Google event ID : "+calendarbase.u_number);

                        gs.log("Uppdating Event Name: "+summary);

                        gs.log("Uppdating start Date: "+calendar_Schedule_start);

                        gs.log("Uppdating End Date : "+calendar_Schedule_end);

                        while (calendarbase.next())

                        {

        calendarbase.name=summary;

                        calendarbase.start_date_time=calendar_Schedule_start;

                        calendarbase.end_date_time=calendar_Schedule_end;

                        calendarbase.update();

        }

            }

            else

                        {

                        gs.log("Creating a New Event fron Google Calendar");

           

                                               calendarbase.initialize();       

                                               calendarbase.name=summary;

 

                                               // get the user name based on the user email from the google calendar event

                                               var target = new GlideRecord('sys_user');

                                               target.addQuery('email',email);

                                               target.query(); // Issue the query to the database to get relevant records

                                               while (target.next())

                                               {

                                      // add code here to process the incident record

                                               gs.log("target.name"+target.name);

                                               calendarbase.setDisplayValue('user',target.name);

                                              

                                               //gs.log("Hello Word"+target.getValue('name'));

                                               }          

 

 

                                    //calendarbase.sys_created_by=organizer;  

                                    //calendarbase.user='Dadi Haithem';//organizer;

                                    //calendarbase.setValue('start_date_time',+calendar_Schedule_start.getValue());

                                              

                                               

                                               calendarbase.start_date_time=calendar_Schedule_start;

                                               calendarbase.end_date_time=calendar_Schedule_end;

                                               calendarbase.type='Meeting';

                                               calendarbase.u_number=googleid;

                                               calendarbase.insert();

                                                //gs.log(calendarbase.getValue('end_date_time'));gs.log(calendarbase.getValue('user'));

                        }

           

            //********

            /*calendarbase.addQuery('u_number','!=',googleid);

            calendarbase.query();

            while(calendarbase.next())

                        {

                                    gs.log("number"+calendarbase.u_number);

                                    if (calendarbase.u_number!==googleid)

                                               {

                                                           gs.log("exist not delet");

                                               }

                                    else

                                               {

                                                           gs.log("not exist Delet");

                                                           calendarbase.deleteRecord();

                                               }

                        }*/

            }

 

 

 

             

             

             

}

catch(ex) {

 var message = ex.message;

}

            gs.log("End of Function Update");

} // end Function

 

//Delete Event

 

//calendarbase.addQuery('u_number',googleid);

//calendarbase.query();

gs.log("arr.length"+arr.length);

//for(k=0;k<arr.length;k++)

//{

            var calendarbase1 = new GlideRecord('user_calendar_event');

            //gs.log("arr :["+k+"]"+arr[k]);

            //calendarbase1.addQuery('u_number','!=',arr[k]);

            calendarbase1.query();

            //var count1 = calendarbase1.getRowCount();

            //gs.log("count :["+k+"]"+count1);

            while(calendarbase1.next())

            {

                        gs.log("u_number I :"+calendarbase1.u_number);//gs.log("arr :"+arr[0]);gs.log("arr :"+arr[1]);

                        var noo =arr.indexOf(calendarbase1.getValue('u_number'));

                        gs.log("noo I :"+noo);

                                               if (noo!=-1)

                                               {

                                               gs.log("Not Deleting Record .... "+calendarbase1.getValue('u_number'));

           

                                               }

                        else

                                    {

                                               gs.log("Deleting Record .... "+calendarbase1.getValue('u_number'));

            calendarbase1.deleteRecord();

                                    }

           

                                    }

           

//}

Comments
sherardpulmano
ServiceNow Employee
ServiceNow Employee

This is great! Thanks

 

Is there a way to do the reverse? synchronize ServiceNow schedule/calendar events to another calendar such as google calendar or your mobile phone calendar?

Haithem D_
Giga Expert

Hello,

Yes take a look to this article: 

How to send events from servicenow to google calendar

Haithem D_
Giga Expert

Hello,

Did you find the answer in this article? could you please update?

kjain
Giga Expert

Hi Haithem, 

 

Can you provide steps to integrate Servicenow change request, release with google calendar.

 

 

Version history
Last update:
‎03-14-2019 07:34 AM
Updated by: