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
Joan RubioJoan Rubio 

Help with trigger test

Hi all,

I'm new on Apex, and i need to test an apex trigger, but it's my first time and I dont know how to do it.

Can anyone help me testing this trigger please?

Thanks in advance!!



if(trigger.isAfter){
        List<Id> bosIdList = new List<Id>();
        List<Id> bosIdListNotNull = new List<Id>();
        List<Oportunidad_Producto__c> listToUpdate = new List<Oportunidad_Producto__c>();
        for(AQUA_Peticion_BOe__c q: Trigger.new){
            System.debug('Estado: ' + q.BOE_SEL_Estado_BOe__c);
            if(q.BOE_SEL_Estado_BOe__c == 'Petición anulada'){
                bosIdList.add(q.BOE_LKP_Producto_de_oportunidad__c);
            }
            else{
                bosIdListNotNull.add(q.BOE_LKP_Producto_de_oportunidad__c);
            }
        }
        if(!bosIdList.isEmpty()){ 
            List<Oportunidad_Producto__c> prodOportList = [SELECT Id,OP_TXT_Departamento__c,OP_DAT_Fecha_envio_tecnologica__c,OP_DAT_Fecha_de_recepcion_de_oferta__c, (SELECT Id,BOE_LKP_Producto_de_oportunidad__c, BOE_DAT_Fecha_Inicio_en_curso__c,  BOE_DAT_Fecha_Fin_en_curso__c, BOE_SEL_Departamento__c FROM Peticiones_BOe1__r WHERE BOE_SEL_Estado_BOe__c != 'Petición anulada'  ORDER BY LastModifiedDate Desc ) FROM Oportunidad_Producto__c WHERE Id IN: bosIdList];
            for( Oportunidad_Producto__c op : prodOportList ){
                List<AQUA_Peticion_BOe__c> petBos = new List<AQUA_Peticion_BOe__c>();
                petBos.addAll(op.Peticiones_BOe1__r);
                if(!petBos.isEmpty()){ 
                    op.OP_TXT_Departamento__c = petBos.get(0).BOE_SEL_Departamento__c;
                    op.OP_DAT_Fecha_envio_tecnologica__c= petBos.get(0).BOE_DAT_Fecha_Inicio_en_curso__c;
                    op.OP_DAT_Fecha_de_recepcion_de_oferta__c= petBos.get(0).BOE_DAT_Fecha_Fin_en_curso__c;
                }
                else{  
                    op.OP_TXT_Departamento__c = null;
                    op.OP_DAT_Fecha_envio_tecnologica__c= null;
                    op.OP_DAT_Fecha_de_recepcion_de_oferta__c= null; 
                }
                listToUpdate.add(op);
            }    
        }
        if(!bosIdListNotNull.isEmpty()){  
            List<Oportunidad_Producto__c> prodOportList = [SELECT Id,OP_TXT_Departamento__c,OP_DAT_Fecha_envio_tecnologica__c,OP_DAT_Fecha_de_recepcion_de_oferta__c, (SELECT Id,BOE_LKP_Producto_de_oportunidad__c, BOE_DAT_Fecha_Inicio_en_curso__c,  BOE_DAT_Fecha_Fin_en_curso__c, BOE_SEL_Departamento__c FROM Peticiones_BOe1__r ORDER BY LastModifiedDate Desc ) FROM Oportunidad_Producto__c WHERE Id IN: bosIdListNotNull];
            for( Oportunidad_Producto__c op : prodOportList ){
               List<AQUA_Peticion_BOe__c> petBos = new List<AQUA_Peticion_BOe__c>();
               petBos.addAll(op.Peticiones_BOe1__r);
               op.OP_TXT_Departamento__c = petBos.get(0).BOE_SEL_Departamento__c;
               op.OP_DAT_Fecha_envio_tecnologica__c= petBos.get(0).BOE_DAT_Fecha_Inicio_en_curso__c;
               op.OP_DAT_Fecha_de_recepcion_de_oferta__c= petBos.get(0).BOE_DAT_Fecha_Fin_en_curso__c;
               listToUpdate.add(op);
            }            
        }
        try{
            update listToUpdate;
        }Catch(Exception e){
            System.debug('Error: '+e.getMessage());
        }
    }
Best Answer chosen by Joan Rubio
jigarshahjigarshah
Joan,

You will need to write Test classes, which are nothing but Apex classes that are aimed at testing the code containing the business logic. Basically, the test code is reuquired to recreate the scenarios which cause the Apex code containing the business logic, to execute. The more lines of Apex code are executed due to the test code, higher is the test code coverage.

In your case, you would need to insert records of AQUA_Peticion_BOe__c type, via your Apex Test method and check if that results in updating the relevant Oportunidad_Producto__c records. Basically the test code verified that the Apex code containing the business logic complies to the requirement by verifying the expected and actual output values using the System.assert() and System.assertEquals() methods. 

Please note that all data changes performed during test code execution are temporary and do not persist.

Refer the following links to understand more about how to implement a test class
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

Salesforce mandates the overall org code coverage to be 75% i.e. of all the code existing on the production instance, 75% of that should be covered via the test code. Salesforce executes this verification while the code is being deployed to Production and if this mandate is not satisifed, Salesforce permits the deployment until this mandate is adhered to. This is Salesforce's way of ensuring that the code being deployed to any production instance is tested thus promoting a Test Driven Development approach.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.