function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
George Laird 12George Laird 12 

Help with APEX Trigger on Individual Link Level Detail from Marketing Cloud Connector

Hello All,

I'm completely stuck here.  I wrote a trigger to fire after update on the object "Individual Link Level Detail" which is part of the Marketing Cloud Connector.  Basically, whenever someone clicks on a link, i need to update a score on a custom object that's related to the Contact that clicked the link.  That all works fine, but the problem is the way Marketing Cloud updates those objects using the bulk api.  

When I click on one link, the trigger fires, calls my apex class, and works great.  The score increases.  
When I click on mulitiple links, they get put into a bulk api call from MC, and then nothing really works correctly. 

The problem also is that the dubug logs seems to say the update is happening, although the values don't change.  OR they change for one link, but not the next other ones.  I have no idea what's going on here.   Here is my trigger and my code:

For example:  I'll open the email and click on two links.  SiteVisit and WhitepaperDownload.   My trigger runs and calls the apex class twice, once for each link update.  It will only update one of the two records though.  If I open the email and just click on ONE link, it'll work every time. 



TRIGGER:

trigger ClicksTrigger on et4ae5__IndividualLink__c (after insert, after update) {
   
    if(Trigger.isInsert){ 
        for (et4ae5__IndividualLink__c click : Trigger.new){
        
        GivePointsForClicks.givePoints(click.Id);
    
           }
    } 
    
    else if(Trigger.isUpdate){
        
        for (et4ae5__IndividualLink__c click : Trigger.new){
            
            //Get the old value of the number of clicks
            et4ae5__IndividualLink__c il = Trigger.oldMap.get(click.Id);
            double oldValue = il.et4ae5__NumberOfTotalClicks__c;
            system.debug('The old value for clicks was: '+oldValue);
            //Get the new value of the number of clicks
            double newValue = click.et4ae5__NumberOfTotalClicks__c;
            system.debug('The new value for clicks is: '+newValue);
            //Subtrack old value from new value to get the delta value
            double deltaValue = newValue - oldValue;
            system.debug('The delta value is: '+deltaValue);
            
            if(deltaValue > 0){
                
                
                CountClicks.giveClicks(click.id,deltaValue);    
                
            }
            

            
        }
        
    }
    
}


APEX CLASS: 

public class CountClicks {
    
