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
Vittorio Luca MonacoVittorio Luca Monaco 

Trigger after insert, after update

Hi, i have a problem with a trigger.
I need to reduce the count when a "Iscrizione" is deleted from a Contact. The trigger doesn't work when i remove the record. How can i solve this ?

trigger ContaIscr on Iscrizione__c (after insert ,after delete) {
    set<id> studId = new set<id>();
    for(Iscrizione__c a : trigger.new){
        studId.add(a.Contact__c);
    }
    list<Contact> lstCoustumObject = [Select id, totale_iscrizioni__c from contact where id in: studId];
    for(Iscrizione__c isc : Trigger.new){
    for(Contact con : lstCoustumObject){
        if(isc.contact__c != NULL){
            con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c +1;
        }else{
            con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c -1;   
        }
    }
}
    update lstCoustumObject;
}
Best Answer chosen by Vittorio Luca Monaco
IqbalIqbal
Hi Vittorio Luca Monaco,

There is nothing wrong with your logic just a minor change is required. According to salesforce, you cannot use trigger.new in delete context. 

trigger ContaIscr on Iscrizione__c (after insert ,after delete) {

if(trigger.isAfter && trigger.isInsert){
       ContalscrHelper.checkMethod(trigger.new);
}
else if(trigger.isAfter && trigger.isDelete){
       ContalscrHelper.checkMethod(trigger.old);
}


public class ContalscrHelper{

public static void checkMethod(list<Iscrizione__c > IscrizioneList){
set<id> studId = new set<id>();
    for(Iscrizione__c a : IscrizioneList){
        studId.add(a.Contact__c);
    }
    list<Contact> lstCoustumObject = [Select id, totale_iscrizioni__c from contact where id in: studId];
    for(Iscrizione__c isc : IscrizioneList){
    for(Contact con : lstCoustumObject){
        if(isc.contact__c != NULL){
            con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c +1;
        }else{
            con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c -1;   
        }
    }
}
    update lstCoustumObject;
}

}

Please mark my answer as it will help to close this request.


    

All Answers

SwethaSwetha (Salesforce Developers) 
HI Vittorio,
Your ask looks similar to https://developer.salesforce.com/forums/?id=9060G000000XhhYQAS . You can take the code snippet as example to get started.
 
IqbalIqbal
Hi Vittorio Luca Monaco,

There is nothing wrong with your logic just a minor change is required. According to salesforce, you cannot use trigger.new in delete context. 

trigger ContaIscr on Iscrizione__c (after insert ,after delete) {

if(trigger.isAfter && trigger.isInsert){
       ContalscrHelper.checkMethod(trigger.new);
}
else if(trigger.isAfter && trigger.isDelete){
       ContalscrHelper.checkMethod(trigger.old);
}


public class ContalscrHelper{

public static void checkMethod(list<Iscrizione__c > IscrizioneList){
set<id> studId = new set<id>();
    for(Iscrizione__c a : IscrizioneList){
        studId.add(a.Contact__c);
    }
    list<Contact> lstCoustumObject = [Select id, totale_iscrizioni__c from contact where id in: studId];
    for(Iscrizione__c isc : IscrizioneList){
    for(Contact con : lstCoustumObject){
        if(isc.contact__c != NULL){
            con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c +1;
        }else{
            con.Totale_iscrizioni__c = con.Totale_Iscrizioni__c -1;   
        }
    }
}
    update lstCoustumObject;
}

}

Please mark my answer as it will help to close this request.


    
This was selected as the best answer