• Constance Rouge
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi All,

I am new to Apex triggers. Could you please help me with an issue I have with my test class?
I have a Master-Child relationship between Person account and a custom object called Staff_and_duty_travel__c (Person account being the master), but I am not always sure I'll have a value in the Associated_account__c field of Staff_and_duty_travel__c.
Therefore, I wrote an Apex Trigger before insert on this object.

The Trigger checks if there is a value in Associated_account__c.
If not, it uses the field Staff_email_address__c of the record to search in the emails fields of the Person Accounts (Customer_Email__pc and Secondary_Email__pc) if an account already exists with this email.
If yes, it gives the id of this account to the field Associated_account__c.
If not, it creates the account with the values contained in the record and it gives the id of this new account to the field Associated_account__c.

The trigger seems to work fine (I created several records to test every scenario), but my Test class fails.

Here is my trigger:
trigger SDT_Master_Child_Relation on Staff_and_duty_travel__c (before insert) {
    
    Set<String> staffEmailAdress = new Set <String> ();
    List<Account> accountsList = new List<Account> ();
    Id assocAccountId;
    RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account' Limit 1];

    
    //We create the list of all the email adresses from the bookings that are inserted
    for (Staff_and_duty_travel__c Travel : Trigger.New)
    {
        staffEmailAdress.add(Travel.Staff_email_address__c);
    }
    
    //We create the list of all the accounts containing one of the emails from the bookings that are inserted
    for(Account acc :[select Id, Customer_Email__pc, Secondary_Email__pc from Account where Customer_Email__pc IN:staffEmailAdress OR Secondary_Email__pc IN:staffEmailAdress])
    {
               accountsList.add(acc);
    }
        
   
    //For every booking that is inserted, we associate an account.
    for (Staff_and_duty_travel__c Travel : Trigger.New)
    {
        assocAccountId=Null;
        
        //We check if an account is already associated
        assocAccountId=Travel.Associated_account__c;
        
        //If not, we associate one.
        If (assocAccountId==Null){
            
            //We check if an account with the Staff email exists
            for (Account acc2 : accountsList){
                if(acc2.Customer_Email__pc.equals(Travel.Staff_email_address__c) || acc2.Secondary_Email__pc.equals(Travel.Staff_email_address__c)) {
                    assocAccountId=acc2.Id;
                }
            }
            //If not, we create thew account
            if (assocAccountId==Null){
                //Create the account
                Account newPersonAccount = new Account();
                newPersonAccount.FirstName = Travel.Staff_First_Name__c;
                newPersonAccount.LastName = Travel.Staff_Last_name__c;
                newPersonAccount.Customer_Email__pc = Travel.Staff_email_address__c;
                newPersonAccount.PersonHomePhone=Travel.Staff_phone_number__c;
                newPersonAccount.RecordTypeId = personAccountRecordType.Id;
                insert newPersonAccount;
                assocAccountId=newPersonAccount.Id;
            }
        }
        //We associate the account
        Travel.Associated_account__c=assocAccountId;
    }
    
}
Here is my Test class (I have put in bold the assertions that fail):
 
@isTest