    @future
    public static void giveClicks(id linkID,double deltaValue){
     
        //Get the ContactId from the related Email Result record (who did the clicking).
        List<et4ae5__IndividualLink__c> ContactList = [SELECT et4ae5__Individual_Email_Result__r.et4ae5__Contact__r.id 
                                                       FROM et4ae5__IndividualLink__c 
                                                       WHERE et4ae5__IndividualLink__c.id =: linkID];
        Id ContactID = ContactList.get(0).et4ae5__Individual_Email_Result__r.et4ae5__Contact__r.id;
        system.debug('The Contact Id: '+ContactId);
         
        //Get the most recent scoring record from that Contact
        List<Scoring__c> scoringRecords = [SELECT id,name,
                                          Scoring__c.Whitepaper_Download_Click_CountCES1__c,Scoring__c.Site_Visit_Click_CountCES1__c,Scoring__c.Blog_Visit_Click_CountCES1__c,Scoring__c.Resource_Center_Visit_Click_CountCES1__c,Scoring__c.Video_Watched_Click_CountCES1__c,
                                          Scoring__c.Whitepaper_Download_Click_CountCES2__c,Scoring__c.Site_Visit_Click_CountCES2__c,Scoring__c.Blog_Visit_Click_CountCES2__c,Scoring__c.Resource_Center_Visit_Click_CountCES2__c,Scoring__c.Video_Watched_Click_CountCES2__c,
                                          Scoring__c.Whitepaper_Download_Click_CountCES3__c,Scoring__c.Site_Visit_Click_CountCES3__c,Scoring__c.Blog_Visit_Click_CountCES3__c,Scoring__c.Resource_Center_Visit_Click_CountCES3__c,Scoring__c.Video_Watched_Click_CountCES3__c,
                                          Scoring__c.Whitepaper_Download_Click_CountCES4__c,Scoring__c.Site_Visit_Click_CountCES4__c,Scoring__c.Blog_Visit_Click_CountCES4__c,Scoring__c.Resource_Center_Visit_Click_CountCES4__c,Scoring__c.Video_Watched_Click_CountCES4__c
                                          FROM  Scoring__c 
                                          WHERE RelatedContact__c  =: ContactId 
                                          ORDER BY createdDate 
                                          DESC limit 1];
        Scoring__c scoringRecord = scoringRecords[0];
        system.debug('The Scoring Record: '+scoringRecord);
        system.debug('The Scoring Record Name: '+scoringRecord.Name);
        
        //Get the name of the Link clicked
        List<et4ae5__IndividualLink__c> LinkList = [SELECT name FROM et4ae5__IndividualLink__c WHERE et4ae5__IndividualLink__c.id =: linkID];
        string linkName = LinkList.get(0).Name;
        system.debug('The linked clicked was: '+linkName);
        
        //Give points to the appropriate CES category and link.
        if(linkName.contains('CES1')){
            
            if(linkName.contains('Whitepaper')){
                            
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Whitepaper_Download_Click_CountCES1__c;
                scoringRecord.Whitepaper_Download_Click_CountCES1__c = currentValue+deltaValue;
                update scoringRecord;
            }
            li
            else if(linkName.contains('Website')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Site_Visit_Click_CountCES1__c;
                scoringRecord.Site_Visit_Click_CountCES1__c = currentValue+deltaValue;
                update scoringRecord;
          
            }
            
            else if(linkName.contains('Blog')){
             
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Blog_Visit_Click_CountCES1__c;
                scoringRecord.Blog_Visit_Click_CountCES1__c = currentValue+deltaValue;
                update scoringRecord;
                 
            }
            
             else if(linkName.contains('ResourceCenter')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Resource_Center_Visit_Click_CountCES1__c;
                scoringRecord.Resource_Center_Visit_Click_CountCES1__c = currentValue+deltaValue;
                update scoringRecord;
  
            }
            
            else if(linkName.contains('Video')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Video_Watched_Click_CountCES1__c;
                scoringRecord.Video_Watched_Click_CountCES1__c = currentValue+deltaValue;
                update scoringRecord;
               
            }            
        }
        
        else if(linkName.contains('CES2')){
            
            if(linkName.contains('Whitepaper')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Whitepaper_Download_Click_CountCES2__c;
                scoringRecord.Whitepaper_Download_Click_CountCES2__c = currentValue+deltaValue;
                update scoringRecord;
                              
            }
            
            else if(linkName.contains('Website')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Site_Visit_Click_CountCES2__c;
                scoringRecord.Site_Visit_Click_CountCES2__c = currentValue+deltaValue;
                update scoringRecord;
  
            }
            
            else if(linkName.contains('Blog')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Blog_Visit_Click_CountCES2__c;
                scoringRecord.Blog_Visit_Click_CountCES2__c = currentValue+deltaValue;
                update scoringRecord;
  
            }
            
             else if(linkName.contains('ResourceCenter')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Resource_Center_Visit_Click_CountCES2__c;
                scoringRecord.Resource_Center_Visit_Click_CountCES2__c = currentValue+deltaValue;
                update scoringRecord;
  
            }
            
            else if(linkName.contains('Video')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Video_Watched_Click_CountCES2__c;
                scoringRecord.Video_Watched_Click_CountCES2__c = currentValue+deltaValue;
                update scoringRecord;
  
            }            
        }
        
        else if(linkName.contains('CES3')){
            
            if(linkName.contains('Whitepaper')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Whitepaper_Download_Click_CountCES3__c;
                scoringRecord.Whitepaper_Download_Click_CountCES3__c = currentValue+deltaValue;
                update scoringRecord;
            }
            
            else if(linkName.contains('Website')){
                
               //Add delta value to scoring record click count
                double currentValue = scoringRecord.Site_Visit_Click_CountCES3__c;
                scoringRecord.Site_Visit_Click_CountCES3__c = currentValue+deltaValue;
                update scoringRecord;
  
            }
            
            else if(linkName.contains('Blog')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Blog_Visit_Click_CountCES3__c;
                scoringRecord.Blog_Visit_Click_CountCES3__c = currentValue+deltaValue;
                update scoringRecord;
  
            }
            
             else if(linkName.contains('ResourceCenter')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Resource_Center_Visit_Click_CountCES3__c;
                scoringRecord.Resource_Center_Visit_Click_CountCES3__c = currentValue+deltaValue;
                update scoringRecord;
  
            }
            
            else if(linkName.contains('Video')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Video_Watched_Click_CountCES3__c;
                scoringRecord.Video_Watched_Click_CountCES3__c = currentValue+deltaValue;
                update scoringRecord;
  
            }            
        }
        
        else if(linkName.contains('CES4')){
            
            if(linkName.contains('Whitepaper')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Whitepaper_Download_Click_CountCES4__c;
                scoringRecord.Whitepaper_Download_Click_CountCES4__c = currentValue+deltaValue;
                update scoringRecord;
                
            }
            
            else if(linkName.contains('Website')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Site_Visit_Click_CountCES4__c;
                scoringRecord.Site_Visit_Click_CountCES4__c = currentValue+deltaValue;
                update scoringRecord;
  
            }
            
            else if(linkName.contains('Blog')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Blog_Visit_Click_CountCES4__c;
                scoringRecord.Blog_Visit_Click_CountCES4__c = currentValue+deltaValue;
                update scoringRecord;
  
            }
            
            else if(linkName.contains('ResourceCenter')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Resource_Center_Visit_Click_CountCES4__c;
                scoringRecord.Resource_Center_Visit_Click_CountCES4__c = currentValue+deltaValue;
                update scoringRecord;
  
            }
            
            else if(linkName.contains('Video')){
                
                //Add delta value to scoring record click count
                double currentValue = scoringRecord.Video_Watched_Click_CountCES4__c;
                scoringRecord.Video_Watched_Click_CountCES4__c = currentValue+deltaValue;
                update scoringRecord;
  
            }            
        }        
    }
}