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
Sweta jainSweta jain 

Insert Contact Fields on another record before insert

I am having 3 objects
1 contact (Standard Object)
2 Banking
3 Customer

Banking is having lookup of contact
Customer is having lookup of Banking

I have this trigger to insert a customer record as a banking record is created

Now i want to insert the values of standard field on contact ( related to Banking via lookup  ) into text fields of Customer record on insert

like Customer.First_name__c = Contact.first name

Please suggest


trigger Banking_PopulatecustomerRecord on Banking__c (after insert){
Set<Id> IdSet = new Set<Id>();

   for(Banking__c mp:trigger.new)
   IdSet.add(mp.Id);
     
   List<Customer __c> BankingstubToCreate = new List<Customer __c>();
   for(Banking__c mp:trigger.new)
   {
    if(IdSet.size()>0 )
   {
     Customer __c s = new Customer __c();
      s.Banking__c =mp.Id ;
      mccystubToCreate.add(s);
   }
   }
   
   if(BankingstubToCreate.size()>0)
   {
    Database.SaveResult[] lsr = Database.insert( BankingtubToCreate, false);
             //insert finalCreateListOfStub  
   }
   
 
}
Virendra ChouhanVirendra Chouhan
Hi Sweta,

What is mccystubToCreate.
I think you have to write there BankingstubToCreate insted of mccystubToCreate.
Try this .
it'll work.

And on more thing If you want to insert many fields not only Banking field in Customer object then you have to write them.
like
s.Banking__c =mp.Id ;
s.first_name__C = mp.Contact.firstname;
s.email__c = mp.contact.email;
BankingstubToCreate.add(s);


Regards
Virendra
Sweta jainSweta jain
Thanks for the reply ,

But you mean to say

s.first_name__c = mp.contact__r.firstname

I tried the same but it is not working , Do i have to query fields from contact ?
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan

trigger Banking_PopulatecustomerRecord on Banking__c (after insert){
Set<Id> IdSet = new Set<Id>();
for(Banking__c mp:trigger.new)
{
   IdSet.add(mp.Id);
}
List<banking__c> banklist=[Select id,name,(select contactid,contactname from contact__r)
from Banking where ID in :Idset];
For(Banking__c b:banklist)
{
customer__c c=new Customer__C()
C.name=b.contactname
\\other fields
}
Database.insert(c);
}
Change it accordng to your fieldname i worte it in general
Sweta jainSweta jain
Thanks Vidyasagaran

But i dont wanna populate fields from banking  into customer , your idea is ok for the

But i need to update fields from conact to customer and contact is lookup on  banking , so we have to quesy fields from contact
Virendra ChouhanVirendra Chouhan
Hi 

i hope this is helpful for you.
Here ClassA__C is your Banking__c.
And Object_A__c is Customer__c.

trigger ForHelp on ClassA__c (after insert) {
	
    Set<Id> IdSet = new Set<Id>();

   for(ClassA__c mp:trigger.new)
   IdSet.add(mp.Id);
     
   List<Object_A__c> BankingstubToCreate = new List<Object_A__c>();
   for(ClassA__C mp : [select id,name,Contact__r.name,Contact__r.email From ClassA__c where id IN : Trigger.new])
   {
    if(IdSet.size()>0 )
   {
     Object_A__c s = new Object_A__c();
      s.ClassA__c =mp.Id ;
       s.Name = mp.contact__r.Name;
       s.Email__c = mp.Contact__r.email;
      BankingstubToCreate.add(s);
   }
   }
   
   if(BankingstubToCreate.size()>0)
   {
    Database.SaveResult[] lsr = Database.insert(BankingstubToCreate, false);
             //insert finalCreateListOfStub  
   }
   
 
}

Kind regards
Virendra