• Joan Rubio
  • NEWBIE
  • 20 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
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());
        }
    }
I'm new programming and need some help...
I want to make a trigger to prevent deleting Accounts that have any Location associated. If an account has an associated Location, I want to display an error message.
There are a lookup field on Accounts to Location (Location__c).
Can you help me?
Thanks!
I'm new programming and need some help...
I want to make a trigger to prevent deleting Accounts that have any Location associated. If an account has an associated Location, I want to display an error message.
There are a lookup field on Accounts to Location (Location__c).
Can you help me?
Thanks!