+ Start a Discussion
dindevdindev 

how can i pass data one object data to another object

I have two objects obj1 and obj2
having same fields like Name,phone when i enter Name phone values in obj1 after saving that will be shown obj2 as well how can achieve this
Raju yadavRaju yadav
Hi dindev,
You just need to write a trigger that copy object1 in object2.
Deepali KulshresthaDeepali Kulshrestha
Hi dindev,

This scenario can be done by using Triggers

Let Us consider
Object 1  - Account
Object 2 - contact
Write trigger on account object(after insert)
TRIGGER:
Trigger objecttgr on Account(after insert){
List<contact> cons =new List<contact>();
 for(Account a :trigger.new){
   contact c  =new contact();
   c.lastname = a.name;
   c.phone = a.phone;
// we can match the fileds of both objects, what are required fileds u want before going to insert of contact
c.accountid = a.id;
 cons.add(c);
 

}
insert cons;
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.


 Thanks and Regards,
 Deepali Kulshrestha


 
Ajay K DubediAjay K Dubedi
Hi Dindev,
Try this code:
trigger:
trigger CopyRecordAccountToContact on Account (before insert) {
        if(trigger.IsInsert && trigger.IsBefore) {
            CopyRecordAccountToContact_Handler.copyRecord(trigger.new);
        }
 }
handler:
public class CopyRecordAccountToContact_Handler {
    public static void copyRecord(List<Account> accList) {
        List<Contact> conList = new List<Contact>();
        try {
            if(accList.size() > 0) {
                for (Account ac : accList) {
                    Contact con = new Contact();
                    con.LastName = ac.Name;
                    con.Phone = ac.Phone;
                    conList.add(con);
                }
                insert conList;
            }
        } catch (Exception e)    
        {
            system.debug('------>>>'+e.getCause() +e.getLineNumber() + e.getMessage());
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi