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
PyramidPyramid 

Populating two fields automatically when a record is created

 

The hierarchy of objects for this problem is Account - Contract - Invoice - Shipment.  
Contract is detail to master with account,
Invoice is detail to master with contract and lookup with account. and
Shipment is detail to master with invoice and lookup with both contract and account.  

The code below  populates the account field on the shipment record when the shipment record is created for the first time.  The problem that I am trying to solve is how to populate the contract field also at the same time that the account field is populated because currently I have to enter the contract number manually even though the shipment record belongs to one and only one contract through the invoice record.

/*Salesforce.com Extension Doug copied OLI_Serial_Number Trigger
This trigger is used to populate Account field on Shipment__c custom object
*/
trigger updateShipmentInfo on Shipment__c (before insert) {

    Set<Id> shipIds=new Set<Id>();
    Set<Id> conIds=new Set<Id>();
    
    for(Shipment__c ship:Trigger.new){
        shipIds.add(ship.Id);
        conIds.add(ship.contract__c);
    }
    
    if(shipIds.size() > 0){
    Map<Id,Contract> conMap = new Map<Id,Contract>([select AccountId from contract where id in:conIds]);
    
        for(Shipment__c ship:Trigger.new){
            ship.put(Shipment__c.Account__c, conMap.get(ship.contract__c).accountId);
        }
    }
}

 

Any help will be much appreciated!

 

Thanks

 

Doug

Jeremy.NottinghJeremy.Nottingh

It sounds like you're going to have to query on Invoice__c to find out the Contract ID. 

Jeremy.NottinghJeremy.Nottingh

I agree with the other poster. Invoice has Account and Contract information, so you'll need to query on that and keep track of what Contract and Account go with what Invoice. Here's my first take on it, I commented out some of your code.

 

trigger updateShipmentInfo on Shipment__c (before insert) {

    //Set<Id> shipIds=new Set<Id>();
    //Set<Id> conIds=new Set<Id>();
    Set<id> invoiceIDset = new set<id>();
    
    for(Shipment__c ship:Trigger.new){
        //shipIds.add(ship.Id);
        //conIds.add(ship.contract__c);
        invoiceIDset.add(ship.Invoice__c);
    }
    
    map<id, id> invId2ConIdMap = new map<id, id>();
    map<id, id> invId2AccIdMap = new map<id, id>();
    
    for (Invoice__c inv : [select Id, Contract__c, Account__c from Invoice__c where Id in :invoiceIDset]) {
    	invId2AccIdMap.put(inv.id, inv.Account__c);
    	invId2ConIdMap.put(inv.id, inv.Contract__c);
    }
    
    for (Shipment__c ship : Trigger.new) {
    	ship.Account__c = invId2AccIdMap.get(ship.Invoice__c);
    	ship.Contract__c = invId2ConIdMap.get(ship.Invoice__c);
    }
    
    /*
    if(shipIds.size() > 0){
    Map<Id,Contract> conMap = new Map<Id,Contract>([select AccountId from contract where id in:conIds]);
    
        for(Shipment__c ship:Trigger.new){
            ship.put(Shipment__c.Account__c, conMap.get(ship.contract__c).accountId);
        }
    }
    */
}

 

I guessed on the field names and stuff. See if that helps you out.

 

Jeremy