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
Phuc Nguyen 18Phuc Nguyen 18 

update fields from other object

Hello All,
I am hoping you can help me with this issue.
I have 2 custom objects.
Custom object A has fields quatity and estimate
Custom object B has field price.
Custom object A has a Lookup to Custom Objetc B.
So when a user enters a quantity on a custom object A fields, I need to go to custom object B and grab the price for those fields.  Bring it back and multiply the value retrieved by the quantity to get the estimate.
So in the Class(already have a trigger and other classes for theis object) what is the best approach to make sure the update works not only on single record edits but during large data loads .  My issue is that a user can edit up to 20 fields on record A and I am not sure how to update all of them all at once.
Thank you,
P
Best Answer chosen by Phuc Nguyen 18
David Zhu 🔥David Zhu 🔥
I would add a BEFORE INSERT and BEFORE UPDATE trigger on objectA. You may refer to the code below.
list<objectA__c> objectAs = trigger.new;  //assign to list, you need to modify according to your code.
List<Id> objectBIDs = new List<Id>();
for (objectA__c a: objectAs)
{
   objectBIDs.add(a.objectB__c);   
}

if (objectBIDs.size()>0)
{
    map<Id,ObjectB__c> objectBMap = new map<Id,objectB__c>([select ID,price__c from objectB__c where id in :objectBids]);

    for (objectA__c a: objectAs)
    {
       if (objectBmap.containskey(a.objectB__c))
       {
         a.estimate__c = a.quantity__c * objectBmap.get(a.objectB__c).price__c;
       }
    }
}



 

All Answers

