You need to sign in to do that
Don't have an account?
JJames
Accessing related list data in trigger
I am a little unsure on how to go about doing this because my understanding of related lists on an object isn't completely there yet. I want to create a trigger that accesses data in an opportunity. The data I need to access is a related list of custom objects, the related list is called Quoted Products. Would I access it something like:
Also, where are the related lists defined? I cannot find the related list in objects or labels. This particular related list is a list of products but I would like to know where this list is named and setup. Any help is greatly appreciated.
trigger UpdateSplits on Opportunity (before update, before insert) { Set<id> eventIds = new Set<Id>(); for (Opportunity opp : Trigger.new) { eventIds.add(opp.Quoted_Products__c); } Map<Id, Event__c> eventMap = new Map<id,Quoted_Products__c>([SELECT id, Name, (SELECT Id, Name FROM Products) FROM Quoted_Products__c WHERE id in :eventIds]); //use data for other things }
Also, where are the related lists defined? I cannot find the related list in objects or labels. This particular related list is a list of products but I would like to know where this list is named and setup. Any help is greatly appreciated.
Before you go any further with the relationships though, I suggest you read up on the following documentation first: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships.htm
Your trigger is going to look something like the following:
I took a few guesses as to the api name of the opportunity lookup on the Quoted_Products__c object, and the API name of the Products child relationship on the Quoted_Products object as well.
Furthermore, Trigger.newMap.keySet() is only available on either after insert or before/after update, so this logic will not execute on before insert of the Opportunity record.
Hope that helps you get started at least.
All Answers
Related Lists in every object show you all the records of other custom/standard objects that are referencing your current record. This relation is done using Lookup fields or Master Detail fields, so in your data model example, you would like to retrieve all the Quoted_Products__c that are referencing to a specific opportunity record.
You can see and edit the Label of the related list when you access to the lookup field. Maybe this pic will help you:
Whenever you create a custom lookup/master-detail field, you'll be asked to fill these fields but as you can see you can edit them later.
Hope this helps you :)
Kind regards,
Carlos.
Before you go any further with the relationships though, I suggest you read up on the following documentation first: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships.htm
Your trigger is going to look something like the following:
I took a few guesses as to the api name of the opportunity lookup on the Quoted_Products__c object, and the API name of the Products child relationship on the Quoted_Products object as well.
Furthermore, Trigger.newMap.keySet() is only available on either after insert or before/after update, so this logic will not execute on before insert of the Opportunity record.
Hope that helps you get started at least.
Question - what if I have 2 lists in an opportunity with the same object type , say commission based product sales and another list non commission sales, is there a way to only access the products from one list ? Or would I need some type of flag on the product object and filter each object by checking the flag?
James - the code you provided references Quoted_Product__c which is not an Object, just the label for the related list (which doesn't have an api call name as I see) So I wouldn't be able to use the label name in the code, correct?
Kind regards :)
Carlos.
If you have two related lists / relationships, it's no big deal. It means either an additional relationship in your SOQL query or a new SOQL query all together, depending on your logic / data model.
and when trying to use either QuoteItems or QuoteItems__c in the following code it returns the error message afterwards:
Error: Compile Error: sObject type 'QuoteItems__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 4 column 31
Also, make sure you get the name of the child relationship instead of the label. Hope that helps.
Error: Compile Error: Didn't understand relationship 'QuoteItems__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 10 column 29