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 

Change apex trigger to update Map Values.

Hello,

I need help changing the code below to update Map Values. The code below creates a new instance of the prospect at line 18 and adds it to our prospect list which is causing this error:

i360.ProspectTriggers: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0416000010g4SdAAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Appointment_Price_Quoted: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: a0Z1600000AQwFjEAL Trigger.Appointment_Price_Quoted: line 28, column 1: [] Class.i360.ProspectUtilities.UpdateProspectNames: line 1509, column 1 Class.i360.Prospect_Trigger_Utilities.ProspectChangeName: line 175, column 1 Trigger.i360.ProspectTriggers: line 22, column 1
trigger Appointment_Price_Quoted on i360__Appointment__c (after update) 
{
    if(Trigger.isAfter)
    {
        if(Trigger.isupdate)
        {
            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__Quoted_Amount__c != NULL && Appointment.i360__Prospect__c != null){
                    prospect.Appointment_Quoted_Price__c = Appointment.i360__Quoted_Amount__c;
                }else{
                    prospect.Appointment_Quoted_Price__c = 0.00;
                }
                ProspectsToUpdate.add(prospect);
            }
            if(ProspectsToUpdate.size() > 0){
                update ProspectsToUpdate;
            }
        }
    }
}


 
Anupama SamantroyAnupama Samantroy
Hi Gerald,

I assume there are multiple Appointments with the same i360__Prospect__c.  And at line 25 where you are adding the prospect it's adding duplicate to the list.
For example lets consider  Appointment 1 ->Prospect 1 and also Appointment 2--> Prospect 1. Then the map has only 1 record. But when you iterate through the trigger.new you are adding Prospect 1 twice in the list and trying to update it.
You need to check which Appointment.i360__Quoted_Amount__c value needs to be populated to Prospect. If any then you can create a set of prospect instead of List.
Please let me know if you have any questions.

Thanks
Anupama  
Gerald HarmensGerald Harmens
Thanks Anupama! How would I do that? I'm very new at this.
Anupama SamantroyAnupama Samantroy
Hi Gerald,
Try the below code but keep in mind that it will take the last matching appointment's Quoted Amount. This isn't the best solution. You need to analyse the requirement carefully.

trigger Appointment_Price_Quoted on i360__Appointment__c (after update) 
{
    if(Trigger.isAfter)
    {
        if(Trigger.isupdate)
        {
            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]
            );
            set<i360__Prospect__c> ProspectsToUpdate = new set<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__Quoted_Amount__c != NULL && Appointment.i360__Prospect__c != null){
                    prospect.Appointment_Quoted_Price__c = Appointment.i360__Quoted_Amount__c;
                }else{
                    prospect.Appointment_Quoted_Price__c = 0.00;
                }
                ProspectsToUpdate.add(prospect);
            }
            if(ProspectsToUpdate.size() > 0){
                update ProspectsToUpdate;
            }
        }
    }
}

Thanks
Anupama
Gerald HarmensGerald Harmens
Thanks Anupama, I tried that vesion earlier when you mentioned replacing list with set an got this error:

Error: Compile Error: DML requires SObject or SObject list type: Set<i360__Prospect__c> at line 28 column 17
trigger Appointment_Price_Quoted on i360__Appointment__c (after update) 
{
    if(Trigger.isAfter)
    {
        if(Trigger.isupdate)
        {
            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]
            );
            set<i360__Prospect__c> ProspectsToUpdate = new set<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__Quoted_Amount__c != NULL && Appointment.i360__Prospect__c != null){
                    prospect.Appointment_Quoted_Price__c = Appointment.i360__Quoted_Amount__c;
                }else{
                    prospect.Appointment_Quoted_Price__c = 0.00;
                }
                ProspectsToUpdate.add(prospect);
            }
            if(ProspectsToUpdate.size() > 0){
                update ProspectsToUpdate;
            }
        }
    }
}

 
Anupama SamantroyAnupama Samantroy
Hi Gerald,

Sorry about that. You can add all items to another list and try to update. This should work now.
List<i360__Prospect__c> lstProspectsToUpdate = new List<i360__Prospect__c>()
if(ProspectsToUpdate.size() > 0){
    lstProspectsToUpdate .addAll(ProspectsToUpdate);
    update lstProspectsToUpdate ;
}
Thanks
Anupama
Anupama SamantroyAnupama Samantroy
Hi Gerald,

If that worked please mark the answer as solution or else let me know the issues ,happy to help.

Thanks
Anupama
Gerald HarmensGerald Harmens
Thank you! Below is the updated code based on you last instructions. We are now getting this error:

Error: Compile Error: Variable does not exist: ProspectsToUpdate at line 28 column 34
trigger Appointment_Price_Quoted on i360__Appointment__c (after update) 
{
    if(Trigger.isAfter)
    {
        if(Trigger.isupdate)
        {
            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> lstProspectsToUpdate = 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__Quoted_Amount__c != NULL && Appointment.i360__Prospect__c != null){
                    prospect.Appointment_Quoted_Price__c = Appointment.i360__Quoted_Amount__c;
                }else{
                    prospect.Appointment_Quoted_Price__c = 0.00;
                }
                ProspectsToUpdate.add(prospect);
            }
            if(ProspectsToUpdate.size() > 0){
    lstProspectsToUpdate .addAll(ProspectsToUpdate);
    update lstProspectsToUpdate ;
            }
        }
    }
}

 
Anupama SamantroyAnupama Samantroy
Hi Gerald,

We have to create one set and one list.
trigger Appointment_Price_Quoted on i360__Appointment__c (after update) 
{
    if(Trigger.isAfter)
    {
        if(Trigger.isupdate)
        {
            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]
            );
            Set<i360__Prospect__c> ProspectsToUpdate = new Set<i360__Prospect__c>();
            List<i360__Prospect__c> lstProspectsToUpdate = new List<i360__Prospect__c>();</b>
            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__Quoted_Amount__c != NULL && Appointment.i360__Prospect__c != null){
                    prospect.Appointment_Quoted_Price__c = Appointment.i360__Quoted_Amount__c;
                }else{
                    prospect.Appointment_Quoted_Price__c = 0.00;
                }
                ProspectsToUpdate.add(prospect);
            }
            if(ProspectsToUpdate.size() > 0){
    lstProspectsToUpdate .addAll(ProspectsToUpdate);
    update lstProspectsToUpdate ;
            }
        }
    }
}

Thanks
Anupama
Gerald HarmensGerald Harmens
Hi Anupama, still getting this error:

Error:
i360.ProspectTriggers: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0416000014azn3AAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Appointment_Price_Quoted: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: a0Z1600000BfR8lEAF (): [] (i360)