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 

How to map custom fields with Apex Triggers?

I need help on creating an apex trigger that maps one custom object currency field to another custom object currency field.

IF  (ObjectA.CustomFieldA != NULL)
Then  (ObjectB.CustomFieldB = ObjectA.CustomFieldA)
Else (ObjectB.CustomFieldB = 0.00)
 
Emmanuel Cruz BEmmanuel Cruz B
Hi Gerald,

I think, I'm not getting you at all, could you give me a little bit more of information?, like:
Which object is executing the trigger?
What is the relationship between ObjectA and ObjectB? (Lookup from ObjA to objB or viceversa)
As a quick possible solution is:
Create a Lookup from ObjB to ObjA
From the ObjA trigger create a map with Trigger.newMap (map<Id, ObjectA__c>)
Also create a set of ObjectA's Ids with a for loop
Get a list of ObjectB records with a SOQL filtering with the ObjA's set
Create a for loop to create a map of ObjectB (map<string, ObjectB>) where the string will be the ObjectA lookup field value
Do another for loop with the trigger.new where your If statement is going to be:
view sourceprint?
list<ObjectB__c> ObjBToUpdate = new list<ObjectB__c>();
for(ObjectA__c oA :Trigger.new){
  ObjectB__c ObjB = new ObjectB__c();
  ObjB.Id = ObjBMap.get(oA.Id).Id;
  IF(oA.CustomFieldA__c != NULL){
    ObjB.CustomFieldB__c = oA.CustomFieldA__c;
  }ELSE{
    ObjB.CustomFieldB__c = 0.00;
  }
  ObjBToUpdate.add(ObjB);
}


Update the list pf ObjectB records.
Hope this helps you.

Emmanuel Cruz
 
Gerald HarmensGerald Harmens
Hi Emmanuel, thank you for the reply. I tried your suggestion and I updated it a bit. I am now getting this error: 
Error: Compile Error: Variable does not exist: ProspectMap at line 5 column 17
trigger Appointment_Quoted_Price on i360__Appointment__c (before insert) {
List<ID> ProspectIds = New List<ID>();
for(i360__Appointment__c o : Trigger.new){
  i360__Prospect__c prospect = new i360__Prospect__c();
  Prospect.Id = ProspectMap.get (prospect.Id).Id;
  IF(o.i360__Quoted_Amount__c != NULL){
    prospect.Appointment_Quoted_Price__c = o.i360__Quoted_Amount__c;
  }ELSE{
    prospect.Appointment_Quoted_Price__c = 0.00;  
  }
  prospectToUpdate.add(prospect);
}
}

 
Emmanuel Cruz BEmmanuel Cruz B
Hi Gerald,

You haven't created the map there "ProspectMap", you have to create that one from a list of i360__Prospect__c which you will get from the set of i360__Appointment__c' ids, here a better approach:
 
trigger Appointment_Quoted_Price on i360__Appointment__c (after insert) 
{
    if(Trigger.isAfter)
    {
        if(Trigger.isInsert)
        {
            // Get set of Ids from all appointments inserted
            set<Id> AppointmentSet = new set<Id>();
            for(i360__Appointment__c Appointment :Trigger.new){
                AppointmentSet.add(Appointment.Id);
            }
            // Get list of prospects to modify and create a map
            map<string, i360__Prospect__c> ProspectMap = new map<string, i360__Prospect__c>();
            list<i360__Prospect__c> ProspectList = [SELECT Id, Appointment_Quoted_Price__c, i360__Appointment__c FROM i360__Prospect__c WHERE i360__Appointment__c IN :AppointmentSet];
            for(i360__Prospect__c Prospect :ProspectList){
                ProspectMap.put(Prospect.i360__Appointment__c, Prospect);
            }
            // Modify prospects and creatte a list to update
            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.Id).Id;
                if(Appointment.i360__Quoted_Amount__c != NULL){
                    prospect.Appointment_Quoted_Price__c = Appointment.i360__Quoted_Amount__c;
                }else{
                    prospect.Appointment_Quoted_Price__c = 0.00;
                }
                ProspectsToUpdate.add(prospect);
            }
            // Update prospects related
            if(ProspectsToUpdate.size() > 0){
                update ProspectsToUpdate;
            }
        }
    }
}

Be aware that you need to create the lookup field i360__Appointment__c on i360__Prospect__c and this field should contain a record related.
Since you are using ids, your trigger need to change to after insert. I also recommend to add a line to verify if the prospect is on the map to avoid an error.

