1

I have a visual force page for my organization where each member can schedule a task by clicking simply on a commandbutton. what i want is when he clicks on the button, i create a event on his google calendar with the corresponding date. i know this is possible in apex and i red the documentation on the google calendar API (here) but i did not understand any thing , can you please provide me with a sample code on how to do that in apex ?

3

I wrote a tutorial on this functionality long back. Here are the steps

  1. Create Google client id and client secret.
  2. Create a button on your task object and it will ask user to login and it will create event in calendar.

Here are the snippet for the same.

var clientId = 'YOUR_CLIENT_ID';
var apiKey = 'YOUR_API_KEY'; 

var scopes = 'https://www.googleapis.com/auth/calendar'; 

// Use a button to handle authentication the first time.
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth,1);
} 

function checkAuth() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
} 

function handleAuthResult(authResult) {
// var authorizeButton = document.getElementById('authorize-button');
if (authResult && !authResult.error) { 

makeApiCall(); 

} else {
handleAuthClick();
}
} 

function handleAuthClick() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
return false;
} 

// Load the API and make an API call. Display the results on the screen.
function makeApiCall() {
AddEvent();
}
function AddEvent()
{
var startdate='{!YEAR(DATEVALUE(Task.CreatedDate))}' + '-' + '{!MONTH(DATEVALUE(Task.CreatedDate))}' + '-' + '{!DAY(DATEVALUE(Task.CreatedDate))}';
var enddate='{!YEAR(Task.ActivityDate)}' + '-' + '{!MONTH(Task.ActivityDate)}' + '-' + '{!DAY(Task.ActivityDate)}'; 

var resource = {
"summary":"{!Task.Subject}",
"description": "{!Task.Description}",
"start": {
"date": startdate
},
"end": {
"date": enddate
}
};
gapi.client.load('calendar', 'v3', function() {
var request = gapi.client.calendar.events.insert({
'calendarId': 'primary',
'resource': resource
});
request.execute(function(resp) {
console.log(resp);
if (resp.id){
alert("Event was successfully added to the calendar!");
}
else{
alert("An error occurred. Please try again later.")
} 

});
}); 

}
{!REQUIRESCRIPT("https://apis.google.com/js/client.js?onload=handleClientLoad")};
handleClientLoad()

You can read full tutorial here: http://sfdcbeginner.com/google-calendar-integration-with-salesforce.html

  • Thank you very much , just another question : can we achieve the same thing without authentification or it is just impossible ? – Merabti iBRAHIM May 17 '16 at 9:33
  • without authentication how you will achieve? user need to put their gmail id right? and grant you permission – Himanshu May 17 '16 at 9:59
  • Hi @Himanshu, is this method still working. I tried you code, but I get a setApiKey error. – MnZ Oct 4 '16 at 5:28
  • @Himanshu I'm getting this error when clicking on the tutorial link: Deceptive site ahead Attackers on sfdcbeginner.com may trick you into doing something dangerous like installing software or revealing your personal information (for example, passwords, phone numbers, or credit cards). Learn more – Json Oct 16 at 14:23
  • @Json somehow my site got hacked. I have already submitted a review request with google. – Himanshu Oct 16 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.