public class SDT_Master_Child_Relation_Test {
    
    
    static testMethod void testAssociateAccount(){
        RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account' Limit 1];
        
        //Create a test account
        Account accTest=new Account (RecordTypeId = personAccountRecordType.Id,
                                     FirstName='Test',
                                     LastName='TestAccountTrigger', 
                                     Customer_Email__pc='testFirstEmail@trigger.com',
                                     Secondary_Email__pc='testSecondEmail@trigger.com');
        insert accTest;
        
        //Create four travels to test the four possibilities
        Staff_and_duty_travel__c travelTest1=new Staff_and_duty_travel__c (Staff_Last_name__c='AccountAlreadyAssociated', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testNoEmail@trigger.com',
                                                                           Associated_account__c=accTest.Id);
        Staff_and_duty_travel__c travelTest2=new Staff_and_duty_travel__c (Staff_Last_name__c='FirstEmail', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testFirstEmail@trigger.com');
        Staff_and_duty_travel__c travelTest3=new Staff_and_duty_travel__c (Staff_Last_name__c='SecondEmail', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testSecondEmail@trigger.com');
        Staff_and_duty_travel__c travelTest4=new Staff_and_duty_travel__c (Staff_Last_name__c='ThirdEmail', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testThirdEmail@trigger.com');
        
        List<Staff_and_duty_travel__c> travels = new List<Staff_and_duty_travel__c>{travelTest1, travelTest2, travelTest3, travelTest4};
        
        //Run the test
        Test.startTest();
        insert travels;
        Test.stopTest();

        //We check for each of the travels if the account has been associated
        //For travelTest1:
        System.assert(travelTest1.Associated_account__c==accTest.Id);
        
        //For travelTest2:
        System.assert(travelTest2.Associated_account__c==accTest.Id);
        
        //For travelTest3:
        System.assert(travelTest3.Associated_account__c==accTest.Id);
        
        //For travelTest4, we have to check if an account has been created with the right values, then if it has been associated to the travel:
        Account accTest4=[select Id, FirstName,LastName,PersonHomePhone
                         from Account
                         where RecordTypeId = :personAccountRecordType.Id and Customer_Email__pc=:travelTest4.Staff_email_address__c ];
        System.assert(accTest4.FirstName==travelTest4.Staff_First_Name__c);
        System.assert(accTest4.LastName==travelTest4.Staff_Last_name__c);
        System.assert(accTest4.PersonHomePhone==travelTest4.Staff_phone_number__c);
        System.assert(travelTest4.Associated_account__c==accTest4.Id);   
    }
}

I get the "System.AssertException: Assertion Failed" error for the assertion I have put in bold.
I don't understand where is the problem coming from, so any help is appreciated.
Regards,
Constance Rouge
 
Hello,

I am creating a web-to-case form containing several picklist which have a lot of different possible values and are dependant from each others.
I have therefore two questions:

1. Is it possible to update automatically the values available in the picklist? Meaning that if I modify the values in Salesforce, it will be modified automatically in the web form to? Maybe by autopopulating the picklist each time it opens instead of having its values written in the code?

2. Is it possible to put the dependencie rules of the picklists in the form without needing to write them in the code? And to update them when they are modified on Salesforce?

Any suggestions?

Thanks,
Constance
Hi All,

I am new to Apex triggers. Could you please help me with an issue I have with my test class?
I have a Master-Child relationship between Person account and a custom object called Staff_and_duty_travel__c (Person account being the master), but I am not always sure I'll have a value in the Associated_account__c field of Staff_and_duty_travel__c.
Therefore, I wrote an Apex Trigger before insert on this object.

The Trigger checks if there is a value in Associated_account__c.
If not, it uses the field Staff_email_address__c of the record to search in the emails fields of the Person Accounts (Customer_Email__pc and Secondary_Email__pc) if an account already exists with this email.
If yes, it gives the id of this account to the field Associated_account__c.
If not, it creates the account with the values contained in the record and it gives the id of this new account to the field Associated_account__c.

The trigger seems to work fine (I created several records to test every scenario), but my Test class fails.

Here is my trigger:
trigger SDT_Master_Child_Relation on Staff_and_duty_travel__c (before insert) {
    
    Set<String> staffEmailAdress = new Set <String> ();
    List<Account> accountsList = new List<Account> ();
    Id assocAccountId;
    RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account' Limit 1];

    
    //We create the list of all the email adresses from the bookings that are inserted
    for (Staff_and_duty_travel__c Travel : Trigger.New)
    {
        staffEmailAdress.add(Travel.Staff_email_address__c);
    }
    
    //We create the list of all the accounts containing one of the emails from the bookings that are inserted
    for(Account acc :[select Id, Customer_Email__pc, Secondary_Email__pc from Account where Customer_Email__pc IN:staffEmailAdress OR Secondary_Email__pc IN:staffEmailAdress])
    {
               accountsList.add(acc);
    }
        
   
    //For every booking that is inserted, we associate an account.
    for (Staff_and_duty_travel__c Travel : Trigger.New)
    {
        assocAccountId=Null;
        
        //We check if an account is already associated
        assocAccountId=Travel.Associated_account__c;
        
        //If not, we associate one.
        If (assocAccountId==Null){
            
            //We check if an account with the Staff email exists
            for (Account acc2 : accountsList){
                if(acc2.Customer_Email__pc.equals(Travel.Staff_email_address__c) || acc2.Secondary_Email__pc.equals(Travel.Staff_email_address__c)) {
                    assocAccountId=acc2.Id;
                }
            }
            //If not, we create thew account
            if (assocAccountId==Null){
                //Create the account
                Account newPersonAccount = new Account();
                newPersonAccount.FirstName = Travel.Staff_First_Name__c;
                newPersonAccount.LastName = Travel.Staff_Last_name__c;
                newPersonAccount.Customer_Email__pc = Travel.Staff_email_address__c;
                newPersonAccount.PersonHomePhone=Travel.Staff_phone_number__c;
                newPersonAccount.RecordTypeId = personAccountRecordType.Id;
                insert newPersonAccount;
                assocAccountId=newPersonAccount.Id;
            }
        }
        //We associate the account
        Travel.Associated_account__c=assocAccountId;
    }
    
}
Here is my Test class (I have put in bold the assertions that fail):
 
@isTest

public class SDT_Master_Child_Relation_Test {
    
    
    static testMethod void testAssociateAccount(){
        RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account' Limit 1];
        
        //Create a test account
        Account accTest=new Account (RecordTypeId = personAccountRecordType.Id,
                                     FirstName='Test',
                                     LastName='TestAccountTrigger', 
                                     Customer_Email__pc='testFirstEmail@trigger.com',
                                     Secondary_Email__pc='testSecondEmail@trigger.com');
        insert accTest;
        
        //Create four travels to test the four possibilities
        Staff_and_duty_travel__c travelTest1=new Staff_and_duty_travel__c (Staff_Last_name__c='AccountAlreadyAssociated', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testNoEmail@trigger.com',
                                                                           Associated_account__c=accTest.Id);
        Staff_and_duty_travel__c travelTest2=new Staff_and_duty_travel__c (Staff_Last_name__c='FirstEmail', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testFirstEmail@trigger.com');
        Staff_and_duty_travel__c travelTest3=new Staff_and_duty_travel__c (Staff_Last_name__c='SecondEmail', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testSecondEmail@trigger.com');
        Staff_and_duty_travel__c travelTest4=new Staff_and_duty_travel__c (Staff_Last_name__c='ThirdEmail', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testThirdEmail@trigger.com');
        
        List<Staff_and_duty_travel__c> travels = new List<Staff_and_duty_travel__c>{travelTest1, travelTest2, travelTest3, travelTest4};
        
        //Run the test
        Test.startTest();
        insert travels;
        Test.stopTest();

        //We check for each of the travels if the account has been associated
        //For travelTest1:
        System.assert(travelTest1.Associated_account__c==accTest.Id);
        
        //For travelTest2:
        System.assert(travelTest2.Associated_account__c==accTest.Id);
        
        //For travelTest3:
        System.assert(travelTest3.Associated_account__c==accTest.Id);
        
        //For travelTest4, we have to check if an account has been created with the right values, then if it has been associated to the travel:
        Account accTest4=[select Id, FirstName,LastName,PersonHomePhone
                         from Account
                         where RecordTypeId = :personAccountRecordType.Id and Customer_Email__pc=:travelTest4.Staff_email_address__c ];
        System.assert(accTest4.FirstName==travelTest4.Staff_First_Name__c);
        System.assert(accTest4.LastName==travelTest4.Staff_Last_name__c);
        System.assert(accTest4.PersonHomePhone==travelTest4.Staff_phone_number__c);
        System.assert(travelTest4.Associated_account__c==accTest4.Id);   
    }
}

I get the "System.AssertException: Assertion Failed" error for the assertion I have put in bold.
I don't understand where is the problem coming from, so any help is appreciated.
Regards,
Constance Rouge
 
Hello,

I am creating a web-to-case form containing several picklist which have a lot of different possible values and are dependant from each others.
I have therefore two questions:

1. Is it possible to update automatically the values available in the picklist? Meaning that if I modify the values in Salesforce, it will be modified automatically in the web form to? Maybe by autopopulating the picklist each time it opens instead of having its values written in the code?

2. Is it possible to put the dependencie rules of the picklists in the form without needing to write them in the code? And to update them when they are modified on Salesforce?

Any suggestions?

Thanks,
Constance