You need to sign in to do that
Don't have an account?
HARSH saini 6
Trigger to insert data from one object to another.My code is not working , can anyone tell me what's the mistake in my code.
trigger trigger52 on India__c (after insert) {
list<san_francisco__c> con =new list<san_francisco__c>();
for(India__c h : trigger.new){
san_francisco__c s = new san_francisco__c();
s.Name=h.Name;
s.Company__c=h.Company__c;
s.Mobile__c=h.Mobile__c;
s.Email__c=h.Email__c;
con.add(s);
}
insert con;
}
list<san_francisco__c> con =new list<san_francisco__c>();
for(India__c h : trigger.new){
san_francisco__c s = new san_francisco__c();
s.Name=h.Name;
s.Company__c=h.Company__c;
s.Mobile__c=h.Mobile__c;
s.Email__c=h.Email__c;
con.add(s);
}
insert con;
}
I'm telling you only Way from this link.
Create Test class from yourself.
https://sitanshusfdc.blogspot.in/2018/02/apex-trigger-with-helper-class-and-test.html
All Answers
Try to assign id of the object to each other
i.e s.id = h.India__c
Are you inserting all the required fields of san_francisco__c object. Be sure you are not missing any required field of this object.
s.india__c = h.id;
Actually when you want to populate one object field to other object's field then the relationship should must as lookup field and the first object record should be commit to the database.
I'm sure it will work when you understand these points.
1. This is not working because the 'India__c' object record not commit in your database.
2. So first insert the 'India__c' record and then use your code.
If you understand this thing and want a sample code, then please mark as best answer.
And give me your Email, I'll send you trigger code.
Thanks & Best wishes,
Sitanshu
First of all Thanks for this "Best Answer chosen by HARSH saini 6".
First Thing is that your code is not working, so I suggest to you that please create a new post with your updated code.
After that if your code is working then I'll test in my org and create a test class for you.
Thanks & kind Regard,
thanks.
I'm telling you only Way from this link.
Create Test class from yourself.
https://sitanshusfdc.blogspot.in/2018/02/apex-trigger-with-helper-class-and-test.html
trigger trigger52 on India__c (after insert,after update) {
if (trigger.isinsert){
list<san_francisco__c> con =new list<san_francisco__c>();
for(India__c h : trigger.new){
san_francisco__c s = new san_francisco__c();
s.Name=h.Name;
s.Company__c=h.Company__c;
s.Mobile__c=h.Mobile__c;
s.Email__c=h.Email__c;
con.add(s);
}
insert con;
}
if (trigger.isupdate){
for(India__c h : trigger.new){
san_francisco__c s = new san_francisco__c();
s.Name=h.Name;
s.Company__c=h.Company__c;
s.Mobile__c=h.Mobile__c;
s.Email__c=h.Email__c;
update s;
}
}}
Will You please post a new question ?
if (trigger.isinsert){
list<san_francisco__c> con =new list<san_francisco__c>();
for(India__c h : trigger.new){
san_francisco__c s = new san_francisco__c();
s.Name=h.Name;
s.Company__c=h.Company__c;
s.Mobile__c=h.Mobile__c;
s.Email__c=h.Email__c;
con.add(s);
}
insert con;
}
if (trigger.isupdate){
map<id,India__c> mymap=trigger.newmap;
list< san_francisco__c> updatefield = new list< san_francisco__c>();
list< san_francisco__c> field= [select Company__c from san_francisco__c where India__c in : mymap.keyset()];
for(san_francisco__c b: field){
b.Company__c=mymap.get(b.India__cid).Company__c;
updatefield.add(b);
}
update updatefield;
}}