1

I have written a code that should fulfill the following:

  1. Postgraduate Course (Product2) has fields Cierre_economico_de_Curso__c, Fecha_cierre_economico__c and Notificacion_calificaciones__c. If they are set or filled and condition in point 3) is fulfilled, they should do 4)
  2. I look for every record in object Matriculaci_n__c where the Postgraduate Course Id in Product2 equals the Postgraduate Course Id in the Matriculaci_n__c object (Matriculaci_n__c has a lookup to Product2, the field is Programa_Academico_1__c)
  3. If they are equal
  4. Field Envio_de_Actas__c should be set

PS: Edicion_de_Postgrado__c is a formula field in Matriculaci_n__c: Programa_Academico__c.Edicion_de_Postgrado__c. Postgraduate Course (Product2) has the same field.

Unfortunately my code doesn't do what it should do, it doesn't happen anything, no errors, no effects. My code is the following:

    public class MatriculacionTriggerHandler extends BaseHandler{
public MatriculacionTriggerHandler(boolean isExecuting, integer size){
        super( isExecuting,  size);
    }

    public void handleAfter(List<Matriculaci_n__c> matriculaciones)
    {

        List<Product2> postgrados = [SELECT Id, Name, Edicion_de_Postgrado__c FROM Product2 where Cierre_economico_de_Curso__c = true and Notificacion_de_calificaciones__c = true and Fecha_de_cierre_economico__c != null];
        List<Matriculaci_n__c> matriculacionestoupdate = [SELECT Id, Name, Programa_acad_mico_1__c, Edicion_de_Postgrado__c FROM Matriculaci_n__c where Envio_de_Actas__c = false and Numero_de_actas__c > 0];

        for(Matriculaci_n__c matriculacion: matriculaciones){
            for(Product2 postgrado: postgrados){
              for(Matriculaci_n__c matriculaciontoupdate: matriculacionestoupdate) {
                  if(matriculaciontoupdate.Programa_acad_mico_1__c == postgrado.Id){
                   matriculaciontoupdate.Envio_de_Actas__c = true;
                   matriculacionestoupdate.add(matriculaciontoupdate);
                }
              }
            }
        }
        update matriculacionestoupdate;

    }



}

This should happen with already existing records and not by inserting records. Probably I am not using the correct trigger handler.

I would pretty appreciate if someone could give me some tips, is my code correctly written?

  • did you put debug statement to find out how the values are coming into this method – Santanu Boral Apr 19 '17 at 22:17
  • No...where would you recommend me to put the debugs? – José Apr 20 '17 at 7:05
0

1) You have 3 for-loops nested, and the outer loop has a variable named 'matriculacion' which is not really used anywhere. So it tries to do exactly the same thing many times over... So the whole code needs an overhaul.

2) With so many variables with similar, lowercase names one looses track of what is what.

3) This is what you would do in an AFTER UPDATE only, as in the BEFORE UPDATE trigger you could simply change the value of Envio_de_Actas__c without any extra list or update statement.

4) You don't want to select the whole Product2 table, just the values you need.

5) You probably want something like this:

public void handleAfter(List<Matriculaci_n__c> matriculaciones)
{
  Set<Id> acad_micos = new Set<Id>();
  for (Matriculaci_n__c m : matriculaciones) {
    acad_micos.add(m.Programa_acad_mico_1__c);
  }
  Map<Id, Product2> postgrados = new Map<Id, Product2>([
      SELECT Id, Name, Edicion_de_Postgrado__c
      FROM Product2
      WHERE Cierre_economico_de_Curso__c = true
      AND Notificacion_de_calificaciones__c = true
      AND Fecha_de_cierre_economico__c != null
      AND Id IN :acad_micos
  ]);
  List<Matriculaci_n__c> matristoupdate = new List<Matriculaci_n__c>();
  for(Matriculaci_n__c m: matriculaciones){
    if (postgrados.containsKey(m.Programa_acad_mico_1__c) && m.Envio_de_Actas__c = false && m.Numero_de_actas__c > 0) {
      m.Envio_de_Actas__c = true;
      matristoupdate.add(m);
    }
  }
  update matristoupdate;
}

6) I wrote this without compiling or anything... It can be even simpler if you only need the Product2.Id, and no other fields of that object (no map but just a list of Id's). Maybe you want something else from the Product2 to copy later, I don't know :-) But this should get you on the right track.

  • Thanks Henk for the answer. I am trying to correct line: if (postgrados.containsKey(m.Programa_acad_mico_1__c) && m.Envio_de_Actas__c = false && m.Numero_de_actas__c > 0) { – José Apr 20 '17 at 8:59
  • Amended: if (postgrados.containsKey(m.Programa_acad_mico_1__c) && m.Envio_de_Actas__c == false && m.Numero_de_actas__c > 0) { – José Apr 20 '17 at 9:00
0

Actually my MatriculacionTrigger looks like this:

trigger MatriculacionTrigger on Matriculaci_n__c (before insert, after insert, before update, after update) {

MatriculacionTriggerHandler handler = new MatriculacionTriggerHandler(Trigger.isExecuting, Trigger.size); if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){ //handler.handleBefore(Trigger.new); }else if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){ handler.handleAfter(Trigger.new); } }

it has the part isUpdate, so there is something missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.