You need to sign in to do that
Don't have an account?
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!
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!
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..
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....
Here's my solution. I didn't try to compile so you may need to tweak:
Regards,
jh
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.