Maya CroftMaya Croft
Im pretty sure you can find a especific tutorial for that in Youtube. If you dont find it try to search at pelicula online (http://images.google.com.kh/url?sa=t&url=https%3A%2F%2Flearning.cmu.edu%2Feportfolios%2F4057%2FPgina_de_Inicio%2FAvengers_Endgame_Film_2020)
ShirishaShirisha (Salesforce Developers) 
Hi Phuc,

Greetings!

You can create the Estimate field as Formula field and get the value from the parent to calculate the total as per your requirement.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Phuc Nguyen 18Phuc Nguyen 18
Thank you both for your reply.  I was trying not to use formula fields if at all possible. I was hoping I could query all of the records in Object B initially and then  iterate throught the list and grab the associated object B record and fields.  This way when multiple object A are updated we only query the Object B records once.
Thanks,
P
David Zhu 🔥David Zhu 🔥
I would add a BEFORE INSERT and BEFORE UPDATE trigger on objectA. You may refer to the code below.
list<objectA__c> objectAs = trigger.new;  //assign to list, you need to modify according to your code.
List<Id> objectBIDs = new List<Id>();
for (objectA__c a: objectAs)
{
   objectBIDs.add(a.objectB__c);   
}

if (objectBIDs.size()>0)
{
    map<Id,ObjectB__c> objectBMap = new map<Id,objectB__c>([select ID,price__c from objectB__c where id in :objectBids]);

    for (objectA__c a: objectAs)
    {
       if (objectBmap.containskey(a.objectB__c))
       {
         a.estimate__c = a.quantity__c * objectBmap.get(a.objectB__c).price__c;
       }
    }
}



 
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Hello David,
Thank you for the reply.  Is the code the same for before insert and before update?  In my class it is framed where I can seperate out if the action is a before update or before insert.... 
Also, there is 15 fields that could possibly be updated.  Should I add those fields to a another list and then do an update outside the for loop?

Also, I saw an example that looked like this where they where copying fields from one object to another but I have never worked with schema before.  Are you familiar with this syntax.  Is it effecient?
syncPNWtoProjField(
            oldvalue,
            newvalue,
            m_RelatedProjects.get(newProject.Project__c),            
            Schema.Warehouse__c.Structural_Analysis_c,
            Schema.Project__c.Structural_Submitted__c
        );         

private void syncWaretoProjField(Warehouse__c oldvalue, Warehouse__c newvalue, Project__c relatedProjectId, Schema.sobjectField wareField, Schema.sobjectField projectField){

Thank you,
P
David Zhu 🔥David Zhu 🔥
1. Yes, the code is for both Insert and update.
If you seperate them, you can create a static trigger handler and call the handler in different places. or you can use cod like;

if (trigger.isbefore && (trigger.isInsert || trigger.isUpdate))
{
 //call the method
}

2. for multipe fields processing, you may use code like below. You usually don't have to use a separate loop.
if (objectBIDs.size()>0)
{
    map<Id,ObjectB__c> objectBMap = new map<Id,objectB__c>([select ID,price__c,field1,field2,field3 from objectB__c where id in :objectBids]);

    for (objectA__c a: objectAs)
    {
       if (objectBmap.containskey(a.objectB__c))
       {
         a.estimate__c = a.quantity__c * objectBmap.get(a.objectB__c).price__c;
         a.anotherfield1 = a.xxxx * objectBmap.get(a.objectB__c).field1;
         a.anotherfield2 = a.yyyy* objectBmap.get(a.objectB__c).field2;
       }
    }
}


 
Phuc Nguyen 18Phuc Nguyen 18
Hey David,
Will you elaborate on what you mean by 'you need to modify according to your code.'
Also, will you annotate on the code?  This way I(and others reading  this post) can better understand what each line of code is doing. 
Thanks for your time and help,
P
Phuc Nguyen 18Phuc Nguyen 18
Hey David,
Also, in my Class the methods have refrences to the previous and new vlaues of the object.  Was not sure if that helps.
global void beforeUpdate( SObject oldSo, SObject newSo )

 
David Zhu 🔥David Zhu 🔥
The method signature seems strange. When the method is called in Trigger, the parameters should be List<sObject>. By using sObject, it cannot handle multiple record at a time.
I would modify the handler method code:
global void beforeUpdate( List<SObject> oldSo, List<SObject> newSo )

When it is called in trigger, it would be:

SObjectClassHandler.beforeUpdate(trigger.Old,trigger.New);
Phuc Nguyen 18Phuc Nguyen 18
Thanks for the reply David.  Could I add a list to the method? Trying to work inside a set framework.
global void beforeUpdate( SObject oldSo, SObject newSo ){
objectA__c nObjA = (objectA__c )newso;

list<objectA__c> objectAs = trigger.new; 
List<Id> objectBIDs = new List<Id>(); 
for (objectA__c a: objectAs) { objectBIDs.add(a.objectB__c); } 
if (objectBIDs.size()>0) { map<Id,ObjectB__c> objectBMap = new map<Id,objectB__c>([select ID,price__c from objectB__c where id in :objectBids]); for (objectA__c a: objectAs) 
{ 
if (objectBmap.containskey(a.objectB__c)) { a.estimate__c = a.quantity__c * objectBmap.get(a.objectB__c).price__c; } 
} }
Phuc Nguyen 18Phuc Nguyen 18
David, would add some comments in the code.  Especially for the mapping.  Understand what the code is doing would be very  benificial.
Thank you,
P
David Zhu 🔥David Zhu 🔥
Unfortunately, the code snippet I provided is very straight forward and it takes too much time to explain each line in writting.

Back to you the second last post, if you use the code in the trigger directly, you can do assignment "list<objectA__c> objectAs = trigger.new; ".
If using triggerhanlder  class, you cannot as the method beforeUpdate won't understand what trigger.new is.
 
trigger xx on objectA__c (after update)
{

   list<objectA__c> objectAs = trigger.new; 
   List<Id> objectBIDs = new List<Id>(); 
   for (objectA__c a: objectAs) 
  { 
     objectBIDs.add(a.objectB__c);
   } 
  if (objectBIDs.size()>0) 
  {
      map<Id,ObjectB__c> objectBMap = new map<Id,objectB__c>  ([select ID,price__c from objectB__c where id in :objectBids]); for (objectA__c a: objectAs) 
      { 
       if (objectBmap.containskey(a.objectB__c)) 
        { 
            a.estimate__c = a.quantity__c *   objectBmap.get(a.objectB__c).price__c;
         } 
       } 
}
Phuc Nguyen 18Phuc Nguyen 18

Hey David if you have time, would you look at https://developer.salesforce.com/forums/ForumsMain?id=9062I000000Qxk2QAC? 
I am trying to match field names from 1 object to record names on another object.  
Thank you,
P