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
hideawayguyhideawayguy 

Trigger on custom object to update lookup field from other custom object lookup field

hi folks this is my first attempt at writing a trigger - not going well! here's what i'm trying to do....

 

i have a custom object called Booking__c that has a lookup field called Commission_Agent__c. 

 

Booking__c is related via lookup to another custom object called Property__c.

 

Property__c has a lookup to a Contact record called Contact_Commission_Agent__c.

 

When a new Booking is created (not every time it's edited, just on creation - or directly after it's created i'm assuming), i need the trigger to lookup the Property__c record via the lookup Field Property__c on Booking__c - then update the Commission_Agent__c field with the id of the Contact_Commission_Agent__c field on the property object. 

 

here's what i have so far. 

 

trigger AssignCommAgent on Booking__c (after insert, before update)
{
//instantiate set to hold unique deployment record ids
Set<Id> bookingIds = new Set<Id>();
for(Booking__c s : Trigger.new)
{
bookingIds.add(s.Property__c);
}

//instantiate map to hold deployment record ids to their corresponding ownerids
Map<Id, Property__c> propertyCommAgentMap = new Map<Id, Property__c>([SELECT Id, Contact_Commission_Agent__c FROM Property__c WHERE Id IN: bookingIds]);

for (Booking__c s : Trigger.new)
{
if (s.Commission_Agent__c == null && propertyCommAgentMap.containsKey(s.Property__c))
{
s.Commission_Agent__c = propertyCommAgentMap.get(s.Property__c).Contact_Commission_Agent__c;
}
}
}

 

here's my error. 

 

Error:Apex trigger AssignCommAgent caused an unexpected exception, contact your administrator: AssignCommAgent: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AssignCommAgent: line 17, column 1

 

any insights are greatly appreciated!

hideawayguyhideawayguy

hey folks me again, i just want to update this real quick. i'm sure you all can see i'm grabbing other peoples code and attempting to adapt it to do what i need it to do, sometimes without really understanding what i'm doing. anyhow, i've got a simpler version here that i have somewhat working....

 

trigger assignCommAgent on Booking__c (before insert) {
for (Booking__c a : Trigger.new) {
if(a.Commission_Agent__c==null){
a.Commission_Agent__c = '0035000000Zm34p';
}
}
}

 

the above works great but i need it to do a lookup first as described above. right now i'm just assiging the Commission_Agent__c to a hard coded id. how would i do something a kin to this....

 

$default_comm_agent = mysql(select Contact_Commission_Agent__c from Property__c where id=Booking__c.Property__c)

 

then i could change this line

 

a.Commission_Agent__c = '0035000000Zm34p';

 

to 

 

a.Commission_Agent__c = $default_comm_agent;

 

thank you again for you help!

jbardetjbardet

i'm in your same position i'm pretty sure!!  here's where i'm at, and maybe my code helps you? but i am in your same boat, just trying to take other people's code and adapt it..

 

I have 3 objects
 
Estimate (Estimate__c) 
   Estimate Part (Estimate_Part__c)
   Estimate Step (Estimate_Step__c)
 
Estimate is parent of Estimate Part and Estimate Step.
Estimate step and Estimate Part are joined by lookup relationship.
 
i'm not sure if it is needed, but the child relationship name between Estimate Part and Estimate is  Estimate_Workups
 
 
So, my problem is, user usually creates Estimate Steps through the related list on the Estimate Part record, not the estimate itself. So i want a trigger that will plug the parent Estimate name into the lookup field for Estimate on the Estimate Step record.
 
when i create an estimate step, i do it off of a related list on Estimate Part. It has a blank value for the parent Estimate. I want a trigger that takes the parent (found in the related Estimate Part) and insert it into the Estimate name lookup field on the Estimate STep.
 
i get an error on this part, b/c i don't know what i'm doing: 
[select Name.Estimate_Step__c from Estimate_Part__c where id in:estIds];

 

trigger updateGrandChild on Estimate_Step__c (before insert,before update) {
 
    List<Id> estIds = new List<Id>();
 
    for(Estimate_Step__c tp:trigger.new){
        estIds.add(tp.Estimate_Part__c);
    }
 
   // List<Estimate_Part__c> estList = [select Name.Estimate_Step__c from Estimate_Part__c where id in:estIds];
   
   Map<Id,Estimate__c> teamMap = new Map<Id,Estimate__c>([select Estimate_Part__r.Estimate__c.Name, from Estimate__c where id in:estIds]);
   
    
    for(Estimate_Step__c tp:trigger.new){
    if(!estMap.IsEmpty()){       
        tp.Estimate_Step__c = estMap.get(tp.Estimate_Part__c).Name.Estimate_Step__c;
    
    }
    }
    
    
}

 

 

 
 
hideawayguyhideawayguy

hi, sounds similar to me. i got this mine figure out with this....

 

trigger assignCommAgent on Booking__c (before insert) {
for (Booking__c a : Trigger.new) {

List<Property__c> res =
[SELECT Contact_Commission_Agent__c FROM Property__c WHERE Id = :a.Property_name__c limit 1];

 

if(a.Commission_Agent__c==null){

a.Commission_Agent__c = res[0].Contact_Commission_Agent__c;
}
}
}

 

not sure if this will help you with yours or not.... 

 

JHayes SDJHayes SD

Here's my solution.  I didn't try to compile so you may need to tweak:

 

// Fetch the associated properties
Set<Id> propertyIds = new Set<Id>();
for (Booking__c b : trigger.new) {
if (b.Property__c != null) { propertyIds.add(b.Property__c);
} } // Build an index of property agents Map<Id,String> propertyAgents = new Map<Id,String>(); List<Property__c> propertiesWithAgents = [SELECT Id, Contact_Commission_Agent__c FROM Property__c WHERE Id IN :propertyIds]; for (Property__c p : propertiesWithAgents) { propertyAgents.put(p.Id, p.Contact_Commission_Agent__c); } // Assign a property agent to the booking if one exists for (Booking__c b : trigger.new) { if (propertyAgents.containsKey(b.Property__c)) { b.Commission_Agent__c = propertyAgents.get(b.Property__c); } }

 

 

Regards,

jh

jbardetjbardet
trying this out, but not sure what to put after WHERE.
 
 
 
trigger EstimateParentInsert on Estimate_Step__C (before insert) {
for (Estimate_Step__c a : Trigger.new) {
 
List<Estimate_Part__C> res =
[SELECT id, Estimate__C FROM Estimate_Part__c WHERE _______________ ?
 
if(a.Estimate__c==null){
a.Estimate__c = res[0].Estimate__c
}
}
}
 
here's the situation again in perhaps more clear terms: 
 
so, i have two siblings, A and B.
 
User creates A off the parent.. 
 
Then child B is created off sibling A.  The record therefore has sibling B related properly but does not know parent. 
 
I want the trigger to insert the parent automatically which it can grab from the related sibling. 
 
objects
Parent = Estimate__c    (relevant field on this object = Name  , the standard field)
Child A = Estimate_Part__c   (contains parent lookup; field is named Estimate__c)
Child B = Estimate__Step__c  (I  need trigger to populate field Estimate__c on this object).
 
jbardetjbardet

ehh, i realized a trigger can't help me because the record i'm creating won't know the parent (via related sibling) until after I save the record to lock in the relationship to the sibling.

 

a lot of words, but in any event, i think i need a custom button to accomplish this.