Saturday, 11 August 2012

copy field values of one object to another object

Normal Logic:
trigger insertMember2 on Member__c (after insert) {
 Set<Id> Ids= new Set<Id>();
    for (Member__c member :
 List<Member__c> memberList = new List<Member__c>([Select Id,FirstName__c,LastName__c  From Member__c e where Id in :Ids]);

 for(Member__c temp : memberList )
  Member2__c member2 = new Member2();
  member2.FirstName__c = temp.FirstName__c;
  member2.LastName__c = temp.LastName__c;
  insert member2;

Good Logic:
* Trigger should be like this and will work for bulk data as well as insert statement is used out side the for loop

 List<Member2__c> listMember2ObjToInsert = new List<Member2__c>();
 for (Member__c memberObj :
        Member2__c member2Obj = new Member2();
 member2Obj.FirstName__c = memberObj.FirstName__c;
 member2Obj.LastName__c = memberObj.LastName__c;
        // If you have any refernce of member in member2 you should fill that as well
insert listMember2ObjToInsert;

1 comment:

  1. Hi srinu
    I am Learner sfdc
    This trigger is Compilation is successful but not fired..

    pls rply me