+ Start a Discussion
Roger EyreRoger Eyre 

APEX custom object row update fails silently

I am trying to update a record from a test class, no errors are thrown but when I do a select of the updated record for assertion checking, no changes have been made, as detected by " System.assertEquals(FALSE, preURN.get(0).TWAM_URN_Is_Assigned__c);"

Here is the code:

 TRIGGER:

     trigger deleteContactReceiver on Contact (before delete) 
    {
        List<Contact> con = New List<Contact>();
        
        If(Trigger.IsBefore && Trigger.IsDelete)
         {     
            For(Contact c : Trigger.Old)
             {
               con.add(c);
              }
         }
         GDPRContact.deleteContactURN(con);
    }

METHOD CALL:

    public static void deleteContactURN(List<Contact> con)
        { 
        for(Contact c:con)
            {
                try
                    {
                    TWAM_URN__c preURN=[select Name, URN__c, TWAM_SFID__c, TWAM_Last_Name__c, TWAM_URN_Allocation_Time__c, TWAM_URN_Is_Assigned__c from TWAM_URN__c where TWAM_SFID__c=:c.TWAM_SFID__c LIMIT 1];
                   preURN.TWAM_SFID__c=null;
                    preURN.TWAM_Last_Name__c=null;
                    preURN.TWAM_URN_Allocation_Time__c=null;
                    preURN.TWAM_URN_Is_Assigned__c=FALSE;
                    update preURN;
                    }    
                catch(Exception e) 
                    {
                     System.debug('**** An unexpected error has occurred ****: ' + e.getMessage());
                    }
               }  // end for 
                    
       }  

TEST INVOCATION:

    public static testMethod void deleteContact()
            {
            Test.startTest();
        
            //Test cases are added using different Salesforce ID's to ensure there is no risk of pulling up the same contact after a later test case has executed, triggers may batch up inserts
        
            Contact con1=new Contact(LastName='Test1001'); 
            insert con1;
            Contact con2=[select Id, AccountId, LastName from Contact where LastName='Test1001'];

            delete con1;
            List<TWAM_URN__c> preURN=[select Name, TWAM_Last_Name__c, TWAM_SFID__c, TWAM_URN_Allocation_Time__c, TWAM_URN_Is_Assigned__c from TWAM_URN__c where TWAM_Last_Name__c=:con2.LastName];
        
            System.assertEquals(FALSE, preURN.get(0).TWAM_URN_Is_Assigned__c);
            System.assertEquals(null, preURN.get(0).TWAM_SFID__c);
            
            Test.stopTest();
            }

I thought "Name" was the ID field that I needed to make sure an update occurred.  

Can you help?

Thanks,

Roger.
NagendraNagendra (Salesforce Developers) 
Hi Roger,

Please give a try by removing the try-catch block from the deleteContactURN method and you will receive your exceptions using which you can move further.Also, I would suggest you never use an empty catch block.As you are also doing SOQL in a loop in your trigger handler, which might also be causing the above issue.

Thanks,
Nagendra