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
Jodi SpitlerJodi Spitler 

Creating an audit trail for opp split records

Hello out there :),

As I've discovered that there is no OOTB history for the opportunity split records, I've tried to create my own with a custom object and the code below.  Unfortunately, it is not catching the delete action, nor is it catching the insert when it is a part of the update of adding a new team member with a new percentage split.  Any ideas?  My thoughts have been centering around using trigger.old for delete and the fact that there is some automation already around the opportunity split record that is happening behind the scenes.  Any help would be greatly appreciated :)

Thanks,
Jodi


trigger CreateSplitAuditRecord on OpportunitySplit (after delete, after insert, after update)
{
    if(!myStaticClass.flag)
    {
        List<Opportunity_Split_History__c> listOSH = new List<Opportunity_Split_History__c>();
        
        for(OpportunitySplit os : trigger.new)
        {
            if(Trigger.isDelete)
                {
             listOSH.add(new Opportunity_Split_History__c(Name='Deleted '+ os.split,
                                                          Action__c = 'Deleted',
                                                          CurrencyIsoCode = os.CurrencyIsoCode,
                                                          Modified_By__c =os.LastModifiedById,
                                                          Modified_Date__c =os.LastModifiedDate,
                                                          Opportunity__c =os.OpportunityId,
                                                          Opp_Split_ID__c =os.ID,
                                                          /*Split_Note_New__c =null,
                                                          Split_Note_Old__c =trigger.oldmap.get(os.ID).SplitNote,*/
                                                          Split_Percentage_New__c =0,
                                                          Split_Percentage_Old__c =trigger.oldmap.get(os.ID).SplitPercentage,
                                                          Split_Team_Member_New__c =null,
                                                          Split_Team_Member_Old__c =trigger.oldmap.get(os.ID).SplitOwnerId,
                                                          Split__c =trigger.oldmap.get(os.ID).Split,
                                                          Team_nnCMRR_New__c =null,
                                                          Team_nnCMRR_Old__c =trigger.oldmap.get(os.ID).Team_nnCMRR__c));
                }   
        {
            if(Trigger.isInsert)
            {
             listOSH.add(new Opportunity_Split_History__c(Name='Created '+ os.split,
                                                          Action__c = 'Created',
                                                          CurrencyIsoCode = os.CurrencyIsoCode,
                                                          Modified_By__c =os.LastModifiedById,
                                                          Modified_Date__c =os.LastModifiedDate,
                                                          Opportunity__c =os.OpportunityId,
                                                          Opp_Split_ID__c =os.ID,
                                                          /*Split_Note_New__c =os.SplitNote,
                                                          Split_Note_Old__c =null,*/
                                                          Split_Percentage_New__c =os.SplitPercentage,
                                                          Split_Percentage_Old__c =0,
                                                          Split_Team_Member_Old__c =null,
                                                          Split_Team_Member_New__c =os.SplitOwnerId,
                                                          Split__c =os.Split,
                                                          Team_nnCMRR_New__c =os.Team_nnCMRR__c,
                                                          Team_nnCMRR_Old__c = null));
                }
         if(Trigger.isUpdate)
         {
             listOSH.add(new Opportunity_Split_History__c(Name='Upserted '+ os.split,
                                                          Action__c = 'Upserted',
                                                          CurrencyIsoCode = os.CurrencyIsoCode,
                                                          Modified_By__c =os.LastModifiedById,
                                                          Modified_Date__c =os.LastModifiedDate,
                                                          Opportunity__c =os.OpportunityId,
                                                          Opp_Split_ID__c =os.ID,
                                                          /*Split_Note_New__c =os.SplitNote,
                                                          Split_Note_Old__c =Trigger.oldMap.get(os.Id).Split,*/
                                                          Split_Percentage_New__c =os.SplitPercentage,
                                                          Split_Percentage_Old__c =Trigger.oldMap.get(os.Id).SplitPercentage,
                                                          Split_Team_Member_New__c =os.SplitOwnerId,
                                                          Split_Team_Member_Old__c =Trigger.oldMap.get(os.Id).SplitOwnerId,
                                                          Split__c =os.Split,
                                                          Team_nnCMRR_New__c =os.Team_nnCMRR__c,
                                                          Team_nnCMRR_Old__c =Trigger.oldMap.get(os.Id).Team_nnCMRR__c));
                }
                   
            
            }
        
    if(listOSH.size() > 0)
        {
            insert listOSH;
        }}
        myStaticClass.flag = true ;
    }}