+ Start a Discussion

How to write a trigger to copy few field value from one object to another object ?

How to write a trigger code with handler class to copy few field value from one object (A)to another object (B) ?

Condition :1
Object A

picklist Field : Subodh            Salary =5000
picklist Field : Subodh             Salary =7000

Object B 
picklist Field : Subodh       Total Salary :12000

Condition : 2

Object : A

picklist Field : Subodh             Salary =7000
picklist Field : Rahul                Salary =7000
picklist Field : Amit                  Salary =5000
picklist Field : Rahul                Salary =7000
picklist Field : Amit                   Salary =5000

Object : B

picklist Field : Subodh            Total Salary =7000
picklist Field : Rahul               Total Salary =14000
picklist Field : Amit                  Total Salary =10000

ANUTEJANUTEJ (Salesforce Developers) 
Hi Subodh,

Generally, a trigger is written so that when ever a new record is entered or when a record is updated or deleted there could be certain operations that can be done. Can you try considering if this could be done using the OOB tools like flows or process builders so that it could be easily done.

Yes Anutej !!!
We can do OOB but need to understand with trigger only.
ANUTEJANUTEJ (Salesforce Developers) 
Generally, it would be prefered to implement the solutions with OOB tools and in case if it would not be possible to implement the scenario with OOB tools the developer tools are recommended.

However, in case if you would like to implement using the developer tools I think you should be able to do this use case using schedulable apex, for which the example is given in below documentation:


The above solution is considering the records are existing in the system and running the code at a specific interval.

In case if you want to implement to combine records and insert while inserting records, you can do something as below:

>> create a set of values of names from the incoming records and then run through the loop by adding the values and creating a list os objectb instances with name and total salaries as components and insert them after calculating the total amount.
Sanjay Bhati 95Sanjay Bhati 95

Can you post your object relation also? It will be easy to the writer the logic. Also on which object update, you want to perform this functionality.
You can write formula field if they have relation between them.

both are unrelated object and need to update or insert on B object.
Sanjay Bhati 95Sanjay Bhati 95

You can write the trigger. Here is the basic example of the logic that you need.
List<ObjectB> objectBList = new List<ObjectB>();
Map<String,ObjectB> objBMap = new Map<String,ObjectB>();
Set<String> picklistFieldSet = new Set<String>();
for(ObjectB objB : trigger.new){

for(ObjectB objb : [Select Id, PicklistField From ObjectB Where PickListField 

for(ObjectA objA : trigger.new){
     Integer salary = 0;
     ObjectB objb = new ObjectB();
          objb = objBMap.get(objA); 
     objb.Salary += objA.Salary;

If(objectBList.size() > 0){
    insert objectBList;
Let me know if you have any doubt.
Avesh LakhaAvesh Lakha
Hi Sanjay,

I know it's an old thread but will this work for my below scenario? which I feel like instead of having a picklist field I have my text field. User-added image