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 a new record on deletion

Hi All,

I'm successfully passed my first trigger and have even created a few that work (with test cases) without bugging all of you, but now I'm stuck again :(.

I have a trigger that works successfully in my sandbox (an after delete trigger that creates a record in a custom object to track the history of opportunity splits... don't know why we can't just track history, but there you go ;).
trigger CreateSplitAuditRecordDelete on OpportunitySplit (after delete)
{
    if(!myStaticClass.flag)
    {
        List<Opportunity_Split_History__c> listDOSH = new List<Opportunity_Split_History__c>();
        
        for(OpportunitySplit Dos : trigger.old)
        {
           
 listDOSH.add(new Opportunity_Split_History__c(Name='Deleted '+ trigger.oldmap.get(Dos.ID).split,
                                               Action__c = 'Deleted',
                                               CurrencyIsoCode = trigger.oldmap.get(Dos.ID).CurrencyIsoCode,
                                               Modified_By__c =Dos.LastModifiedById,
                                               Modified_Date__c =Dos.LastModifiedDate,
                                               Opportunity__c =trigger.oldmap.get(Dos.ID).OpportunityId,
                                               Opp_Split_ID__c =trigger.oldmap.get(Dos.ID).ID,
                                               Split_Percentage_New__c =0,
                                               Split_Percentage_Old__c =trigger.oldmap.get(Dos.ID).SplitPercentage,
                                               Split_Team_Member_New__c =null,
                                               Split_Team_Member_Old__c =trigger.oldmap.get(Dos.ID).SplitOwnerId,
                                               Split__c =trigger.oldmap.get(Dos.ID).Split,
                                               Team_nnCMRR_New__c =0,
                                               Team_nnCMRR_Old__c =trigger.oldmap.get(Dos.ID).Team_nnCMRR__c));
                }
            
    if(listDOSH.size() > 0)
        {
            insert listDOSH;
        }}
        myStaticClass.flag = true ;
    }
This works perfectly..., but I cannot get the test case to work.  For some reason, my trigger isn't firing.  I delete my opportunity split, I can see that it has been deleted, but the new record that should be created in the opportunity split history is not showing up.
 
@isTest

private class Test_OpportunitySplitDelete {

    static testMethod void TestOpportunitySplitDelete() {
//Insert users with sales profile 
//If failing tests, be sure that the sales profile is still there       	
        
        Profile mySalesProfile = [SELECT Id FROM Profile WHERE Name='Starmind Sales User'];
      	User myUser = new User(Alias = 'standt', 
                               Email='standarduser@starmind.com', 
                               EmailEncodingKey='UTF-8', 
                               LastName='Testing', 
                               LanguageLocaleKey='en_US',
                               LocaleSidKey='en_US', 
                               ProfileId = mySalesProfile.Id,
                               TimeZoneSidKey='America/Los_Angeles',
                               UserName='standarduser@starmind.com',
                              isActive=True);
        insert myUser;
        User mySalesUser1 = new User(Alias = 'sales1', 
                               Email='salesuser1@starmind.com', 
                               EmailEncodingKey='UTF-8', 
                               LastName='Primary', 
                               LanguageLocaleKey='en_US',
                               LocaleSidKey='en_US', 
                               ProfileId = mySalesProfile.Id,
                               TimeZoneSidKey='America/Los_Angeles',
                               UserName='salesuser1@starmind.com',
                              isActive=True);
        insert mySalesUser1;
		User mySalesUser2 = new User(Alias = 'sales2', 
                               Email='salesuser2@starmind.com', 
                               EmailEncodingKey='UTF-8', 
                               LastName='Secondary', 
                               LanguageLocaleKey='en_US',
                               LocaleSidKey='en_US', 
                               ProfileId = mySalesProfile.Id,
                               TimeZoneSidKey='America/Los_Angeles',
                               UserName='salesuser2@starmind.com',
                              isActive=True);
        Insert mySalesUser2;
// The following code runs as user 'myUser'

        System.runAs(myUser) {
       
//Create test Account,Opportunity,Opportunity Split
        Account myAccount = new Account(name='TestAccount',
                                           currencyisocode='CHF',
                                           Region__c='CH');
    		insert myAccount;
    		
		Opportunity myOpportunity = new Opportunity(name='TestOpportunity',
                                                       AccountID=myAccount.Id,
                                                       CloseDate=date.today(),
                                                       StageName='5 Contract Sent',
                                                       CurrencyIsoCode=myAccount.CurrencyIsoCode,
                                                       OwnerId=myuser.id);
    		      
            insert myOpportunity;
            
//Selecting the opportunity split type, if more than one is active, next line will need to change            
		OpportunitySplitType ost = [Select ID from OpportunitySplitType where isactive = true];
            
//Defining variable for original split that was created with the opportunity        
        OpportunitySplit osOwner = [SELECT CreatedById,CreatedDate,CurrencyIsoCode,Id,IsDeleted,LastModifiedById,LastModifiedDate,
                                   OpportunityId,Split,SplitAmount,SplitNote,SplitOwnerId,SplitPercentage,SplitTypeId,
                                   SystemModstamp,Team_nnCMRR__c FROM OpportunitySplit WHERE OpportunityId = :myOpportunity.id and SplitOwnerID = :myUser.Id and SplitTypeID = :ost.id];

         system.debug('owner split, 100%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osOwner.id]);            
//Adding new opportunity split -- to be deleted later             
        OpportunitySplit osDelete = new OpportunitySplit(SplitTypeId=ost.ID,
                                                      OpportunityID=myOpportunity.ID,
                                                      SplitOwnerID=mySalesUser1.id,
                                                      SplitPercentage=60);
                            
            insert osDelete; 
		system.debug('delete split, created at 60%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osDelete.id]);            
            
         OpportunitySplit osAdd = new OpportunitySplit(SplitTypeId=ost.ID,
                                                      OpportunityID=myOpportunity.ID,
                                                      SplitOwnerID=mySalesUser2.id,
                                                      SplitPercentage=20);
                            
            insert osAdd; 
		system.debug('add split, created at 20%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osAdd.id]);            

            
//Check to see that new opp split was created  
system.debug('updated owner split, to 20%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osOwner.id]);
system.debug('delete split, stays 60%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osDelete.id]);

            
//Delete newly created record to test the deletion trigger
            delete [SELECT Id FROM OpportunitySplit WHERE ID = :osDelete.id];

//check new percentages; did the new opp split get deleted This works
system.debug('original split, to 80%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osOwner.id]);
system.debug('does the record show as deleted?'+ [SELECT Id,isDeleted,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osDelete.id all rows]);
system.debug('should have no records'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osDelete.id]);            
OpportunitySplit Deleted = [SELECT Id, IsDeleted FROM opportunitysplit WHERE Id = :osDelete.Id ALL ROWS];
System.assertEquals(deleted.IsDeleted, true);
          
//Validate deletion recorded in the history table  This doesn't work              
system.debug('should show 1 record with delete action' + [SELECT Action__c,Id,IsDeleted,Split__c,Split_Percentage_New__c,Split_Percentage_Old__c, Split_Team_Member_New__c
              FROM Opportunity_Split_History__c where Opp_Split_ID__c = :osDelete.id]); 
            
Opportunity_Split_History__c OSH1 =  [SELECT Action__c,CreatedById,CreatedDate,Id,IsDeleted,
                                                          LastModifiedById,LastModifiedDate,Modified_By__c,Modified_Date__c,
                                                          Name,Opportunity__c,Opp_Split_ID__c,OwnerId,Split_Percentage_New__c,
                                                          Split_Percentage_Old__c,Split_Team_Member_New__c,
                                                          Split_Team_Member_Old__c,Split__c,SystemModstamp,Team_nnCMRR_New__c,
                                                          Team_nnCMRR_Old__c, Opportunity_Split_History__c.CurrencyIsoCode
                                                  	FROM Opportunity_Split_History__c 
                                                  	WHERE Action__c = 'Deleted' /*and Opp_Split_ID__c = :osDelete.Id*/]; 
                                       
/*           	System.assertEquals('Deleted', OSH1.Action__c, 'Expected Action = Deleted');
			System.assertEquals('CHF', OSH1.CurrencyIsoCode, 'Expected CurrencyIsoCode =CHF');
            System.assertEquals(myuser.id, OSH1.Modified_By__c, 'Expected Modified by = MyUser');
 			System.assertEquals(osDelete.LastModifiedDate, (OSH1.Modified_Date__c), 'Expected Modified date = Today');
			System.assertEquals(myOpportunity.id, OSH1.Opportunity__c, 'Expected Opportunity = TestOpportunity');
			System.assertEquals(0, OSH1.Split_Percentage_New__c , 'Expected Split_Percentage_New__c  to = null');
            System.assertEquals(0, OSH1.Split_Percentage_Old__c, 'Expected Split_Percentage_Old__c =0');   
 			System.assertEquals(mySalesUser1.Id, OSH1.Split_Team_Member_Old__c, 'Expected Split_Team_Member_Old__c = primary');
            System.assertEquals(null, OSH1.Split_Team_Member_New__c, 'Expected Split_Team_Member_New__c =null');
            System.assertEquals(0, OSH1.Team_nnCMRR_New__c, 'Expected Team_nnCMRR_New__c = 0');
            System.assertEquals(0, OSH1.Team_nnCMRR_Old__c, 'Expected Team_nnCMRR_Old__c = null');

Any thoughts would be greatly appreciated :).

Thanks,

Jodi