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
Gerald HarmensGerald Harmens 

Child to Parent Apex Trigger Not Working

Hello, I have the Apex Trigger below that is updating the Else statement on the parent object (Prospect) as soon as the child record (Appointment) is created. I need the parent field (Appointment_Quoted_Price__c) to update when the IF statement is true. Your feedback is greatly appreciated.
trigger Appointment_Price_Quoted on i360__Appointment__c (after insert) 
{
    if(Trigger.isAfter)
    {
        if(Trigger.isInsert)
        {
            set<Id> ProspectSet = new set<Id>();
            for(i360__Appointment__c Appointment :Trigger.new){
                ProspectSet.add(Appointment.i360__Prospect__c);
            }
            map<string, i360__Prospect__c> ProspectMap = new map<string, i360__Prospect__c>(
               [SELECT Id, Appointment_Quoted_Price__c 
               FROM i360__Prospect__c
                WHERE Id IN :ProspectSet]
            );
            list<i360__Prospect__c> ProspectsToUpdate = new list<i360__Prospect__c>();
            for(i360__Appointment__c Appointment : Trigger.new){
                i360__Prospect__c prospect = new i360__Prospect__c();
                Prospect.Id = ProspectMap.get(Appointment.i360__Prospect__c).Id;
                if((Appointment.i360__Result__c == 'Demoed, Not Sold' || Appointment.i360__Result__c == 'Sold') &&  Appointment.i360__Prospect__c != null){
                    prospect.Appointment_Quoted_Price__c = Appointment.Quoted_Amount__c;
                }else{
                    prospect.Appointment_Quoted_Price__c = 0.00;
                }
                ProspectsToUpdate.add(prospect);
            }
            if(ProspectsToUpdate.size() > 0){
                update ProspectsToUpdate;
            }
        }
    }
}

 
Rohit Sharma 66Rohit Sharma 66
can you please debug your code, add the system.debug after each statement and see what value u r getting.
Also try putting 1 condition at a time in your if statement that will help u finding which of your condition not working.
 
JeffreyStevensJeffreyStevens
Ya - I'd verify the value of Appointment.i360__Result__c with a debug statement after line #19
amol salveamol salve
Hi Gerald Harmens,
     
      As per above code you were created one map with 'string(Id) as key and 'i360__Prospect__c' as value but when you enter into for loop at line number 18, you have created the new instance of an object and assign Id  and Quoted_Amount__c value from i360__Appointment__c  to Appointment_Quoted_Price__c of new instance or 0.00 value to  Appointment_Quoted_Price__c of new instance and add this list to new list named as ProspectsToUpdate.
    Problem is, you query on an object and stored retrieve records to the map and update the new list which is not possible. If you want to update records then you must have to update  ProspectsToUpdate.value() because you have made changes on records which is present in ProspectsToUpdate but for that you have to change your code.
    I have one simple approach
    
    trigger Appointment_Price_Quoted on i360_Appointment__c (after insert) {
    if(trigger.isAfter){
        if(trigger.isInsert){
            set<Id> AppointmentSet = new set<Id>();
            set<Id> ProspectSet = new set<Id>();
            for( i360_Appointment__c Appointment : trigger.new){
                AppointmentSet.add(Appointment.Id);
                ProspectSet.add(Appointment.i360_Prospect__c);
            }
            list<i360_Prospect__c> prospectList = new list<i360_Prospect__c>([SELECT Id, 
                                                                              Appointment_Quoted_Price__c, 
                                                                                  (SELECT Id, 
                                                                                   i360_Result__c, 
                                                                                   i360_Prospect__c, 
                                                                                   Quoted_Amount__c 
                                                                                   FROM i360_Appointment__r WHERE id IN : AppointmentSet)
                                                                              FROM i360_Prospect__c WHERE id IN :ProspectSet]);
            for(i360_Prospect__c prospectObj : prospectList){
                for(i360_Appointment__c appointmentObj : prospectObj.i360_Appointment__r){
                    if((appointmentObj.i360_Result__c == 'Demoed, Not Sold' || appointmentObj.i360_Result__c == 'Sold') && appointmentObj.i360_Prospect__c != null){
                        prospectObj.Appointment_Quoted_Price__c = appointmentObj.Quoted_Amount__c;
                    }
                    else{
                        prospectObj.Appointment_Quoted_Price__c = 0.00;
                    }
                }
            }
            if(prospectList!= null && !prospectList.isEmpty()){
                try { 
                    database.saveResult [ ] updateResult = database.update (prospectList,false);
                }
                catch ( DMLException e ) {
                }
            }
        }
    }
}

Thank you,
Amol B.Salve