How to create a calendar from salesforce page

H

To create a calendar from salesforce page to Google calendar use following code :

// VisualForce page

<apex:page controller="CreateCalendarController" sideBar="false">
<apex:form >
	<apex:outputPanel id="errorPanel">
		<apex:pageMessages />
	</apex:outputPanel>
	<apex:outputPanel id="mainPanel">
	<apex:pageBlock title="Create Calendar">
		<apex:pageBlockSection title="Calendar Input" columns="2">
			<apex:inputText value="{!inputCalendar.summary}" label="Summary: " required="true"/>
			<apex:inputText value="{!inputCalendar.description}" label="Description: " required="true"/>
			<apex:inputText value="{!inputCalendar.location}" label="Location: " required="true"/>
			<apex:selectList id="timeZone" value="{!inputCalendar.timeZone}" size="1" label="Time Zone: " required="true">
	            <apex:selectOption itemValue="America/Los_Angeles" itemLabel="America/Los_Angeles"/>
	        </apex:selectList>
	        <apex:pageBlockSectionItem />
	        <apex:pageBlockSectionItem dataStyle="text-align:center;">
	        	<apex:commandButton action="{!createCalendar}" value="Create Calendar" rerender="mainPanel, errorPanel"/>
	        </apex:pageBlockSectionItem>
		</apex:pageBlockSection>
		<apex:pageBlockSection title="Calendar Output" columns="2" id="calendarOutputPanel" rendered="{!isResponseReady}">
			<apex:outputText value="{!responseCalendar.id}" label="Id: " />
			<apex:outputText value="{!responseCalendar.etag}" label="etag: " />
			<apex:outputText value="{!responseCalendar.kind}" label="kind: " />
			<apex:outputText value="{!responseCalendar.summary}" label="Summary: " />
			<apex:outputText value="{!responseCalendar.description}" label="Description: " />
			<apex:outputText value="{!responseCalendar.location}" label="Location: " />
			<apex:outputText value="{!responseCalendar.timeZone}" label="Time Zone: " />
		</apex:pageBlockSection>
	</apex:pageBlock>
	</apex:outputPanel>
</apex:form>
</apex:page>

// Below is the code for apex createCalendarController
/**
* Controller for the CreateCalendar VF page
* Creates a Google Calendar by making an API call go Google Calendar API and processes the response back
*/
public class CreateCalendarController extends GoogleApiManager { �
  //calendar object to be serialized and passed to the API
  public GoogleCalendar inputCalendar {
    get {
      if(inputCalendar == null)
        inputCalendar = new GoogleCalendar();
      return inputCalendar;
    }
    set;
  }
  //calendar object to be deserialized from the API response
  public GoogleCalendar responseCalendar{
    get {
      if(responseCalendar == null)
        responseCalendar = new GoogleCalendar();
      return responseCalendar;
    }
    set;
  }
 �
  public Boolean isResponseReady {
    get { return responseCalendar.id != null; }
    set;
  }
 �
  public PageReference createCalendar(){
    try{
      String calendarJsonInput = inputCalendar.serialize(); �
      System.debug('calendarJsonInput: ' + calendarJsonInput);
      String calendarJsonOutput;
      //no callouts in test mode
      if(!Test.isRunningTest())
        calendarJsonOutput = doAPICall(CALENDAR_URL, calendarJsonInput, 'POST');
      else{
        doAPICall(CALENDAR_URL, calendarJsonInput, 'POST');
        calendarJsonOutput = testCalendarJson;
      }
      System.debug('calendarJsonOutput: ' + calendarJsonOutput);
      System.debug('isAPIError: ' + isAPIError);
      if(isAPIError)
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, calendarJsonOutput));
      else{
        responseCalendar.deserialize(calendarJsonOutput);
      }
    }
    catch(Exception e){
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage()));
      System.debug(e);
    }
    return null;
  }
}

// for methods such as serialize and deserialize or doAPICall see GoogleApp project on AppExchange.

About the author

prashant.wayal
By prashant.wayal

Category