You need to sign in to do that
Don't have an account?
Trigger Help
Hey all,
I'm new to apex coding and i'm have an incredibly hard time understanding something that should be really simple.
I have a custom object Reference_Request__c that has a field OftR__c - reference
OftR__c in turn is reference to Opportunity.
Basically, what i want to do is set Reference_Request__c 's OwnerId to OftR__c's OwnerId.
I know that OwnerId is a lookup for OftR__c, but i dont really know what that means or how i should approach something like that.
this is what i have so far, but it returns an Exception on line 5.
trigger Insert_RelatedOwnerID_as_RefReqOwnerID on Reference_Request__c (before update, before insert) { List<Reference_Request__c> l = new List<Reference_Request__c>(); for(Reference_Request__c req: Trigger.new){ l = [Select req.OftR__r.Owner_ID__c From Reference_Request__c req limit 1]; req.OwnerId = l[0].OwnerId; } }
While i'm here asking for help, could someone explain the child-parent, parent-child relationship i have here? I read the web api on relationships and i just cant seem to connect the dots.
Any help is appreciated
Your Reference_Request__c is a child object to Opportunity. You custom object referes to its parent through the OftR__c custom filed. As this field is look up field, you can refer to the object that it points to through OftR__r.
Hope this helps.
All Answers
Your Reference_Request__c is a child object to Opportunity. You custom object referes to its parent through the OftR__c custom filed. As this field is look up field, you can refer to the object that it points to through OftR__r.
Hope this helps.
Dude thanks, worked like a charm.
I have a quick question regarding the logic.
What does the where condition:
id in :Trigger.newMap.keySet()
mean?
and
rmap.get(r.Id).OftR__r.OwnerId
When I tried OftR__r.OwnerId i've always gotten Null Pointer Exceptions, how does adding the get(r.Id) help this case?
Sorry if i'm just being annoying, but I really want to learn from this.
Got it.
thank you very much!