+ Start a Discussion
HARSH saini 6HARSH 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;
}
Best Answer chosen by HARSH saini 6
Sitanshu TripathiSitanshu Tripathi
Dear Harsh,

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

Kalyani Jagdale 1Kalyani Jagdale 1

Try to assign id of the object to each other

i.e s.id = h.India__c

GauravendraGauravendra
Hi Harsh,

Are you inserting all the required fields of san_francisco__c object. Be sure you are not missing any required field of this object.
brijender singh rathore 16brijender singh rathore 16
ADD THIS
s.india__c = h.id;
Sitanshu TripathiSitanshu Tripathi
Hi HARSH,
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
HARSH saini 6HARSH saini 6
Hi Sitanshu Tripathi, please write the test clas for above trigger .
Sitanshu TripathiSitanshu Tripathi
Dear Harsh,
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,
HARSH saini 6HARSH saini 6
Hey Sitanshu Tripathi,  above code dont have any mistake ,its correct now & its working fine. you can write test class for this trigger .so please write test class fast,I will be waiting for you. please reply fast.
thanks.
Sitanshu TripathiSitanshu Tripathi
Dear Harsh,

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
This was selected as the best answer
HARSH saini 6HARSH saini 6
Thankyou,Sitanshu Tripathi . I written test class and its working fine. I think in future, i needs your guidline.
Sitanshu TripathiSitanshu Tripathi
That is your greatness Harsh.
HARSH saini 6HARSH saini 6
Hey sitanshu tripathi. In above same trigger i want if i update a field on one object then it will automatically be updated on another object.please check the code below:

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;
    }
}}

 
HARSH saini 6HARSH saini 6
for updation the above code is not working.
Sitanshu TripathiSitanshu Tripathi
Dear Harsh,
Will You please post a new question ?
HARSH saini 6HARSH saini 6
if you can then please check the above code.
HARSH saini 6HARSH saini 6
trigger trigger52 on India__c (after insert,before 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){
    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;
}}