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
netbrainnetbrain 

Trigger

I have one question about the Trigger problem, and the problem details is:

Account
   Name:ABC
Contact
   Email:a1@a.a


The Account(Name:ABC) plays the role as container and the Contact(Email:al@a.a) plays the role as being container.

For testing the relationships between the Account(Name:ABC) and Contact(Email:al@a.a), I have written down one testing method

as following:


@isTest

private with sharing class Test {
    static testMethod void myUnitTest()
    {
    List<Contact> sdfe=[select id,AccountId from Contact where Contact.Email=:'a1@a.a'];
    System.debug('contact account '+sdfe.get(0).AccountId);
        
    List<Account> aade=[select id,Name from Account where id=:sdfe.get(0).AccountId];
    System.debug(aade.get(0).Name);        
    
    List<Contact> sdfet=[select id,Email from Contact where Contact.AccountId=:sdfe.get(0).AccountId];
    if(sdfet.size()>0)
    {
        System.debug('contact emailt '+sdfet.get(0).Email);
    }    
    else
    {
        System.debug('no rows');
    }
        
    Account a=[select id from Account where Account.Name='ABC'];
    delete a;
        
    }
}


From the testing result, we can see that the key function flow works, however, while trying to invoke the Delete Trigger

command, the system sent the incorrect result back.




trigger AfterDeleteAccount on Account (after delete) {   
    for(Account a:trigger.old)
    {                
        System.debug('--------------------------');
        System.debug('delete account id '+a.Id);
        System.debug('delete account name '+a.Name);        
        List<Contact> sdfe=[select id,AccountId from Contact where Contact.Email=:'a1@a.a'];
        System.debug('contact size='+sdfe.size());
        System.debug('--------------------------');            
    }
}


Why?
Display the results:



20090922051611.515:Class.Test.myUnitTest: line 9, column 15: SOQL query with 1 row finished in 5 ms
20090922051611.515:Class.Test.myUnitTest: line 13, column 30: SOQL query with 1 row finished in 3 ms
20090922051611.515:Class.Test.myUnitTest: line 26, column 22: SOQL query with 1 row finished in 6 ms
20090922051611.515:Class.Test.myUnitTest: line 27, column 3: contact account 0014000000Orxxxxxx
20090922051611.515:Class.Test.myUnitTest: line 29, column 22: SOQL query with 1 row finished in 3 ms
20090922051611.515:Class.Test.myUnitTest: line 30, column 3: ABC
20090922051611.515:Class.Test.myUnitTest: line 32, column 23: SOQL query with 1 row finished in 5 ms
20090922051611.515:Class.Test.myUnitTest: line 35, column 4: contact account a1@a.a
20090922051611.515:Class.Test.myUnitTest: line 42, column 13: SOQL query with 1 row finished in 7 ms
20090922051611.515:Class.Test.myUnitTest: line 43, column 3: Delete: SOBJECT:Account
*** Beginning AfterDeleteAccount on Account trigger event AfterDelete for 0014000000Orxxx



20090922051611.640:Trigger.AfterDeleteAccount: line 3, column 2: SelectLoop:LIST:SOBJECT:Account
20090922051611.640:Trigger.AfterDeleteAccount: line 5, column 3: --------------------------
20090922051611.640:Trigger.AfterDeleteAccount: line 6, column 3: delete account id 0014000000Orxxxxxx
20090922051611.640:Trigger.AfterDeleteAccount: line 7, column 3: delete account name ABC
20090922051611.640:Trigger.AfterDeleteAccount: line 8, column 22: SOQL query with 0 rows finished in 5 ms
20090922051611.640:Trigger.AfterDeleteAccount: line 10, column 3: contact size=0
20090922051611.640:Trigger.AfterDeleteAccount: line 11, column 3: --------------------------


Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 7 out of 100
Number of query rows: 6 out of 500
Number of SOSL queries: 0 out of 20
Number of DML statements: 1 out of 100
Number of DML rows: 1 out of 500
Number of script statements: 21 out of 200000
Maximum heap size: 0 out of 1000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 10
Number of System.runAs() invocations: 0 out of 20

Total email recipients queued to be sent : 0

*** Ending AfterDeleteAccount on Account trigger event AfterDelete for 0014000000Orxxx
Message Edited by netbrain on 09-21-2009 11:23 PM
Message Edited by netbrain on 09-21-2009 11:25 PM
ThomasTTThomasTT
I didn't test it by myself I tested it by myself, but isn't Contact cascadely deleted when Account is deleted? I'm sure that you can query the Contact if the trigger is "before delete".

ThomasTT
Message Edited by ThomasTT on 09-22-2009 09:48 AM
netbrainnetbrain

Thank you for your help!

works fine.