+ Start a Discussion
LudivineLudivine 

cloud scheduler Not working because of a trigger

Hi all,

We want to use Cloud Scheduler but we get an error due to a trigger on Event Update/insert, when we disable this trigger Cloud Scheduler works perfectly.
Can we amend this trigger in order to avoid it to fire when we use Cloud scheduler for a meeting?
trigger Event_On_Update on Event (before update) {

    // Initialise ID maps for related sObjects
    map<Id, Contact> mapContact   = new map<Id, Contact>();
    map<Id, Lead> mapLead         = new map<Id, Lead>();
    map<Id, Contract> mapContract = new map<Id, Contract>();
    map<Id, Account> mapAccount   = new map<Id, Account>();
    map<Id, Opportunity> mapOpp   = new map<Id, Opportunity>();
    
    // Loop through event and fill the ID maps
    for(Event sEvent : trigger.new)
    {
        if(sEvent.WhoId != null && String.ValueOf(sEvent.WhoId).startsWith('003')){
          mapContact.put(sEvent.WhoId, null);
        }
        if (sEvent.WhoId != null && String.ValueOf(sEvent.WhoId).startsWith('00Q')){
          mapLead.put(sEvent.WhoId, null);
        }
        
        if(sEvent.WhatId != null && String.ValueOf(sEvent.WhatId).startsWith('001')){
          mapAccount.put(sEvent.WhatId, null);
        }
        if(sEvent.WhatId != null && String.ValueOf(sEvent.WhatId).startsWith('800')){
          mapContract.put(sEvent.WhatId, null);
        }
        if(sEvent.WhatId != null && String.ValueOf(sEvent.WhatId).startsWith('006')){
          mapContract.put(sEvent.WhatId, null);
        }
    }
    
    // Fill the Id -- > sObject maps for Contacts
    for(Contact arrContact : [
                                    select  Id,
                                            Account.Name,
                                            Name
                                    from    Contact
                                    where   id in :mapContact.keySet()
                                ])
    {
  
        mapContact.put(arrContact.Id, arrContact );
    }
    // Fill the Id -- > sObject maps for Leads
    for(Lead arrLead : [
                                    select  Id,
                                            Name,
                                            Company
                                    from    Lead
                                    where   id in :mapLead.keySet()
                                ])
    {
        mapLead.put(arrLead.Id, arrLead );
    }
    
    
    // Fill the Id -- > sObject maps for Contracts
    for(Contract arrContract : [
                                    select  Id,
                                            Account.Name,
                                            Contract_Name_Region_Location__c
                                    from    Contract
                                    where   id in :mapContract.keySet()
                                ])
    {
        mapContract.put(arrContract.Id, arrContract );
    }
    // Fill the Id -- > sObject maps for Opportunities
    for(Opportunity arrOpp : [
                                    select  Id,
                                            Account.Name
                                    from    Opportunity
                                    where   id in :mapOpp.keySet()
                                ])
    {
        mapOpp.put(arrOpp.Id, arrOpp );
    }
    // Fill the Id -- > sObject maps for Accounts
    for(Account arrAccount: [
                                    select  Id,
                                            Name
                                    from    Account
                                    where   id in :mapAccount.keySet()
                                ])
    {
        mapAccount.put(arrAccount.Id, arrAccount);
    }
    
    for(Event sEvent : trigger.new)
    {
        Contact thisContact    = mapContact.get(sEvent.WhoId);
        Lead    thisLead       = mapLead.get(sEvent.WhoId);
        
        Contract thisContract  = mapContract.get(sEvent.WhatId);
        Account  thisAccount   = mapAccount.get(sEvent.WhatId);
        Opportunity thisOpp    = mapOpp.get(sEvent.WhatId);
        
         //Update dates for Event
        sEvent.DisplayDateStart__c = sEvent.StartDateTime.format();
        sEvent.DisplayDateEnd__c = sEvent.EndDateTime.format();

        if(thisContact != null)
        {
          sEvent.DisplayRelatedWho__c = thisContact.Name + ', ' + thisContact.Account.Name;
        }
        if(thisLead != null)
        {
          sEvent.DisplayRelatedWho__c = thisLead.Name + ', ' + thisLead.Company;
        }
        
        if(thisContract != null)
        {
          sEvent.DisplayRelatedTo__c = thisContract.Contract_Name_Region_Location__c + ', ' + thisContract.Account.Name;
        }
        if(thisAccount != null)
        {
          sEvent.DisplayRelatedTo__c = thisAccount.Name;
        }
        if(thisOpp != null)
        {
          sEvent.DisplayRelatedTo__c = thisOpp.Name + ', ' + thisOpp.Account.Name;
        }
   
}

}