Let me know how it goes.
Gerald HarmensGerald Harmens
Hi Emmanuel, I created lookup field i360__Appointment__c on i360__Prospect__c and Related To i360__Appointment__c. I don't know what you mean when you say, "this field should contain a record related."

I'm getting this error:

Error: Compile Error: No such column 'i360__Appointment__c' on entity 'i360__Prospect__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 14 column 52
Emmanuel Cruz BEmmanuel Cruz B
Hi Gerald, make sure that you are using the API name in your code. Go to Setup > Build > Create > Objects > i360__Prospect__c and copy the API name from the lookup field i360__Appointment__c.

And with "record related" I mean that the Prospect record should point to any Appointment record in that field, for example you have appointmentA and PropectB, if PropestB is not pointing to AppointmentB, ProspectB will never get the value that you are looking for from Appointment.

In the other hand, I realize that the logic that we are creating is not  going to work, because you need to create the lookup field in Appointment insteat of in Prospect, because you are creating the appointment assuming that the prospect already have a relationship with the appointment, but this can be possible since the appointment is just going to be created. Sorry for that!! :(

Your trigger will finally look like this.
 
trigger Appointment_Quoted_Price on i360__Appointment__c (after insert) 
{
    if(Trigger.isAfter)
    {
        if(Trigger.isInsert)
        {
            // Get set of Ids from all prospects related
            set<Id> ProspectSet = new set<Id>();
            for(i360__Appointment__c Appointment :Trigger.new){
                ProspectSet.add(Appointment.i360__Prospect__c);
            }
            // Get list of prospects to modify
            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]
            );
            // Modify prospects and create a list to update
            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){
                    prospect.Appointment_Quoted_Price__c = Appointment.i360__Quoted_Amount__c;
                }else{
                    prospect.Appointment_Quoted_Price__c = 0.00;
                }
                ProspectsToUpdate.add(prospect);
            }
            // Update prospects related
            if(ProspectsToUpdate.size() > 0){
                update ProspectsToUpdate;
            }
        }
    }
}

You need to delete the first lookup field in Prospect and create a new one but this time in appointment. Make sure to use the correct APi names for the fields, the API name for this field need to be i360__Prospect__c.
 
Gerald HarmensGerald Harmens
Thanks Emmanuel! The trigger saved with no errors but was mapping $0.00. I took the Else statement out and it mapped a blank value.
Emmanuel Cruz BEmmanuel Cruz B
Cool!, Is that what you were looking for?
Gerald HarmensGerald Harmens
Hi Emmanuel, not quite.

prospect.Appointment_Quoted_Price__c = $0.00 no matter what I enter in Appointment.i360__Quoted_Amount__c.

So if I enter $100.00 in Appointment.i360__Quoted_Amount__c, 
prospect.Appointment_Quoted_Price__c
= $0.00
Emmanuel Cruz BEmmanuel Cruz B
Ok, can you please add some system.debug to your code?
Please add system.debug(ProspectSet) after line 11
Also add system.debug(ProspectMap) after line 17
Try verifying the if statement adding a system.debug(Appointment.i360__Quoted_Amount__c​) before line 23
Finally debug the propest list adding system.debug(ProspectsToUpdatebefore line 31

Copy your debug log and paste it here to see what is happening.
Gerald HarmensGerald Harmens
Which debug log?
User-added image

Here is the current code:
trigger Appointment_Quoted_Price on i360__Appointment__c (after insert) 
{
    if(Trigger.isAfter)
    {
        if(Trigger.isInsert)
        {
            // Get set of Ids from all prospects related
            set<Id> ProspectSet = new set<Id>();
            for(i360__Appointment__c Appointment :Trigger.new){
                ProspectSet.add(Appointment.i360__Prospect__c);
            }
            system.debug(ProspectSet);
            // Get list of prospects to modify
            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]
            );
            system.debug(ProspectMap);
            // Modify prospects and create a list to update
            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;
                system.debug(Appointment.i360__Quoted_Amount__c); 
                if(Appointment.i360__Quoted_Amount__c != NULL){
                    prospect.Appointment_Quoted_Price__c = Appointment.i360__Quoted_Amount__c;
                }
                ProspectsToUpdate.add(prospect);
            }
            // Update prospects related
            if(ProspectsToUpdate.size() > 0){
                update ProspectsToUpdate;
                system.debug(ProspectsToUpdate);
                            }
        }
    }
}

 
Emmanuel Cruz BEmmanuel Cruz B
I think you have a lot of debugs in your ORG. Try with the developer console > Debug > Open Execute Anonymous Window
With apex code, create a Prospect, then create an Appointment related to the Prospect and then click the log tab and open the last executed operation. Finally click the debug only checkbox, copy and paste the code here.