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
Jerfy Vermeulen 8Jerfy Vermeulen 8 

Can you help me with a test class?

Hello, 

I'm getting stuck when I want to write a test class for this class. Please can you help me out? Thanks in advance.

public class UpsertCollectantenV2 {

    public String strNote { get; set; }
    public String strInitials { get; set; }
    public String strSalutation { get; set; }
    public String strMobiel { get; set; }
    public String strPlace { get; set; }
    public String strSuffix { get; set; }
    public decimal strHousenumber { get; set; }
    public String strPostalStreet { get; set; }
    public String strPostalCode { get; set; }
    public Date strDateOfBirth { get; set; }
    public String strPhone { get; set; }
    public String strEmail { get; set; }
    public String strLastname { get; set; }
    public String strFirstname { get; set; }
    public String description { get; set; }
    
    public PageReference save() {

        String strSleutelAccountKlein = 'H-' +
            strLastname.left(3) +  '-' +
            strPostalCode.left(4) + strPostalCode.right(2) +  '-' +
            strHousenumber + strSuffix; 
        
        String strSleutelAccount = strSleutelAccountKlein.ToUpperCase();
    
        String strSleutelContactKlein = 'P-' +
            strSalutation.replaceAll('De heer', 'M').replaceAll('Mevrouw', 'V') + '-' +
            strFirstname.left(1) + '-' +
            strLastname.left(3) +  '-' +
            strPostalCode.left(4) + strPostalCode.right(2) +  '-' +
            strHousenumber + strSuffix; 
        
        String strSleutelContact = strSleutelContactKlein.ToUpperCase();  
        
        //Check of account bestaat  
        Account[] oldAccounts = [SELECT Account.Id 
                              FROM Account 
                              WHERE NAW_Sleutel__c = :strSleutelAccount
                              LIMIT 1];
        
        // loop through them and update the Description field
        for (account acct : oldAccounts) {
          acct.Description = description;
        }
                
        
            if(oldAccounts.size() >0) {
            
                // Check of contact bestaat
                Contact[] oldContacts = [SELECT Account.Id 
                                      FROM Contact 
                                      WHERE NAW_Sleutel__c = :strSleutelContact
                                      LIMIT 1];
                
                // loop through them and update the Description field
                for (contact cont : oldContacts) {
                  cont.Description = description;
                }                
                    
                if(oldContacts.size() >0) {
                    
                    // Bestaand contact ophalen
                    Contact queriedContact = [SELECT Account.Id 
                                          FROM Contact 
                                          WHERE NAW_Sleutel__c = :strSleutelContact
                                          LIMIT 1];
                                                                            
                    // Update the contact's phone number
                    queriedContact.Salutation = strSalutation;    
                    queriedContact.bf_Voorletters__c = strInitials;
                    queriedContact.FirstName = strFirstName;    
                    queriedContact.Phone = strPhone;   
                    queriedContact.Email = strEmail;
                    queriedContact.Birthdate= strDateOfBirth;
                    queriedContact.MobilePhone = strMobiel;
                    queriedContact.Notities_ORCA__c = strNote;
                           
                    // Update the related account fields
                    queriedContact.Account.soco__Billing_Zipcode__c = strPostalCode;
                    queriedContact.Account.soco__Billing_Street__c = strPostalStreet;
                    queriedContact.Account.soco__Billing_Housenumber__c = strHousenumber;     
                    queriedContact.Account.soco__Billing_Suffix__c = strSuffix;
                    queriedContact.Account.soco__Billing_City__c = strPlace;
                            
                    // Make two separate calls 
                    // 1. This call is to update the contact's phone.
                    update queriedContact;
                    // 2. This call is to update the related account fields.
                    update queriedContact.Account;
                }    
                else {
            
                    //Bestaand account ophalen
                    Account queriedAccount = [SELECT Account.Id 
                                              FROM Account 
                                              WHERE NAW_Sleutel__c = :strSleutelAccount
                                              LIMIT 1];
                                              
                        // Voeg contact toe aan account
                        Contact con = new Contact(
                            Salutation= strSalutation,
                            bf_Voorletters__c = strInitials,
                            FirstName= strFirstName,
                            LastName= strLastname,
                            Phone= strPhone,
                            MobilePhone = strMobiel,
                            Email = strEmail,
                            Birthdate= strDateOfBirth,
                            Notities_ORCA__c = strNote,
                            AccountId=queriedAccount.ID);
                        insert con;
                    
                        ID conID = con.ID;
                
                        // Contact opvoeren als campagnelid
                        CampaignMember mem = new CampaignMember(
                            CampaignId = '7010Y000000I783', //Website BWS collectevrijwilliger
                            ContactId = conID);
                        // Insert the campaign member by using DML
                        insert mem;
                
                }
            }             
            else
            {
                // Nieuw account opvoeren
                Account acct = new Account(
                    Name= strLastName, 
                    soco__Billing_Street__c = strPostalStreet, 
                    soco__Billing_Zipcode__c = strPostalCode, 
                    soco__Billing_Housenumber__c = strHousenumber,     
                    soco__Billing_Suffix__c = strSuffix,
                    soco__Billing_City__c = strPlace,   
                    RecordTypeId = '0120Y000000k85mQAA');     
                // Insert the account by using DML
                insert acct;
                
                ID acctID = acct.ID;
            
                // Nieuw contact toevoegen aan account
                Contact con = new Contact(
                    Salutation = strSalutation,
                    bf_Voorletters__c = strInitials,
                    FirstName = strFirstName,
                    LastName = strLastName,
                    Phone = strPhone,
                    MobilePhone = strMobiel,
                    Email = strEmail,
                    Birthdate = strDateOfBirth,
                    Notities_ORCA__c = strNote,
                    AccountId = acctID);
                // Insert the contact by using DML
                insert con;
            
                ID conID = con.ID;
            
                // Contact opvoeren als campagnelid
                CampaignMember mem = new CampaignMember(
                    CampaignId = '7010Y000000I783', //Website BWS collectevrijwilliger
                    ContactId = conID);
                // Insert the campaign member by using DML
                insert mem;
            
            }
            
        Pagereference pr = New PageReference('/testsite');
        return pr;
        //return null;            
        
    }
}
Best Answer chosen by Jerfy Vermeulen 8
Raj VakatiRaj Vakati
try this
 
@isTest 
private class UpsertCollectantenV2Test {
static testMethod void validateSave() {

Account acc = new Account(name='Test Account',NAW_Sleutel__c ='Demo');
insert acc;


Contact c=new Contact(
FirstName='fname',
LastName = 'lname',NAW_Sleutel__c ='Demo',
Email = 'email@gmail.com',
Phone = '9743800309'); 
insert c; 

UpsertCollectantenV2  css= new UpsertCollectantenV2 () ;
css.strNote='Demo';
css.strInitials='D';
css.strSalutation='MR';
css.strMobiel='123123'
css.strPlace='USA';
css.strSuffix='MR';
css.strHousenumber=12323;
css.strPostalStreet='SDrive';
css.strPostalCode='78717';
css.strDateOfBirth=System.today(); 
css.strPhone='234234';
css.strEmail='234234@sdkfj.com';
css.strLastname='asdkasd';
css.strFirstname='asdasd';
css.description='asdas asdadsasd ';

css.save() ; 


}
}

 

All Answers

Raj VakatiRaj Vakati
try this
 
@isTest 
private class UpsertCollectantenV2Test {
static testMethod void validateSave() {

Account acc = new Account(name='Test Account',NAW_Sleutel__c ='Demo');
insert acc;


Contact c=new Contact(
FirstName='fname',
LastName = 'lname',NAW_Sleutel__c ='Demo',
Email = 'email@gmail.com',
Phone = '9743800309'); 
insert c; 

UpsertCollectantenV2  css= new UpsertCollectantenV2 () ;
css.strNote='Demo';
css.strInitials='D';
css.strSalutation='MR';
css.strMobiel='123123'
css.strPlace='USA';
css.strSuffix='MR';
css.strHousenumber=12323;
css.strPostalStreet='SDrive';
css.strPostalCode='78717';
css.strDateOfBirth=System.today(); 
css.strPhone='234234';
css.strEmail='234234@sdkfj.com';
css.strLastname='asdkasd';
css.strFirstname='asdasd';
css.description='asdas asdadsasd ';

css.save() ; 


}
}

 
This was selected as the best answer
Jerfy Vermeulen 8Jerfy Vermeulen 8
Thanks a lot! You saved my life.