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
KyoKyo 

Problem Test Class Relationship

public class RelationshipMgr {


    public static void createRelationship(Relationship__c relArgs) {
    
        //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + relArgs.RecordType);
        
        // First ,get corresponding "Relationship Type" from the given "Relationship Category", we
        // need to query from "Relationship Category" obj, "relArgs.Relationship_Category__c"
        // is a Lookup, thus, it contains system ID (Id field) of the "Relationship_Category" obj
        // not the "Name" field...
        List<Relationship_Category__c> relCats = [select Relationship_Type__c, Reverse_Relationship_Category__c, Name, Reversible__c from Relationship_Category__c where Id =: relArgs.Relationship_Category__c];
  
        // TEMP CODE: to be removed or cleaned...      
        if (relCats.IsEmpty()) {
            System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: IsEmpty');    
        }
        
        Relationship_Category__c relCatTemp = relCats.get(0);
 
        
        //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + relCatTemp.Relationship_Type__c);
 
        
        // At the highest level, we group our business rules by "Relationship Type", inside each
        // top-level if/else clause implements BRs specific to each "Relationship Type"...        

        if (relCatTemp.Relationship_Type__c == 'Account to Account') {

            // "Account to Account", all business rules for this case here...
        
            // create second paried (reverse) record...    
            Relationship__c relReverse = new Relationship__c();
            relReverse.Relationship_Category__c = relCatTemp.Reverse_Relationship_Category__c;
            relReverse.Account_ID__c = relArgs.Rev_Account_ID__c;
            relReverse.Rev_Account_ID__c = relArgs.Account_ID__c;
            relReverse.IsTrigger__c = '1';
            
            //insert relReverse;
            
      
            // since, we only submit one obj, therefore, only one obj returned in SaveResult[] array...
            Relationship__c[] relTemps = new Relationship__c[1];
            relTemps[0] = relReverse;
            Database.SaveResult[] result = Database.Insert(relTemps);
            
            //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + relTemps[0].Id);
            //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + result[0].Id);
            
            // now, we need to update the parent "Relationship" record (relArgs) with the new "Id" of the
            // reversed record just inserted...
            Relationship__c relArgsCloned = relArgs.clone(true, true);
            relArgsCloned.Rev_Relationship__c =  result[0].Id;
            update relArgsCloned;            
        
        } else if (relCatTemp.Relationship_Type__c == 'Account to Contact') {

            // "Account to Contact", all business rules for this case here...
     
            // First, we need to check of the underlying Contact record has a value in the "Account Name"
            // field, if NOT, then we need to populate it...
            List<Contact> contacts = [select AccountId from Contact where Id =: relArgs.Contact_ID__c];
            
            if (contacts.IsEmpty()) {
                System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: contacts IsEmpty');    
            }
        
            Contact contactTemp = contacts.get(0);
            
            //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + contactTemp.AccountId);
            
            if (contactTemp.AccountId == null) {
            
                List<Account> accounts = [select Id from Account where Id =: relArgs.Account_ID__c];
            
                if (accounts.IsEmpty()) {
                    System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: accounts IsEmpty');    
                }
            
                Account accountTemp = accounts.get(0);
                contactTemp.AccountId = accountTemp.Id;
                
                update contactTemp;
            }

            Relationship__c relReverse = new Relationship__c();
            
            // Next, we need to check whether the "Relationship" is created from Account or Contact page,
            // the ID assignment rule differs according to the source page...
            if (relArgs.Account_ID__c != null) {
                // "Relationship" is created from "Account" page (Account ID available)...
                relReverse.Contact_ID__c = relArgs.Rev_Contact_ID__c;
                relReverse.Rev_Account_ID__c = relArgs.Account_ID__c; 
                     
            }
            else if (relArgs.Contact_ID__c != null) {
                // "Relationship" is created from "Contact" page (Account ID available)...
                relReverse.Rev_Contact_ID__c = relArgs.Contact_ID__c;
                relReverse.Account_ID__c = relArgs.Rev_Account_ID__c; 

            }
            else {
                // invalid case, not supposed to happen, thus raise the exception...
                System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: either Account ID or Contact ID must exist...');    
            
            }
            
           
            // Now, create second paired (reverse) record...
            relReverse.Relationship_Category__c = relCatTemp.Reverse_Relationship_Category__c;
            relReverse.IsTrigger__c = '1';
            insert relReverse;

        } else if (relCatTemp.Relationship_Type__c == 'Contact to Contact') {

            // "Contact to Contact", all business rules for this case here...

            // create second paired (reverse) record...
            Relationship__c relReverse = new Relationship__c();
            
            
            // First, we need to check if the parent Contact has Account or not (AccountId field null),
            // if NO, then we need to auto link with the same Account as the reversed Contact...
            List<Contact> contacts = [select AccountId from Contact where Id =: relArgs.Contact_ID__c];
            
            if (contacts.IsEmpty()) {
                System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: contacts IsEmpty');    
            }
        
            Contact contactParent = contacts.get(0);
         
            if (contactParent.AccountId == null) {
                // the parent Contact does NOT have Account, we must copy the AccountId from the reversed Contact
                // into this parent Contact...            
                contacts = [select AccountId from Contact where Id =: relArgs.Rev_Contact_ID__c];
            
                if (contacts.IsEmpty()) {
                    System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: contacts IsEmpty');    
                }
            
                Contact contactTempReverse = contacts.get(0);
                contactParent.AccountId = contactTempReverse.AccountId;                
 
                update contactParent;
            }


            // Next, we need to check against "Reversible" flag (checkbox), normal cases are NOT reversible, if it's
            // reversible, then it's the case like "Is Married To" (we follow screen here, which a little conflict
            // with our code namving convention)...
            if (!relCatTemp.Reversible__c) {
            
                // Next, we need to check if this is "Has Child" relationship category, if yes,
                // then we need to figure out the parent Contact record whether it's "Male" or
                // "Female" via the "Sex" field, note that, the assumption here is that the "Sex"
                // field is always has value (required field in Contact object)...
                
                // note, we need to use "Relationship Category" obj, not "Relationship", because the "Relationship"
                // obj contains the system Id not actual text...
                if (relCatTemp.Name == 'Has Child') {
                
                    contacts = [select Sex__c from Contact where Id =: relArgs.Contact_ID__c];
                
                    if (contacts.IsEmpty()) {
                        System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: contacts IsEmpty');    
                    }
                
                    Contact contactTemp = contacts.get(0);
    
        
                    //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + contactTemp.Sex__c);
    
                    // we need to get the system "Id", because "Relationship" obj uses "Lookup" for the "Relationship Category"
                    // field...
                    List<Relationship_Category__c> relCatTemps = [select Id, Name from Relationship_Category__c where Name='Has Father' or Name='Has Mother'];
    
                    if (relCatTemps .IsEmpty()) {
                        System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: Relationship Category IsEmpty');    
                    }
                
                    String strRelCatIdFather = '';
                    String strRelCatIdMother = '';
                
                    for (Relationship_Category__c relCatTemp2 : relCatTemps) {
                        
                        if (relCatTemp2.Name == 'Has Father')
                            strRelCatIdFather = relCatTemp2.Id;
                        else if (relCatTemp2.Name == 'Has Mother')
                            strRelCatIdMother = relCatTemp2.Id;    
                    }
       
                    // now, assign corresponding "Relationship Category to the reverse "Relationship" obj...
                    if (contactTemp.Sex__c == 'Male')               
                        relReverse.Relationship_Category__c  = strRelCatIdFather; //'Has Father';
                    else if (contactTemp.Sex__c == 'Femail')
                        relReverse.Relationship_Category__c  = strRelCatIdMother; //'Has Mother';                   
                    else
                        relReverse.Relationship_Category__c  = strRelCatIdMother; //'default to Has Mother';
    
                }
                else {
                
                    relReverse.Relationship_Category__c = relCatTemp.Reverse_Relationship_Category__c;
    
                }
                
            }
            else {
            
                // REVERSIBLE: this is for case like "Is Married To" when the "Reversible" field
                // is checked...
                relReverse.Relationship_Category__c = relArgs.Relationship_Category__c;
            
            }
 
            relReverse.Contact_ID__c = relArgs.Rev_Contact_ID__c;
            relReverse.Rev_Contact_ID__c = relArgs.Contact_ID__c;
            relReverse.IsTrigger__c = '1';            
            insert relReverse;

        }

    
    } // end method...

    
    // This method implements logical delete (Mark Delete) for the Relationship record. It's required
    // "Mark Delete" because of trigger limitation, which does NOT allow delete of the same record type
    // in the "before and after" event via the "delete sObject[]" operation.
    public static void markDeleteRelationship(Relationship__c relArgs) {
        
        //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + relArgs.Rev_Relationship__c + ' - ' + relArgs.Id);       

        // NOTE - 140611: WE NO LONGER NEED THIS FLAGMENT BECAUSE WE USE THE STANDARD DELETE FUNCTION TO PHYSICALLY
        // DELETE PARENT RECORD!!!
        // first, update the parent record as deleted...
        //Relationship__c relArgsCloned = relArgs.clone(true, true);
        //relArgsCloned.Mark_for_Delete__c = true;
        //update relArgsCloned;
       
        // now, we need update the paired record as deleted (Mark for Delete = true), the Apex scheduler will
        // perform the physical delete at scheduled interval...
        Relationship__c relTemp = new Relationship__c(Id=relArgs.Rev_Relationship__c);
        relTemp.Mark_for_Delete__c = true;
        update relTemp;             
    
    } // end method...


} // end class...

 

