• Neo Nashville
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 12
    Replies

Hi guys,

I'm trying to auto-populate the lookup field for a custom child object, which is called Purchase Item, with the same lookup field that its parent custom object, Purchase Order, has. The lookup field is called Supplier which is the standard object Account. 

I had a few goes in trying to create a trigger to auto-populate the supplier lookup field that would activate each time a new purchase item on a purchase order is created.

 

I'm not getting any errors with the following code but it does not seem to do what I want:

 

trigger populateSupplier on Purchase_Item__c (before insert , before update){
    
    Set<ID> setPIIds = new Set<ID>();
    for(Purchase_Item__c obj : trigger.new){
        if(obj.Supplier__c == null)
        setPIIds.add(obj.Supplier__c);
    }
    
     MAP<ID , Purchase_Order__c> mapPO = new MAP<ID , Purchase_Order__c>([Select Supplier__c from Purchase_Order__c where id in: setPIIds]);
     for(Purchase_Item__c obj : trigger.new)
       {
        if(obj.Supplier__c == null)
          {
            Purchase_Order__c c = mapPO.get(obj.Supplier__c);
            obj.Supplier__c = c.Supplier__c;
            //Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
          }
       
       }
}

Thanks so much in advance!

Hi guys,

I'm trying to auto-populate the lookup field for a custom child object, which is called Purchase Item, with the same lookup field that its parent custom object, Purchase Order, has. The lookup field is called Supplier which is the standard object Account. 

I had a few goes in trying to create a trigger to auto-populate the supplier lookup field that would activate each time a new purchase item on a purchase order is created.

 

I'm not getting any errors with the following code but it does not seem to do what I want:

 

trigger populateSupplier on Purchase_Item__c (before insert , before update){
    
    Set<ID> setPIIds = new Set<ID>();
    for(Purchase_Item__c obj : trigger.new){
        if(obj.Supplier__c == null)
        setPIIds.add(obj.Supplier__c);
    }
    
     MAP<ID , Purchase_Order__c> mapPO = new MAP<ID , Purchase_Order__c>([Select Supplier__c from Purchase_Order__c where id in: setPIIds]);
     for(Purchase_Item__c obj : trigger.new)
       {
        if(obj.Supplier__c == null)
          {
            Purchase_Order__c c = mapPO.get(obj.Supplier__c);
            obj.Supplier__c = c.Supplier__c;
            //Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
          }
       
       }
}

Thanks so much in advance!