• Roger Eyre
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
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.
I have this trigger:

trigger deleteContactReceiver on Contact (before delete) 
{
    List<Contact> con=Trigger.new;
    GDPRContact.deleteContactURN(con); 
}

Invokes:

public static void deleteContactURN(List<Contact> con)
    { 
    for(Contact c:con)
        {
            try
                {
                List<TWAM_URN__c> preURN=[select 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];
                preURN.get(0).TWAM_SFID__c='';
                System.debug('**** URN IS="+preURN.get(0).TWAM_SFID__c+" ****: ');
                preURN.get(0).TWAM_Last_Name__c='';
                preURN.get(0).TWAM_URN_Allocation_Time__c=null;
                preURN.get(0).TWAM_URN_Is_Assigned__c=FALSE;
                update preURN;
                }    
            catch(Exception e) 
                {
                System.debug('**** An unexpected error has occurred ****: ' + e.getMessage());
                }
           }  // end for 
                
    }  

The testMethod I'm trying to execute is:

public static testMethod void deleteContact()
        {
        Test.startTest();
        Contact con1=new Contact();
        con1=[select Id, AccountId from Contact where Id='0031o00001TWS6RAAX'];
        delete con1;
        List<TWAM_URN__c> postURN2=[select TWAM_SFID__c,TWAM_URN_Is_Assigned__c from TWAM_URN__c where TWAM_SFID__c=:con1.Id LIMIT 1];
        System.assertEquals(FALSE, postURN2.get(0).TWAM_URN_Is_Assigned__c);
        System.assertEquals(null, postURN2.get(0).TWAM_SFID__c);
            
        Test.stopTest();
        }

The line throwing the error is highlighted in bold. A single row in the DB does exist, verified by looking at debug logs.  I know I should not deploy like this as I may have a list of contacts, just trying to get some code working.  

Can you help?  I have spent most of the day trying to solve this one. 

Best wishes,

Roger.
I have this trigger:

trigger deleteContactReceiver on Contact (before delete) 
{
    List<Contact> con=Trigger.new;
    GDPRContact.deleteContactURN(con); 
}

Invokes:

public static void deleteContactURN(List<Contact> con)
    { 
    for(Contact c:con)
        {
            try
                {
                List<TWAM_URN__c> preURN=[select 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];
                preURN.get(0).TWAM_SFID__c='';
                System.debug('**** URN IS="+preURN.get(0).TWAM_SFID__c+" ****: ');
                preURN.get(0).TWAM_Last_Name__c='';
                preURN.get(0).TWAM_URN_Allocation_Time__c=null;
                preURN.get(0).TWAM_URN_Is_Assigned__c=FALSE;
                update preURN;
                }    
            catch(Exception e) 
                {
                System.debug('**** An unexpected error has occurred ****: ' + e.getMessage());
                }
           }  // end for 
                
    }  

The testMethod I'm trying to execute is:

public static testMethod void deleteContact()
        {
        Test.startTest();
        Contact con1=new Contact();
        con1=[select Id, AccountId from Contact where Id='0031o00001TWS6RAAX'];
        delete con1;
        List<TWAM_URN__c> postURN2=[select TWAM_SFID__c,TWAM_URN_Is_Assigned__c from TWAM_URN__c where TWAM_SFID__c=:con1.Id LIMIT 1];
        System.assertEquals(FALSE, postURN2.get(0).TWAM_URN_Is_Assigned__c);
        System.assertEquals(null, postURN2.get(0).TWAM_SFID__c);
            
        Test.stopTest();
        }

The line throwing the error is highlighted in bold. A single row in the DB does exist, verified by looking at debug logs.  I know I should not deploy like this as I may have a list of contacts, just trying to get some code working.  

Can you help?  I have spent most of the day trying to solve this one. 

Best wishes,

Roger.