@isTest 
private class TestRelationshipMgr {
    
    static testMethod void myTest() {
  
      Account objAcc = new Account();
      objAcc.Name = 'test';
      insert objAcc;
      
      Contact objcon = new Contact();
      objcon.LastName = 'test';
      objcon.AccountID = objAcc.ID;
      objcon.Sex__c = 'Male';
    
      insert objcon;
      
      Relationship_Category__c RC = new Relationship_Category__c();
      RC.Name = 'Test';
      RC.Relationship_Type__c = 'Account to Account';
      RC.Reverse_Relationship_Category__c = RC.id;
      RC.Reversible__c = true;
      insert RC;
      
      Relationship__c objRe = new Relationship__c ();
      objRe.Account_ID__c = objAcc.id;
      //objRe.Contact_ID__c = objCon.id;
      objRe.Rev_Account_ID__c = objAcc.id;
      //objRe.Rev_Contact_ID__c = objCon.id;
      objRe.IsTrigger__c = '1';
      objRe.Relationship_Category__c = RC.id;
      objRe.Mark_for_Delete__c  = true;
      
      test.starttest();
      insert objRe;
      test.stoptest();
     
    }
    
    static testMethod void myTest1() {
  
      Account objAcc = new Account();
      objAcc.Name = 'test';
      insert objAcc;
      
      Contact objcon = new Contact();
      objcon.LastName = 'test';
      objcon.AccountID = objAcc.ID;
      objcon.Sex__c = 'Male';
    
      insert objcon;
      
      Relationship_Category__c RC = new Relationship_Category__c();
      RC.Name = 'Test';
      RC.Relationship_Type__c = 'Account to Contact';
      RC.Reverse_Relationship_Category__c = RC.id;
      RC.Reversible__c = true;
      insert RC;
      
      Relationship__c objRe = new Relationship__c ();
      objRe.Account_ID__c = objAcc.id;
      objRe.Contact_ID__c = objCon.id;
     // objRe.Rev_Account_ID__c = objAcc.id;
      //objRe.Rev_Contact_ID__c = objCon.id;
      objRe.IsTrigger__c = '1';
      objRe.Relationship_Category__c = RC.id;
      objRe.Mark_for_Delete__c  = true;
      
      test.starttest();
      insert objRe;
      test.stoptest();
     
    }
     static testMethod void myTest2() {
  
      Account objAcc = new Account();
      objAcc.Name = 'test';
      insert objAcc;
      
      Contact objcon = new Contact();
      objcon.LastName = 'test';
      objcon.AccountID = objAcc.ID;
      objcon.Sex__c = 'Male';
    
      insert objcon;
      
      Relationship_Category__c RC = new Relationship_Category__c();
      RC.Name = 'Test';
      RC.Relationship_Type__c = 'Contact to Contact';
      RC.Reverse_Relationship_Category__c = RC.id;
      RC.Reversible__c = true;
      insert RC;
      
      Relationship__c objRe = new Relationship__c ();
      //objRe.Account_ID__c = objAcc.id;
      objRe.Contact_ID__c = objCon.id;
     // objRe.Rev_Account_ID__c = objAcc.id;
      objRe.Rev_Contact_ID__c = objCon.id;
      objRe.IsTrigger__c = '1';
      objRe.Relationship_Category__c = RC.id;
      objRe.Mark_for_Delete__c  = true;
      
      test.starttest();
      insert objRe;
      test.stoptest();
     
    }
    
}

 

Code Coverage Low Please Help me!

Thank you so much.

ian b goodeian b goode

I'm sure you have worked this out by now...... but actually calling the method RelationshipMgr.createRelationship(someArg);

would no doubt increase coverage!!