Calendar in suitelet script to display all CRM activities

If you have to create a calendar in a suitelet script to display an employee’s CRM activities without considering the time of the activities, you can use this code.

/**

 *@NApiVersion 2.1

 *@NScriptType Suitelet

 *@NModuleScope SameAccount

 */

define([‘N/ui/serverWidget’, ‘N/search’, ‘N/query’],

  function (serverWidget, search, query,) {

    /**

     * Defines the custom eventList function to an array of object of all activities.

    * @param {interger} employeeId – incoming employeeId value

    * @returns {array} – array of object of activities including phone call, events, and tasks

    */

    function eventList(employeeId) {

      let eventList = [];

      let suiteql = `SELECT title,startdate,completeddate,status FROM phonecall WHERE assigned =${employeeId}`;

      let queryResult = query.runSuiteQL({ query: suiteql }).asMappedResults();

      for (let i = 0; i < queryResult.length; i++) {

        let subject = queryResult[i].title;

        let date = queryResult[i].startdate;

        let status = queryResult[i].status;

        let dateCompleted = queryResult[i].completeddate;

        if (!dateCompleted) {

          dateCompleted = queryResult[i].startdate;

        }

        eventList.push({

          title: subject,

          start: new Date(date),

          end: new Date(dateCompleted)

        });

      }

      suiteql = `SELECT title,startdate,completeddate,status FROM Task WHERE assigned =${employeeId}`;

      queryResult = query.runSuiteQL({ query: suiteql }).asMappedResults();

      for (let i = 0; i < queryResult.length; i++) {

        let subject = queryResult[i].title;

        let date = queryResult[i].startdate;

        let status = queryResult[i].status;

        let dateCompleted = queryResult[i].completeddate;

        if (!dateCompleted) {

          dateCompleted = queryResult[i].startdate;

        }

        eventList.push({

          title: subject,

          start: new Date(date),

          end: new Date(dateCompleted)

        });

      }

      suiteql = `SELECT title,startdate,completeddate,status FROM CalendarEvent WHERE organizer =${employeeId}`;

      queryResult = query.runSuiteQL({ query: suiteql }).asMappedResults();

      for (let i = 0; i < queryResult.length; i++) {

        let subject = queryResult[i].title;

        let date = queryResult[i].startdate;

        let status = queryResult[i].status;

        let dateCompleted = queryResult[i].completeddate;

        if (!dateCompleted) {

          dateCompleted = queryResult[i].startdate;

        }

        eventList.push({

          title: subject,

          start: new Date(date),

          end: new Date(dateCompleted)

        });

      }

      return eventList;

    }

    /**

     * Defines the Suitelet script trigger point.

     * @param {Object} scriptContext

     * @param {ServerRequest} scriptContext.request – Incoming request

     * @param {ServerResponse} scriptContext.response – Suitelet response

     * @since 2015.2

     */

    function onRequest(scriptContext) {

      if (scriptContext.request.method === ‘GET’) {

        try {

//set the default value for the employee or dynamically obtain it from the parameter

          let employeeId = 5;

          let form = serverWidget.createForm({

            title: ‘Calendar’

          });

          // Add an inline HTML field to embed FullCalendar

          let htmlField = form.addField({

            id: ‘custpage_calendar’,

            type: serverWidget.FieldType.INLINEHTML,

            label: ‘Calendar’

          });

          let events = eventList(employeeId);

          log.error({

            title: “events”,

            details: events

          })

          // HTML code to include FullCalendar with no events for now

          let calendarHTML = `

        <link href=’https://cdn.jsdelivr.net/npm/fullcalendar@5.10.1/main.min.css’ rel=’stylesheet’ />

        <script src=’https://cdn.jsdelivr.net/npm/fullcalendar@5.10.1/main.min.js’></script>

        <div id=’calendar’></div>

        

        <script>

          document.addEventListener(‘DOMContentLoaded’, function() {

            let calendarEl = document.getElementById(‘calendar’);

            

            let calendar = new FullCalendar.Calendar(calendarEl, {

              initialView: ‘dayGridMonth’,

              events: ` + JSON.stringify(events) + `,

              displayEventTime: false

            });

            // Render the calendar

            calendar.render();

          });

        </script>

        <style>

          #calendar {

            max-width: 900px;

            margin: 0 auto;

            padding: 20px;

          }

        </style>

      `;

          // Set the inline HTML field’s value to the calendar HTML

          htmlField.defaultValue = calendarHTML;

          // Write the form back to the client

          scriptContext.response.writePage(form);

        }

        catch (error) {

          log.error(“Error @ GET request”, error.message);

        }

      }

    }

    return {

      onRequest: onRequest

    };

  });

NB:

Here suiteQL is used to improve the speed of execution. Hence it would be best if you were mindful of the number of results that will be returned from the search.

The time of the events will not be shown as “ displayEventTime: false ” property is given. If the time is also needed to be displayed then please remove this line and in the object of the eventList array, add time in the start and end properties. The startime and endtime of the activities are not obtained using suiteQL, hence a saved search can be used.

Leave a comment

Your email address will not be published. Required fields are marked *