+ Start a Discussion
alxalx 

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

Best Answer chosen by Admin (Salesforce Developers) 
Venkat PolisettVenkat Polisett
Below is the bulkyfied trigger code:
trigger Insert_RelatedOwnerID_as_RefReqOwnerID on Reference_Request__c (before update, before insert)
{
	Map<Id, Reference_Request__c> rmap = new Map<Id, Reference_Request__c>(
                                                  [select id, OftR__r.OwnerId 
                                                     from Reference_Request__c
                                                    where id in :Trigger.newMap.keySet()]);

         	for(Reference_Request__c r: Trigger.new)
         {
              r.OwnerId = rmap.get(r.Id).OftR__r.OwnerId;
	}
}

 

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

Venkat PolisettVenkat Polisett
Below is the bulkyfied trigger code:
trigger Insert_RelatedOwnerID_as_RefReqOwnerID on Reference_Request__c (before update, before insert)
{
	Map<Id, Reference_Request__c> rmap = new Map<Id, Reference_Request__c>(
                                                  [select id, OftR__r.OwnerId 
                                                     from Reference_Request__c
                                                    where id in :Trigger.newMap.keySet()]);

         	for(Reference_Request__c r: Trigger.new)
         {
              r.OwnerId = rmap.get(r.Id).OftR__r.OwnerId;
	}
}

 

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.

This was selected as the best answer
alxalx

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.

Venkat PolisettVenkat Polisett

alx wrote:

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?

 

Triggers expose a list of current records (Trigger.new) and a map of current records (Trigger.newMap and Trigger.oldMap). In the above expression we are taking the keys as a set and sending them over on the SOQL statement.

and 

rmap.get(r.Id).OftR__r.OwnerId

 

OftR__c points to the opportunity record. From a SOQL point of view, you refernece the Opportunity record though OftR__r. Hence, you can access the Opportunity Owner through OftR__r.OwnerId. Ofcource, you have select the fields you want from Opportunity in your SOQL.

 

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.


 

alxalx

Got it.

 

thank you very much!