• AmberTaylor
  • NEWBIE
  • 30 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies
Hi there, 

I would like to have some advices regarding technical feasibility  of a need and its various possibilities. 

Currently, we have a multi-select list that has no dependency and no limit with 7 items in it.

Now we want to be able to choose items and also say the quantity for each items selected.
User-added image

Creating 7 other number type fields won't be a best practice and will for sure burden the page layout. Plus it will probably be confusing for users. 

I'm now thinking of replacing this multi-select list by a new custom object with the same name : 
 - Record Name (standard field) with type auto-number
 - A picklist field called "Type of animal" with the same values as the previous multi-select list
 - A number field  "Number of animals" that will allows us to fill the quantity 

In addition to this, in the related list of Leads, i'll only show 2 fields Type of  animal & Number of animals. It will still be editable if needed or also deleted if it's no longer the case for the selected lead. 

This method will allow us to keep a light layout on lead and also do the required reporting when needed. 

I would like to know : 
1- Is there a better way to achieve this without adding a new custom object ? 
2- Is this a best practice, considering creating tiny object for one purpose ?
3- I also need to Consider the fact that i'll have to update all existing records that have existing values in this multiselect list to match this evolution and of course number field will have a null value. 

Here is the related list on lead with the new object :
User-added image
Hi there, 

I'm quite stuck here. 
I've written a test class for my batch but it doesn't appears to be running. It stays at 0% :( 

I'm missing something but don't know what. 

Can someone help me figure it out? 

Here is my batch class : 
global class BatchUpdateAllLeads implements Database.Batchable <SObject> {
//START METHOD
    global Database.QueryLocator start(Database.BatchableContext bc){
        String Query='Select id,TECH_Update__c from Lead where TECH_Update__c=false AND Converted=false ' ;
        return Database.getQueryLocator(Query);
            }
//EXECUTE METHOD
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(Lead l: scope){
            l.TECH_Update__c = true;
        }
        update scope;
    }
//FINISH METHOD
    global void finish(Database.BatchableContext bc){
        Id job= bc.getJobId();
        System.debug(job);
    }
}

