+ Start a Discussion
SFDC DummySFDC Dummy 

How to update field when record created....

Hi All

I have two object Obj1 and Obj2 .both are lookup realtionship.
i have created Obj1 manually..when i am going to create Obj2 ,after creating Obj2 ,some field of Obj1 will be updated in Obj2...
B TulasiB Tulasi
Hi SFDC Dummy,

u can use this code. i created on Account and Contact fields. if you want to another objets.  you can change the fileds and objects.

trigger updateAddressOnContact on Account (after insert, after update) {

map<id,account> accMap=new map<id,account>();
list<contact> clist=new list<contact>();
set<id> accids=new set<id>();
for(Account acc:trigger.new){
accids.add(acc.id);
accMap.put(acc.id, acc);
}

clist=[select id,name, accountid, MailingCity, Mailingstate from contact where accountid=:accids];
list<contact> contactToUpdate=new list<contact>();

for(contact con:clist){
con.MailingCity=accMap.get(con.accountid).billingcity;
con.Mailingstate=accMap.get(con.accountid).billingstate;
contactToUpdate.add(con);
}

if(contactToUpdate.size()>0){
update contactToUpdate;
}
}


Thanks 
Thulasi
RAM AnisettiRAM Anisetti
Hi ,
Try to see following trigger....
 
trigger Sampletri on Job_Application__c (before insert) {
/*
   //inherent parent fields to child records
   Objects :Job_Application__c,Position__c
   relationship:Lookup
   Parent:Position__c
   child:Job_Application__c
   relationship field:Position__c

*/
Map<ID,Position__c>mapval=new Map<ID,Position__c>();
List<ID>ids=new List<ID>();
for(Job_Application__c e:trigger.new){
ids.add(e.Position__c);
}

//you need to specify which fields u want inherent in below SOQL 
for(Position__c op:[select id,Name from Position__c where id IN:ids]){
  mapval.put(op.id,op);
}
for(Job_Application__c e1:trigger.new){
Position__c p=mapval.get(e1.Position__c);
if(p!=null){

//assign parent fields to child fields
e1.Cover_Letter__c=p.name;
}
}
}