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
Alejandro Garcia LopezAlejandro Garcia Lopez 

how to do a test for a trigger old and new

hello, i have done some tests for triggers with 100% of coverage but now i have a problem and i think it is because i'm using trigger.old, this is the trigger
trigger gerente on Documento__c (after update) {
    for(Documento__c p:Trigger.old){
    for(Documento__c c:Trigger.new)
{
    Documento__c y=[select en_espera__c from documento__c where id=:c.Id];
    oferta__c r=[select id,etapa__c,TipoOferta__c, gerente_operativo__c from oferta__c where id=:c.oferta__c];
    Account l=[select Pendientes_Realizados__c from Account where id=:r.gerente_operativo__c];
    EtapaOferta__c x=[select EtapaActual__c from EtapaOferta__c where id=:c.EtapaOferta__c];
    if(r.TipoOferta__c!='Contado'){
    if(r.Gerente_Operativo__c!=null){
    if(p.Cerrado__c!=c.Cerrado__c){
    if (((r.Etapa__c=='Expediente aprobado')||(r.Etapa__c=='Compilación expediente crediticio')||(r.Etapa__c=='Rehabilitación')
        ||(r.Etapa__c=='Cierre oferta'))&&((c.name=='Presupuesto de rehabilitación')
       ||(c.name=='Fotografia del estado inicial')||(c.name=='Fotografia del estado final de la propiedad')
       ||(c.name=='CLG')||(c.name=='Predial')||(c.name=='Constancia de Crédito')
       ||(c.name=='Carta de instrucción Notarial')||(c.name=='Carta de Intrucción Revimex')||(c.name=='Proyecto de Escrituras')
       ||(c.name=='Aviso de Retención Y Firma Cliente')||(c.name=='Confirmación cobro propiedad')||(c.name=='Carta de entrega')
       ||(c.name=='Fotos de Entrega')||(c.name=='Pre-cierre')||(c.name=='Pago Activado')||(c.name=='Escrituras Registradas')
       ||(c.name=='Cierre Contabilidad')||(c.name=='Comprobante Firma Apoderado')||(c.name=='Comprobante Comisión Broker')
       ||(c.name=='Comprobante Comisión Gerente')||(c.name=='Comprobante Anticipo Comisión Gerente')||(c.name=='Pago Ventanilla Unica')
       ||(c.name=='Comprobante de Pago Ventanilla Unica')||(c.name=='Pre-Pantalla'))&&(c.Cerrado__c==true)&&(c.Iniciado__c==true)
       &&(x.EtapaActual__c ==true)&&(c.En_Espera__c==false))
    {
        if (l.Pendientes_Realizados__c==null)
        {
        l.Pendientes_Realizados__c=0;
        }
        l.Pendientes_Realizados__c+=1;
        update l;
    }
    }

if (((r.Etapa__c=='Expediente aprobado')||(r.Etapa__c=='Compilación expediente crediticio')||(r.Etapa__c=='Rehabilitación')
        ||(r.Etapa__c=='Cierre oferta'))&&((c.name=='Presupuesto de rehabilitación')
       ||(c.name=='Fotografia del estado inicial')||(c.name=='Fotografia del estado final de la propiedad')
       ||(c.name=='CLG')||(c.name=='Predial')||(c.name=='Constancia de Crédito')
       ||(c.name=='Carta de instrucción Notarial')||(c.name=='Carta de Intrucción Revimex')||(c.name=='Proyecto de Escrituras')
       ||(c.name=='Aviso de Retención Y Firma Cliente')||(c.name=='Confirmación cobro propiedad')||(c.name=='Carta de entrega')
       ||(c.name=='Fotos de Entrega')||(c.name=='Pre-cierre')||(c.name=='Pago Activado')||(c.name=='Escrituras Registradas')
       ||(c.name=='Cierre Contabilidad')||(c.name=='Comprobante Firma Apoderado')||(c.name=='Comprobante Comisión Broker')
       ||(c.name=='Comprobante Comisión Gerente')||(c.name=='Comprobante Anticipo Comisión Gerente')||(c.name=='Pago Ventanilla Unica')
       ||(c.name=='Comprobante de Pago Ventanilla Unica')||(c.name=='Pre-Pantalla'))&&(c.Cerrado__c==true)&&(c.Iniciado__c==true)
         &&(x.EtapaActual__c ==true)&&(c.En_Espera__c==true))
			 {
             if((p.NumFiles_MasFilesPadre__c==0)&&(c.NumFiles_MasFilesPadre__c==1))
             {
        if (l.Pendientes_Realizados__c==null)
        {
        l.Pendientes_Realizados__c=0;
        }
        l.Pendientes_Realizados__c+=1;
        y.En_Espera__c=false;
        update y;         
        update l;
             }
         }
    }
    }}}
}
this is the test class 
@isTest
public class gerenteTest {
    static testMethod void gerenteTest()
    {
        documento__c dd=new documento__c(iniciado__c=true);
        insert dd;
        account a=new account();
        a.Pendientes_Realizados__c=1;
        a.name='prueba';
        insert a;
        Account acc=[select Pendientes_Realizados__c 
        from Account where id=:a.id];
        Oferta__c fer = new Oferta__c();
        fer.gerente_operativo__c = acc.id;
        insert fer;
        oferta__c ofe=[select id,etapa__c,TipoOferta__c, gerente_operativo__c 
        from oferta__c where id=:fer.id];
        etapaoferta__c et=new etapaoferta__c();
        et.oferta__c=ofe.id;
        insert et;
        etapaoferta__c eo=[select EtapaActual__c from EtapaOferta__c where id=:et.id];
        Documento__c e = new Documento__c();
        e.oferta__c=ofe.id;
        e.EtapaOferta__c=eo.id;
        insert e;
        e.name='dd';
        update e;
    }
}

it returns 41% of coverage but i don't know what is missing, could someone help me, please?
 
Shashikant SharmaShashikant Sharma
HI,

Basic mistake that i see in your Code

1. Why are you loopging over both Trigger.Old and Trigger.New
If you want to compare values you should loop over one of them and use Trigger.OldMap and Trigger.NewMap to compare with other.
2. In order to increase coverage you have to meet all conditoanl criterias of you trigger, so your data should be like that.

Thanks
Shashikant
Alejandro Garcia LopezAlejandro Garcia Lopez
hi, thanks for your answer, coud you give me an example of how to compare Trigger.OldMap and Trigger.NewMap?