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
Hari N 20Hari N 20 

Trigger to insert data from one Object to another Object

Hi,
I have two objects. Object1 and Object2.
I have 10 fields in object1. After creating a record in object1, I want to create a record in Object2.
I want only 3 field values from Object1.
If anyone have code syntax, please share.

Thanks in Advance

Regards,
Hari
Best Answer chosen by Hari N 20
Mahesh K 22Mahesh K 22

Hi hari
This senario 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;
//slly 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;
}
 

All Answers

Harish RamachandruniHarish Ramachandruni
Hi,

Replace in below code to your object api names write on object 1 .
trigger AccountTrigger on object1(Before insert)
{


list<object2>  obj2 =  new list<object2> ()

for(object1 a : Trigger.New)
{

    object2 cont = new object2();
    cont.Name = a.Name ;

// add your field aboe for mate cont.Objectfirld = a.object1 filed 

obj2 .add(cont);


}

insert  obj2 

}

Any issue ask me .


Regards ,
Harish.R
Mahesh K 22Mahesh K 22

Hi hari
This senario 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;
//slly 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;
}
 

This was selected as the best answer
Hari N 20Hari N 20
Hi,
How to compare today i.e., Friday with some field in IF loop
Pankaj Yadav 16Pankaj Yadav 16
I am getting below error while running the problem because last name field is mandatory
Pankaj Yadav 16Pankaj Yadav 16
I am getting above error
while runing the problem because last name field is mandatory