And this is my attemp of writting a proper test class :( 
@istest
private class BatchUpdateAllLeads_TEST {
    @istest
    static void testAcc(){
        List<Lead> l = new List<Lead>();
        Lead l1 = new Lead(Company	='BTP',LastName = 'Test',SIRET__c='12659991955626',Numero_d_ordre__c='9999-99-99',Region_GRDF__c='Ile de France',Tech_Update__c=false,IsConverted=false);
        l.add(l1);

        Lead l2= new Lead(Company	='Comp',LastName = 'Test2',SIRET__c='12659991955687',Numero_d_ordre__c='1111-99-99',Region_GRDF__c='Ile de France',Tech_Update__c=false, IsConverted=true);
        l.add(l2);

        Lead l3= new Lead(Company	='Agny', LastName = 'Test3', SIRET__c='19959991988687', Numero_d_ordre__c='1111-99-11', Region_GRDF__c='Ile de France', Tech_Update__c=true,IsConverted=true);
        l.add(l3);
        insert l;
   
    Test.startTest();
    BatchUpdateAllLeads ap= new BatchUpdateAllLeads();
    Id jobid= Database.executeBatch(ap);
    Test.stopTest();
    }
}

Ty very much for your help
Hi there, I have written a Test Class for a Switch condition but When I'm executing Tests, I still have a 68% Coverage, nothing more. 

Here is my main class wich is tested until case : 33 
public with sharing class UpdateDepartmentField_CLASS {
    public static void UpdateDepartmentFieldCommune(List<Commune__c> communeList) {

        for (Integer i=0;  i < communeList.size(); i++) {
                switch on communeList[i].CodeDepartement__c {
                    when '01' {
                        communeList[i].Departement__c = '01 AIN';
                    }
                    when '02' {
                        communeList[i].Departement__c = '02 AISNE';
                    }
                    when '03' {
                        communeList[i].Departement__c = '03 ALLIER';
                    }
                    when '04' {
                        communeList[i].Departement__c = '04 ALPES-DE-HAUTE-PROVENCE';
                    }
                    when '05' {
                        communeList[i].Departement__c = '05 HAUTES-ALPES';
                    }
                    when '06' {
                        communeList[i].Departement__c = '06 ALPES-MARITIMES';
                    }
                    when '07' {
                        communeList[i].Departement__c = '07 ARDECHE';
                    }
                    when '08' {
                        communeList[i].Departement__c = '08 ARDENNES';
                    }
                    when '09' {
                        communeList[i].Departement__c = '09 ARIEGE';
                    }
                    when '10' {
                        communeList[i].Departement__c = '10 AUBE';
                    }
                    when '11' {
                        communeList[i].Departement__c = '11 AUDE';
                    }
                    when '12' {
                        communeList[i].Departement__c = '12 AVEYRON';
                    }
                    when '13' {
                        communeList[i].Departement__c = '13 BOUCHES-DU-RHONE';
                    }
                    when '14' {
                        communeList[i].Departement__c = '14 CALVADOS';
                    }
                    when '15' {
                        communeList[i].Departement__c = '15 CANTAL';
                    }
                    when '16' {
                        communeList[i].Departement__c = '16 CHARENTE';
                    }
                    when '17' {
                        communeList[i].Departement__c = '17 CHARENTE-MARITIME';
                    }
                    when '18' {
                        communeList[i].Departement__c = '18 CHER';
                    }
                    when '19' {
                        communeList[i].Departement__c = '19 CORREZE';
                    }
                    when '21' {
                        communeList[i].Departement__c = '21 COTE-D\'OR';
                    }
                    when '22' {
                        communeList[i].Departement__c = '22 COTES-D\'ARMOR';
                    }
                    when '23' {
                        communeList[i].Departement__c = '23 CREUSE';
                    }
                    when '24' {
                        communeList[i].Departement__c = '24 DORDOGNE';
                    }
                    when '25' {
                        communeList[i].Departement__c = '25 DOUBS';
                    }
                    when '26' {
                        communeList[i].Departement__c = '26 DROME';
                    }
                    when '27' {
                        communeList[i].Departement__c = '27 EURE';
                    }
                    when '28' {
                        communeList[i].Departement__c = '28 EURE-ET-LOIR';
                    }
                    when '29' {
                        communeList[i].Departement__c = '29 FINISTERE';
                    }
                    when '30' {
                        communeList[i].Departement__c = '30 GARD';
                    }
                    when '31' {
                        communeList[i].Departement__c = '31 HAUTE-GARONNE';
                    }
                    when '32' {
                        communeList[i].Departement__c = '32 GERS';
                    }
                    when '33' {
                        communeList[i].Departement__c = '33 GIRONDE';
                    }
                    when '34' {
                        communeList[i].Departement__c = '34 HERAULT';
                    }
                    when '35' {
                        communeList[i].Departement__c = '35 ILLE-ET-VILAINE';
                    }
                    when '36' {
                        communeList[i].Departement__c = '36 INDRE';
                    }
                    when '37' {
                        communeList[i].Departement__c = '37 INDRE-ET-LOIRE';
                    }
                    when '38' {
                        communeList[i].Departement__c = '38 ISERE';
                    }
                    when '39' {
                        communeList[i].Departement__c = '39 JURA';
                    }
                    when '40' {
                        communeList[i].Departement__c = '40 LANDES';
                    }
                    when '41' {
                        communeList[i].Departement__c = '41 LOIR-ET-CHER';
                    }
                    when '42' {
                        communeList[i].Departement__c = '42 LOIRE';
                    }
                    when '43' {
                        communeList[i].Departement__c = '43 HAUTE-LOIRE';
                    }
                    when '44' {
                        communeList[i].Departement__c = '44 LOIRE-ATLANTIQUE';
                    }
                    when '45' {
                        communeList[i].Departement__c = '45 LOIRET';
                    }
                    when '46' {
                        communeList[i].Departement__c = '46 LOT';
                    }
                    when '47' {
                        communeList[i].Departement__c = '47 LOT-ET-GARONNE';
                    }
                    when '48' {
                        communeList[i].Departement__c = '48 LOZERE';
                    }
                    when '49' {
                        communeList[i].Departement__c = '49 MAINE-ET-LOIRE';
                    }
                    when '50' {
                        communeList[i].Departement__c = '50 MANCHE';
                    }
                    when '51' {
                        communeList[i].Departement__c = '51 MARNE';
                    }
                    when '52' {
                        communeList[i].Departement__c = '52 HAUTE-MARNE';
                    }
                    when '53' {
                        communeList[i].Departement__c = '53 MAYENNE';
                    }
                    when '54' {
                        communeList[i].Departement__c = '54 MEURTHE-ET-MOSELLE';
                    }
                    when '55' {
                        communeList[i].Departement__c = '55 MEUSE';
                    }
                    when '56' {
                        communeList[i].Departement__c = '56 MORBIHAN';
                    }
                    when '57' {
                        communeList[i].Departement__c = '57 MOSELLE';
                    }
                    when '58' {
                        communeList[i].Departement__c = '58 NIEVRE';
                    }
                    when '59' {
                        communeList[i].Departement__c = '59 NORD';
                    }
                    when '60' {
                        communeList[i].Departement__c = '60 OISE';
                    }
                    when '61' {
                        communeList[i].Departement__c = '61 ORNE';
                    }
                    when '62' {
                        communeList[i].Departement__c = '62 PAS-DE-CALAIS';
                    }
                    when '63' {
                        communeList[i].Departement__c = '63 PUY-DE-DOME';
                    }
                    when '64' {
                        communeList[i].Departement__c = '64 PYRENEES-ATLANTIQUES';
                    }
                    when '65' {
                        communeList[i].Departement__c = '65 HAUTES-PYRENEES';
                    }
                    when '66' {
                        communeList[i].Departement__c = '66 PYRENEES-ORIENTALES';
                    }
                    when '67' {
                        communeList[i].Departement__c = '67 BAS-RHIN';
                    }
                    when '68' {
                        communeList[i].Departement__c = '68 HAUT-RHIN';
                    }
                        when '69' {
                            communeList[i].Departement__c = '69 RHONE';
                        }
                        when '70' {
                            communeList[i].Departement__c = '70 HAUTE-SAONE';
                        }
                        when '71' {
                            communeList[i].Departement__c = '71 SAONE-ET-LOIRE';
                        }
                        when '72' {
                            communeList[i].Departement__c = '72 SARTHE';
                        }
                        when '73' {
                            communeList[i].Departement__c = '73 SAVOIE';
                        }
                        when '74' {
                            communeList[i].Departement__c = '74 HAUTE-SAVOIE';
                        }
                        when '75' {
                            communeList[i].Departement__c = '75 PARIS';
                        }
                        when '76' {
                            communeList[i].Departement__c = '76 SEINE-MARITIME';
                        }
                        when '77' {
                            communeList[i].Departement__c = '77 SEINE-ET-MARNE';
                        }
                        when '78' {
                            communeList[i].Departement__c = '78 YVELINES';
                        }
                        when '79' {
                            communeList[i].Departement__c = '79 DEUX-SEVRES';
                        }
                        when '80' {
                            communeList[i].Departement__c = '80 SOMME';
                        }
                        when '81' {
                            communeList[i].Departement__c = '81 TARN';
                        }
                        when '82' {
                            communeList[i].Departement__c = '82 TARN-ET-GARONNE';
                        }
                        when '83' {
                            communeList[i].Departement__c = '83 VAR';
                        }
                        when '84' {
                            communeList[i].Departement__c = '84 VAUCLUSE';
                        }
                        when '85' {
                            communeList[i].Departement__c = '85 VENDEE';
                        }
                        when '86' {
                            communeList[i].Departement__c = '86 VIENNE';
                        }
                        when '87' {
                            communeList[i].Departement__c = '87 HAUTE-VIENNE';
                        }
                        when '88' {
                            communeList[i].Departement__c = '88 VOSGES';
                        }
                        when '89' {
                            communeList[i].Departement__c = '89 YONNE';
                        }
                        when '90' {
                            communeList[i].Departement__c = '90 TERRITOIRE DE BELFORT';
                        }
                        when '91' {
                            communeList[i].Departement__c = '91 ESSONNE';
                        }
                        when '92' {
                            communeList[i].Departement__c = '92 HAUTS-DE-SEINE';
                        }
                        when '93' {
                            communeList[i].Departement__c = '93 SEINE-SAINT-DENIS';
                        }
                        when '94' {
                            communeList[i].Departement__c = '94 VAL-DE-MARNE';
                        }
                        when '95' {
                            communeList[i].Departement__c = '95 VAL-D\'OISE';
                        }
                        when '2B', '2b' {
                            communeList[i].Departement__c = '2B Haute-Corse ';
                        }
                        when '2A', '2a' {
                            communeList[i].Departement__c = '2A Corse-du-Sud ';
                        }
                        when else {
                            communeList[i].Departement__c = null;
                        }
                    }
                }
            }
 }
I know it's a huge condition so maybe there are some limitations ?

Here is my test class
@isTest
public with sharing class UpdateDepartmentField_TEST {
    @isTest static void TestUpdateDepartmentField() {
        //First I create a commune that should be automatically updated with Department__c
        Commune__c newCommune = new Commune__c();
        newCommune.Name='Commune 1';
        newCommune.CodeInseeCommune__c='12265';
        newCommune.CodeDepartement__c='01';
        newCommune.CodeCanton__c='02';
        newCommune.CodeRegion__c='73';
        insert newCommune;

        //Then i'll update this commune with all dept
        for (Integer i=2; i < 96; i++) {
            if (i<10) {
                newCommune.CodeDepartement__c= '0'+String.valueOf(i);
                update newCommune;
            } else if (i>9 && i<96) {
                newCommune.CodeDepartement__c= String.valueOf(i);
                update newCommune;
            }
        }

        newCommune.CodeDepartement__c='2B';
        update newCommune;

        newCommune.CodeDepartement__c='2A';
        update newCommune;         
    }
}
I don't know what i'm missing here.
When I test manually everithing seems fine, so i think it's only related to the test class. 
Do you see anything that could help reach 100% for this test class ? 

 
Hello, 

I need help Writting a test class since it's the first time i try it for real. I've done trailhead but i don't really see how it can apply to my trigger. 

Can someone can help me correct it and reach at least 75% of coverage code? I'm currently at 59%. 

Here is my Trigger : 
trigger UpdateCommune on Commune__c (before insert, before update) {
    //Store all Canton Codes in newly created records for Commune__c
    List<String> cantonCode = new List<String>();
    
    for (Commune__c a:Trigger.new){
        cantonCode.add(a.CodeCanton4Chiffres__c);
    }

    //Loop to update each Commune__c record with the proper CantonId__c
    List <Canton__C> CantonList = [Select ID, Name, CodeCanton4Chiffres__c from Canton__C where CodeCanton4Chiffres__c in :cantonCode];
    
    for (Integer i = 0; i <Trigger.new.size(); i++){
        if (CantonList.size() > 0 && Trigger.new[i].CodeCanton4Chiffres__c !=null){
            for (Canton__C c:CantonList){
                if (Trigger.new[i].CodeCanton4Chiffres__c == c.CodeCanton4Chiffres__c){
                    Trigger.new[i].CantonId__c = c.ID;
                  }
            }
        }
       else{
       Trigger.new[i].CantonId__c = null;
       System.System.debug('La référence Canton n\' pas pu être trouvée. Mise à jour impossible de la commune ');
        }
        
        // once the commune is updated with Canton ID, we need to update its "RegionAdministrative__c" fields regarding its "RegionCode__c" field value
    switch on Trigger.new[i].CodeRegion__c {
        when '82', '83' {
            Trigger.new[i].RegionAdministrative__c = 'AUVERGNE-RHONE-ALPES';
        }
        when '26', '43' {
            Trigger.new[i].RegionAdministrative__c = 'BOURGOGNE-FRANCHE-COMTE';
        }
        when '53' {
            Trigger.new[i].RegionAdministrative__c = 'BRETAGNE';
        }
        when '24' {
            Trigger.new[i].RegionAdministrative__c = 'CENTRE-VAL DE LOIRE';
        }
        when '94' {
            Trigger.new[i].RegionAdministrative__c = 'CORSE';
        }
        when '21','41', '42' {
            Trigger.new[i].RegionAdministrative__c = 'GRAND EST';
        }
        when '22', '31' {
            Trigger.new[i].RegionAdministrative__c = 'HAUTS-DE-FRANCE';
        }
        when  '11' {
            Trigger.new[i].RegionAdministrative__c = 'ILE-DE-FRANCE';
        }
        when '25', '23' {
            Trigger.new[i].RegionAdministrative__c = 'NORMANDIE';
        }        
        when '54', '74', '72' {
            Trigger.new[i].RegionAdministrative__c = 'NOUVELLE-AQUITAINE';
        }
        when '73', '91' {
            Trigger.new[i].RegionAdministrative__c = 'OCCITANIE';
        }
        when '52' {
            Trigger.new[i].RegionAdministrative__c = 'PAYS DE LA LOIRE';
        }
        when '93' {
            Trigger.new[i].RegionAdministrative__c = 'PROVENCE-ALPES-COTE D\'AZUR';
        }
    }
    }

}

Here is my Test Class : 
@isTest
private with sharing class UpdateCommune_TEST {
    private static testMethod void updateCommuneTEST() {
        // Create 2 Cantons then add them to a list that we will insert in Canton__c
        List<Canton__c> cantonList = new List<Canton__c>();

        Canton__c canton1 = new Canton__c(Name='Test Canton 2', CodeInseeCanton__c='12345', CodeCanton__c='01', CodeDepartement__c='12' );
        cantonList.add(canton1) ;
        Canton__c canton2 = new Canton__c(Name='Test Canton 2',  CodeInseeCanton__c='12543', CodeCanton__c='02', CodeDepartement__c='12' );
        cantonList.add(canton2) ;
        

        // Create 2 cities then add them to a list that we will insert in Commune__c
        List<Commune__c> communeList = new List<Commune__c>();
        Commune__c commune1 = new Commune__c(Name='Commune 1', CodeInseeCommune__c='12264', CodeDepartement__c='12', CodeCanton__c='01', CodeRegion__c='73');
        communeList.add(commune1);
        Commune__c commune2 = new Commune__c(Name='Commune 2', CodeInseeCommune__c='12265', CodeDepartement__c='12', CodeCanton__c='02', CodeRegion__c='73');
        communeList.add(commune2);
        
        test.startTest();
        insert cantonList;
        insert communeList;
        test.stopTest();

        // Retrieve the new communes
        List<Commune__c> createdCommunes = new List<Commune__c>([Select Name, Id, CodeInseeCommune__c, CodeDepartement__c, CodeCanton__c, CodeCanton4Chiffres__c, CodeRegion__c,Departement__c,CantonId__c, RegionAdministrative__c from Commune__c Where  CodeCanton4Chiffres__c='1201' OR CodeCanton4Chiffres__c='1202']);
       
        for (Integer i=0; i < createdCommunes.size(); i++) {
            System.debug('Value of canton after trigger fired: ' + createdCommunes[i].CantonId__c);
            // Test that the trigger correctly updated the RegionAdministrative
            System.assertEquals('OCCITANIE', createdCommunes[i].RegionAdministrative__c);
        }    
    }
}

Ty in advance for your help. 

 
Hi there, 

I'm quite stuck here. 
I've written a test class for my batch but it doesn't appears to be running. It stays at 0% :( 

I'm missing something but don't know what. 

Can someone help me figure it out? 

Here is my batch class : 
global class BatchUpdateAllLeads implements Database.Batchable <SObject> {
//START METHOD
    global Database.QueryLocator start(Database.BatchableContext bc){
        String Query='Select id,TECH_Update__c from Lead where TECH_Update__c=false AND Converted=false ' ;
        return Database.getQueryLocator(Query);
            }
//EXECUTE METHOD
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(Lead l: scope){
            l.TECH_Update__c = true;
        }
        update scope;
    }
//FINISH METHOD
    global void finish(Database.BatchableContext bc){
        Id job= bc.getJobId();
        System.debug(job);
    }
}

And this is my attemp of writting a proper test class :( 
@istest
private class BatchUpdateAllLeads_TEST {
    @istest
    static void testAcc(){
        List<Lead> l = new List<Lead>();
        Lead l1 = new Lead(Company	='BTP',LastName = 'Test',SIRET__c='12659991955626',Numero_d_ordre__c='9999-99-99',Region_GRDF__c='Ile de France',Tech_Update__c=false,IsConverted=false);
        l.add(l1);

        Lead l2= new Lead(Company	='Comp',LastName = 'Test2',SIRET__c='12659991955687',Numero_d_ordre__c='1111-99-99',Region_GRDF__c='Ile de France',Tech_Update__c=false, IsConverted=true);
        l.add(l2);

        Lead l3= new Lead(Company	='Agny', LastName = 'Test3', SIRET__c='19959991988687', Numero_d_ordre__c='1111-99-11', Region_GRDF__c='Ile de France', Tech_Update__c=true,IsConverted=true);
        l.add(l3);
        insert l;
   
    Test.startTest();
    BatchUpdateAllLeads ap= new BatchUpdateAllLeads();
    Id jobid= Database.executeBatch(ap);
    Test.stopTest();
    }
}

Ty very much for your help
Hi there, 

I'm quite stuck here. 
I've written a test class for my batch but it doesn't appears to be running. It stays at 0% :( 

I'm missing something but don't know what. 

Can someone help me figure it out? 

Here is my batch class : 
global class BatchUpdateAllLeads implements Database.Batchable <SObject> {
//START METHOD
    global Database.QueryLocator start(Database.BatchableContext bc){
        String Query='Select id,TECH_Update__c from Lead where TECH_Update__c=false AND Converted=false ' ;
        return Database.getQueryLocator(Query);
            }
//EXECUTE METHOD
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(Lead l: scope){
            l.TECH_Update__c = true;
        }
        update scope;
    }
//FINISH METHOD
    global void finish(Database.BatchableContext bc){
        Id job= bc.getJobId();
        System.debug(job);
    }
}

And this is my attemp of writting a proper test class :( 
@istest
private class BatchUpdateAllLeads_TEST {
    @istest
    static void testAcc(){
        List<Lead> l = new List<Lead>();
        Lead l1 = new Lead(Company	='BTP',LastName = 'Test',SIRET__c='12659991955626',Numero_d_ordre__c='9999-99-99',Region_GRDF__c='Ile de France',Tech_Update__c=false,IsConverted=false);
        l.add(l1);

        Lead l2= new Lead(Company	='Comp',LastName = 'Test2',SIRET__c='12659991955687',Numero_d_ordre__c='1111-99-99',Region_GRDF__c='Ile de France',Tech_Update__c=false, IsConverted=true);
        l.add(l2);

        Lead l3= new Lead(Company	='Agny', LastName = 'Test3', SIRET__c='19959991988687', Numero_d_ordre__c='1111-99-11', Region_GRDF__c='Ile de France', Tech_Update__c=true,IsConverted=true);
        l.add(l3);
        insert l;
   
    Test.startTest();
    BatchUpdateAllLeads ap= new BatchUpdateAllLeads();
    Id jobid= Database.executeBatch(ap);
    Test.stopTest();
    }
}

Ty very much for your help
Hi there, I have written a Test Class for a Switch condition but When I'm executing Tests, I still have a 68% Coverage, nothing more. 

Here is my main class wich is tested until case : 33 
public with sharing class UpdateDepartmentField_CLASS {
    public static void UpdateDepartmentFieldCommune(List<Commune__c> communeList) {

        for (Integer i=0;  i < communeList.size(); i++) {
                switch on communeList[i].CodeDepartement__c {
                    when '01' {
                        communeList[i].Departement__c = '01 AIN';
                    }
                    when '02' {
                        communeList[i].Departement__c = '02 AISNE';
                    }
                    when '03' {
                        communeList[i].Departement__c = '03 ALLIER';
                    }
                    when '04' {
                        communeList[i].Departement__c = '04 ALPES-DE-HAUTE-PROVENCE';
                    }
                    when '05' {
                        communeList[i].Departement__c = '05 HAUTES-ALPES';
                    }
                    when '06' {
                        communeList[i].Departement__c = '06 ALPES-MARITIMES';
                    }
                    when '07' {
                        communeList[i].Departement__c = '07 ARDECHE';
                    }
                    when '08' {
                        communeList[i].Departement__c = '08 ARDENNES';
                    }
                    when '09' {
                        communeList[i].Departement__c = '09 ARIEGE';
                    }
                    when '10' {
                        communeList[i].Departement__c = '10 AUBE';
                    }
                    when '11' {
                        communeList[i].Departement__c = '11 AUDE';
                    }
                    when '12' {
                        communeList[i].Departement__c = '12 AVEYRON';
                    }
                    when '13' {
                        communeList[i].Departement__c = '13 BOUCHES-DU-RHONE';
                    }
                    when '14' {
                        communeList[i].Departement__c = '14 CALVADOS';
                    }
                    when '15' {
                        communeList[i].Departement__c = '15 CANTAL';
                    }
                    when '16' {
                        communeList[i].Departement__c = '16 CHARENTE';
                    }
                    when '17' {
                        communeList[i].Departement__c = '17 CHARENTE-MARITIME';
                    }
                    when '18' {
                        communeList[i].Departement__c = '18 CHER';
                    }
                    when '19' {
                        communeList[i].Departement__c = '19 CORREZE';
                    }
                    when '21' {
                        communeList[i].Departement__c = '21 COTE-D\'OR';
                    }
                    when '22' {
                        communeList[i].Departement__c = '22 COTES-D\'ARMOR';
                    }
                    when '23' {
                        communeList[i].Departement__c = '23 CREUSE';
                    }
                    when '24' {
                        communeList[i].Departement__c = '24 DORDOGNE';
                    }
                    when '25' {
                        communeList[i].Departement__c = '25 DOUBS';
                    }
                    when '26' {
                        communeList[i].Departement__c = '26 DROME';
                    }
                    when '27' {
                        communeList[i].Departement__c = '27 EURE';
                    }
                    when '28' {
                        communeList[i].Departement__c = '28 EURE-ET-LOIR';
                    }
                    when '29' {
                        communeList[i].Departement__c = '29 FINISTERE';
                    }
                    when '30' {
                        communeList[i].Departement__c = '30 GARD';
                    }
                    when '31' {
                        communeList[i].Departement__c = '31 HAUTE-GARONNE';
                    }
                    when '32' {
                        communeList[i].Departement__c = '32 GERS';
                    }
                    when '33' {
                        communeList[i].Departement__c = '33 GIRONDE';
                    }
                    when '34' {
                        communeList[i].Departement__c = '34 HERAULT';
                    }
                    when '35' {
                        communeList[i].Departement__c = '35 ILLE-ET-VILAINE';
                    }
                    when '36' {
                        communeList[i].Departement__c = '36 INDRE';
                    }
                    when '37' {
                        communeList[i].Departement__c = '37 INDRE-ET-LOIRE';
                    }
                    when '38' {
                        communeList[i].Departement__c = '38 ISERE';
                    }
                    when '39' {
                        communeList[i].Departement__c = '39 JURA';
                    }
                    when '40' {
                        communeList[i].Departement__c = '40 LANDES';
                    }
                    when '41' {
                        communeList[i].Departement__c = '41 LOIR-ET-CHER';
                    }
                    when '42' {
                        communeList[i].Departement__c = '42 LOIRE';
                    }
                    when '43' {
                        communeList[i].Departement__c = '43 HAUTE-LOIRE';
                    }
                    when '44' {
                        communeList[i].Departement__c = '44 LOIRE-ATLANTIQUE';
                    }
                    when '45' {
                        communeList[i].Departement__c = '45 LOIRET';
                    }
                    when '46' {
                        communeList[i].Departement__c = '46 LOT';
                    }
                    when '47' {
                        communeList[i].Departement__c = '47 LOT-ET-GARONNE';
                    }
                    when '48' {
                        communeList[i].Departement__c = '48 LOZERE';
                    }
                    when '49' {
                        communeList[i].Departement__c = '49 MAINE-ET-LOIRE';
                    }
                    when '50' {
                        communeList[i].Departement__c = '50 MANCHE';
                    }
                    when '51' {
                        communeList[i].Departement__c = '51 MARNE';
                    }
                    when '52' {
                        communeList[i].Departement__c = '52 HAUTE-MARNE';
                    }
                    when '53' {
                        communeList[i].Departement__c = '53 MAYENNE';
                    }
                    when '54' {
                        communeList[i].Departement__c = '54 MEURTHE-ET-MOSELLE';
                    }
                    when '55' {
                        communeList[i].Departement__c = '55 MEUSE';
                    }
                    when '56' {
                        communeList[i].Departement__c = '56 MORBIHAN';
                    }
                    when '57' {
                        communeList[i].Departement__c = '57 MOSELLE';
                    }
                    when '58' {
                        communeList[i].Departement__c = '58 NIEVRE';
                    }
                    when '59' {
                        communeList[i].Departement__c = '59 NORD';
                    }
                    when '60' {
                        communeList[i].Departement__c = '60 OISE';
                    }
                    when '61' {
                        communeList[i].Departement__c = '61 ORNE';
                    }
                    when '62' {
                        communeList[i].Departement__c = '62 PAS-DE-CALAIS';
                    }
                    when '63' {
                        communeList[i].Departement__c = '63 PUY-DE-DOME';
                    }
                    when '64' {
                        communeList[i].Departement__c = '64 PYRENEES-ATLANTIQUES';
                    }
                    when '65' {
                        communeList[i].Departement__c = '65 HAUTES-PYRENEES';
                    }
                    when '66' {
                        communeList[i].Departement__c = '66 PYRENEES-ORIENTALES';
                    }
                    when '67' {
                        communeList[i].Departement__c = '67 BAS-RHIN';
                    }
                    when '68' {
                        communeList[i].Departement__c = '68 HAUT-RHIN';
                    }
                        when '69' {
                            communeList[i].Departement__c = '69 RHONE';
                        }
                        when '70' {
                            communeList[i].Departement__c = '70 HAUTE-SAONE';
                        }
                        when '71' {
                            communeList[i].Departement__c = '71 SAONE-ET-LOIRE';
                        }
                        when '72' {
                            communeList[i].Departement__c = '72 SARTHE';
                        }
                        when '73' {
                            communeList[i].Departement__c = '73 SAVOIE';
                        }
                        when '74' {
                            communeList[i].Departement__c = '74 HAUTE-SAVOIE';
                        }
                        when '75' {
                            communeList[i].Departement__c = '75 PARIS';
                        }
                        when '76' {
                            communeList[i].Departement__c = '76 SEINE-MARITIME';
                        }
                        when '77' {
                            communeList[i].Departement__c = '77 SEINE-ET-MARNE';
                        }
                        when '78' {
                            communeList[i].Departement__c = '78 YVELINES';
                        }
                        when '79' {
                            communeList[i].Departement__c = '79 DEUX-SEVRES';
                        }
                        when '80' {
                            communeList[i].Departement__c = '80 SOMME';
                        }
                        when '81' {
                            communeList[i].Departement__c = '81 TARN';
                        }
                        when '82' {
                            communeList[i].Departement__c = '82 TARN-ET-GARONNE';
                        }
                        when '83' {
                            communeList[i].Departement__c = '83 VAR';
                        }
                        when '84' {
                            communeList[i].Departement__c = '84 VAUCLUSE';
                        }
                        when '85' {
                            communeList[i].Departement__c = '85 VENDEE';
                        }
                        when '86' {
                            communeList[i].Departement__c = '86 VIENNE';
                        }
                        when '87' {
                            communeList[i].Departement__c = '87 HAUTE-VIENNE';
                        }
                        when '88' {
                            communeList[i].Departement__c = '88 VOSGES';
                        }
                        when '89' {
                            communeList[i].Departement__c = '89 YONNE';
                        }
                        when '90' {
                            communeList[i].Departement__c = '90 TERRITOIRE DE BELFORT';
                        }
                        when '91' {
                            communeList[i].Departement__c = '91 ESSONNE';
                        }
                        when '92' {
                            communeList[i].Departement__c = '92 HAUTS-DE-SEINE';
                        }
                        when '93' {
                            communeList[i].Departement__c = '93 SEINE-SAINT-DENIS';
                        }
                        when '94' {
                            communeList[i].Departement__c = '94 VAL-DE-MARNE';
                        }
                        when '95' {
                            communeList[i].Departement__c = '95 VAL-D\'OISE';
                        }
                        when '2B', '2b' {
                            communeList[i].Departement__c = '2B Haute-Corse ';
                        }
                        when '2A', '2a' {
                            communeList[i].Departement__c = '2A Corse-du-Sud ';
                        }
                        when else {
                            communeList[i].Departement__c = null;
                        }
                    }
                }
            }
 }
I know it's a huge condition so maybe there are some limitations ?

Here is my test class
@isTest
public with sharing class UpdateDepartmentField_TEST {
    @isTest static void TestUpdateDepartmentField() {
        //First I create a commune that should be automatically updated with Department__c
        Commune__c newCommune = new Commune__c();
        newCommune.Name='Commune 1';
        newCommune.CodeInseeCommune__c='12265';
        newCommune.CodeDepartement__c='01';
        newCommune.CodeCanton__c='02';
        newCommune.CodeRegion__c='73';
        insert newCommune;

        //Then i'll update this commune with all dept
        for (Integer i=2; i < 96; i++) {
            if (i<10) {
                newCommune.CodeDepartement__c= '0'+String.valueOf(i);
                update newCommune;
            } else if (i>9 && i<96) {
                newCommune.CodeDepartement__c= String.valueOf(i);
                update newCommune;
            }
        }

        newCommune.CodeDepartement__c='2B';
        update newCommune;

        newCommune.CodeDepartement__c='2A';
        update newCommune;         
    }
}
I don't know what i'm missing here.
When I test manually everithing seems fine, so i think it's only related to the test class. 
Do you see anything that could help reach 100% for this test class ? 

 
Hello, 

I need help Writting a test class since it's the first time i try it for real. I've done trailhead but i don't really see how it can apply to my trigger. 

Can someone can help me correct it and reach at least 75% of coverage code? I'm currently at 59%. 

Here is my Trigger : 
trigger UpdateCommune on Commune__c (before insert, before update) {
    //Store all Canton Codes in newly created records for Commune__c
    List<String> cantonCode = new List<String>();
    
    for (Commune__c a:Trigger.new){
        cantonCode.add(a.CodeCanton4Chiffres__c);
    }

    //Loop to update each Commune__c record with the proper CantonId__c
    List <Canton__C> CantonList = [Select ID, Name, CodeCanton4Chiffres__c from Canton__C where CodeCanton4Chiffres__c in :cantonCode];
    
    for (Integer i = 0; i <Trigger.new.size(); i++){
        if (CantonList.size() > 0 && Trigger.new[i].CodeCanton4Chiffres__c !=null){
            for (Canton__C c:CantonList){
                if (Trigger.new[i].CodeCanton4Chiffres__c == c.CodeCanton4Chiffres__c){
                    Trigger.new[i].CantonId__c = c.ID;
                  }
            }
        }
       else{
       Trigger.new[i].CantonId__c = null;
       System.System.debug('La référence Canton n\' pas pu être trouvée. Mise à jour impossible de la commune ');
        }
        
        // once the commune is updated with Canton ID, we need to update its "RegionAdministrative__c" fields regarding its "RegionCode__c" field value
    switch on Trigger.new[i].CodeRegion__c {
        when '82', '83' {
            Trigger.new[i].RegionAdministrative__c = 'AUVERGNE-RHONE-ALPES';
        }
        when '26', '43' {
            Trigger.new[i].RegionAdministrative__c = 'BOURGOGNE-FRANCHE-COMTE';
        }
        when '53' {
            Trigger.new[i].RegionAdministrative__c = 'BRETAGNE';
        }
        when '24' {
            Trigger.new[i].RegionAdministrative__c = 'CENTRE-VAL DE LOIRE';
        }
        when '94' {
            Trigger.new[i].RegionAdministrative__c = 'CORSE';
        }
        when '21','41', '42' {
            Trigger.new[i].RegionAdministrative__c = 'GRAND EST';
        }
        when '22', '31' {
            Trigger.new[i].RegionAdministrative__c = 'HAUTS-DE-FRANCE';
        }
        when  '11' {
            Trigger.new[i].RegionAdministrative__c = 'ILE-DE-FRANCE';
        }
        when '25', '23' {
            Trigger.new[i].RegionAdministrative__c = 'NORMANDIE';
        }        
        when '54', '74', '72' {
            Trigger.new[i].RegionAdministrative__c = 'NOUVELLE-AQUITAINE';
        }
        when '73', '91' {
            Trigger.new[i].RegionAdministrative__c = 'OCCITANIE';
        }
        when '52' {
            Trigger.new[i].RegionAdministrative__c = 'PAYS DE LA LOIRE';
        }
        when '93' {
            Trigger.new[i].RegionAdministrative__c = 'PROVENCE-ALPES-COTE D\'AZUR';
        }
    }
    }

}

Here is my Test Class : 
@isTest
private with sharing class UpdateCommune_TEST {
    private static testMethod void updateCommuneTEST() {
        // Create 2 Cantons then add them to a list that we will insert in Canton__c
        List<Canton__c> cantonList = new List<Canton__c>();

        Canton__c canton1 = new Canton__c(Name='Test Canton 2', CodeInseeCanton__c='12345', CodeCanton__c='01', CodeDepartement__c='12' );
        cantonList.add(canton1) ;
        Canton__c canton2 = new Canton__c(Name='Test Canton 2',  CodeInseeCanton__c='12543', CodeCanton__c='02', CodeDepartement__c='12' );
        cantonList.add(canton2) ;
        

        // Create 2 cities then add them to a list that we will insert in Commune__c
        List<Commune__c> communeList = new List<Commune__c>();
        Commune__c commune1 = new Commune__c(Name='Commune 1', CodeInseeCommune__c='12264', CodeDepartement__c='12', CodeCanton__c='01', CodeRegion__c='73');
        communeList.add(commune1);
        Commune__c commune2 = new Commune__c(Name='Commune 2', CodeInseeCommune__c='12265', CodeDepartement__c='12', CodeCanton__c='02', CodeRegion__c='73');
        communeList.add(commune2);
        
        test.startTest();
        insert cantonList;
        insert communeList;
        test.stopTest();

        // Retrieve the new communes
        List<Commune__c> createdCommunes = new List<Commune__c>([Select Name, Id, CodeInseeCommune__c, CodeDepartement__c, CodeCanton__c, CodeCanton4Chiffres__c, CodeRegion__c,Departement__c,CantonId__c, RegionAdministrative__c from Commune__c Where  CodeCanton4Chiffres__c='1201' OR CodeCanton4Chiffres__c='1202']);
       
        for (Integer i=0; i < createdCommunes.size(); i++) {
            System.debug('Value of canton after trigger fired: ' + createdCommunes[i].CantonId__c);
            // Test that the trigger correctly updated the RegionAdministrative
            System.assertEquals('OCCITANIE', createdCommunes[i].RegionAdministrative__c);
        }    
    }
}

Ty in advance for your help.