+ Start a Discussion
Venkat adityaVenkat aditya 

trigger on two custom objects

I have to custom objects 1 and 2, 1 is master object, in master object i have a pick list field, now  i want to write a trigger on child obj, when i select obj 1 through lookup from object 2, then parent obj picklist value should populate in child object text field.

Please tell me trigger code,

Thanks in advance.
 
Best Answer chosen by Venkat aditya
rajat Maheshwari 6rajat Maheshwari 6

Hi Venkat,

Before Context trigger will work in your requirement : - 

Below is code snippet : - 

trigger ContactPopulateTrigger on Contact (before insert, before update ) 
{
  Set<Id> st_AccountId = new Set<Id>();
  for(Contact con : Trigger.new)
     {
        st_AccountId.add(con.AccountId);
     }
     
  Map<Id,Account> mp_Account = new Map<Id,account>([Select Id,Name,AccountSource from Account where Id IN:st_AccountId]);
  
  for(Contact con:Trigger.new)
    {
      if(mp_Account!=null && mp_Account.containsKey(con.AccountId))
         {
            con.FirstName = mp_Account.get(con.AccountId).AccountSource;
         }
   }


}


Here in logic, :-  

1. Contact is child Object

2. Account is Parent Object

3. AccountId is lookup Field on Contact

4. AccountSource is picklist field in parent Object (Account)

5. FirstName is text field on contact which will populate from picklist field on account.

All Answers

buggs sfdcbuggs sfdc
HI Venkat,

Here quote is child,opportunity is parent,change the logic according to your req.
trigger UpdateAmount on Quote__c (after insert, after update) { //You want it on update too, right?
  Map<ID, Opportunity> parentOpps = new Map<ID, Opportunity>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();

  for (Quote__c childObj : Trigger.new {
    listIds.add(childObj.Opportunity);
  }

  //Populate the map. Also make sure you select the field you want to update, amount
  //The child relationship is more likely called Quotes__r (not Quote__r) but check
  //You only need to select the child quotes if you are going to do something for example checking whether the quote in the trigger is the latest
  parentOpps = new Map<Id, Opportunity>([SELECT id, amount Name,(SELECT ID, Total_List_Price__c FROM Quotes__r) FROM Opportunity WHERE ID IN :listIds]);

  for (Quote__c quote: Trigger:new){
     Opportunity myParentOpp = parentOpps.get(quote.Opportunity__c);
     myParentOpp.Amount = quote.Total_List_Price__c;
  }

  update parentOpps.values();
}

 
rajat Maheshwari 6rajat Maheshwari 6

Hi Venkat,

Before Context trigger will work in your requirement : - 

Below is code snippet : - 

trigger ContactPopulateTrigger on Contact (before insert, before update ) 
{
  Set<Id> st_AccountId = new Set<Id>();
  for(Contact con : Trigger.new)
     {
        st_AccountId.add(con.AccountId);
     }
     
  Map<Id,Account> mp_Account = new Map<Id,account>([Select Id,Name,AccountSource from Account where Id IN:st_AccountId]);
  
  for(Contact con:Trigger.new)
    {
      if(mp_Account!=null && mp_Account.containsKey(con.AccountId))
         {
            con.FirstName = mp_Account.get(con.AccountId).AccountSource;
         }
   }


}


Here in logic, :-  

1. Contact is child Object

2. Account is Parent Object

3. AccountId is lookup Field on Contact

4. AccountSource is picklist field in parent Object (Account)

5. FirstName is text field on contact which will populate from picklist field on account.

This was selected as the best answer
rajat Maheshwari 6rajat Maheshwari 6

Hi Venkat,

Please feel free to contact me in case of any issue regarding salesforce on my email Id : rajatzmaheshwari@gmail.com

 

Thanks