+ Start a Discussion
SFDC ROCKSFDC ROCK 

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


Thanks
Subodh
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.

Regards,
Anutej
SFDC ROCKSFDC ROCK
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:

>>https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

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
Hi SFDC ROCK,

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.
 
Chandu007Chandu007
You can write formula field if they have relation between them.
SFDC ROCKSFDC ROCK
@Sanjay,

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

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){
     picklistFieldSet.add(objB.picklistField);
}

for(ObjectB objb : [Select Id, PicklistField From ObjectB Where PickListField 
                              IN:picklistFieldSet]){
     objBMap.put(objB.PickListField,objb);
}

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

If(objectBList.size() > 0){
    insert objectBList;
}
Let me know if you have any doubt.