+ Start a Discussion
Daniel B ProbertDaniel B Probert 

Public Calendar Apex Lookup Query

Hi All,

I have a simple holiday leave app in salesforce that i have added a trigger to that will create an event. I wanted to create the event in a public calender so that they are all viewable from one location.

This is what i have so far which works:

trigger Trg_Employee_Leave_Event on Leave__c (after insert, after update) {
if(Trigger.isInsert || Trigger.isUpdate)
    {
        List<Event> events = new List<Event>();
        ID UKCAL =  '023C0000002UQsP';
        List<Leave__c> Opps = Trigger.new;
        for (Leave__c Opp : Opps)
        {
            if(opp.Status__c=='Approved'){
                Event evt = new Event();
                if(opp.Country__c=='United Kingdom'){
                evt.Ownerid = UKCAL;
                }
                if(opp.Country__c!='United Kingdom'){
                evt.Ownerid = UserInfo.getUserID();
                }
                evt.Subject = opp.Leave_Type__c;      
                evt.StartDateTime = Opp.Start_Date__c;
                evt.EndDateTime = Opp.Return_Date__c;  
                evt.isAllDayEvent = true;
                evt.WhatID = opp.Employee__c;
                evt.LeaveID__c = opp.ID;
            Events.add(evt);
          
        }
        }
        insert events;
    }
}

now my only problem with this is the line:

ID UKCAL =  '023C0000002UQsP';

I would prefer if this was a select id from ??? where name = 'UK Leave Calendar'

but i can't for the life of my find what i do the select from to get said ID.

I keep reading that it's not possible as it's kind of hidden but hoping that's not true as hard coding ID is bad practice.

Any guidance appreciated.

Dan
Daniel B ProbertDaniel B Probert
ok i figured out how I can do this, i've had to build custom setting store the calender id's in there.