Groovy -22 – Access Objects newView; Collections; Related Objects

This Objects can be top level Objects, parents, as they are also called or Child Objects.
Oracle Sales Cloud puts at our disposal 3 ways we can access this Objects:
1 – Related Collection  attribute
This attribute is created by the system for us when we have a relationship
of 1 : M or M:M between 2 objects.
It can exist out of the box for standard objects or is create when we make
a custom Dynamic Choice List or
a Relationship(using relationships in Application Composer).
For example if you go to Opportunity Object we have a Child Object
called Opportunity Revenue. The Opportunity Revenue is
a Child of the Opportunity Object, hence for 1 Opportunity Record there can  be multiple Opportunity Revenue Records.
That means between the Opportunity and Opportunity Revenue
we have a relationship of 1 to Many (1:M).
The system has made for us out of the box on the Opportunity Object a attribute called, ChildRevenue(You can easily  find it if you go to
App Composer > Opportunity and open any Groovy console then search in the Field Pallet for ChildRevenue).
This Related Collections can be used to access all the attributes(fields) from
the child records of the Opportunity.
Lets say we want to sum up all the Qualities in all the Revenue
lines in the Revenue Lines of an Opportunity:
Code looks like this:
def vQuatity = 0  
def revChild = ChildRevenue  
 def row  =  
 vQuatity += row.Quantity  
return vQuatity  
Looking at the code above you we can extract a general form for the code
used on Related Collections as:
def varName = “”  
def collectionView  = NameOfTheCollection  
def row =  
 varName = row.FieldNameFromCollectionObject  
return varName  
So first we need some variables in which we will hold the values of the fields from the Related Collection Object, line 1 Then we make a variable to hold the entire collection, which is basically  view of the related object – collectionView The we use a while loop to cycle through the records of the related collection as long as we have records, see the hasNext() method used in while  loop  condition Then we define a variable to hold each row of that collection, see the def row statement. We also use next() method to move the courser to the next row in the collection while the loop is cycling. And we finally use the row variable to access for each record fields from them, by specifying the name of the field. 2 – Related Object attribute
Related objects are attributes that indicate we have only 1 Record form an external Object connected to our current record  or records. I will keep with the opportunity example. If For the Opportunity Record we have related collections when comes to the revenue, as seen above, for the revenue line we have only 1 Opportunity Record that can be connected with. And that unique Opportunity Record is called a Related Object. Now the name of this Related Objects can differ, depending on what object we are talking about. For Opportunity Revenue we can see in the Pallet that the Related Object is called Opportunity.
But what if we made a Dynamic Choice List pointing to an Opportunity. In that case while the DCL is made the system generates for us a Related Object attribute that has this naming: DCLname_Obj_c Now how would we use a Related Object Attribute to access some Field Value? Is quite simple and straight forward, as we only have 1 possible record that can be connected to the object in which we are, we don’t need loops or checking if other records are there via hasNext() or next(). You simply do like so: RelatedObjectAttributeName?.FieldApiName For Example in Opportunity Revenue, we make trigger in which we want to get the Opportunity name, code is like so:
def vName  = Opportunity?.Name
In case we made some Costume DCL poitinting to Opportunity, code looks like  so:
Now the best way to look for Related Object Attributes and Related Object Collections is by looking into the pallet. You Go to Fields and you switch the searching parameters to Type then just search %Related% It will display you all the Related Collections and Objects available.
3 – Use newView() method.
This is generally  used when we have no relationship between the Object, in which context we are on and the Object we want to get data from. By relationship I mean  the system has not created for us a related collection or a related object attribute. However nothing stops you to use newView() even when there is a Related Object Attribute or Related Collection available, but why would you make your life harder? With newView() the system wont help you in any way, you need to put a lot of code and also this code will execute a bit more slow that if you would use already existing Related Objects or Collections. So recommendation is if you have available Related Collections or Objects use those attributes and reserve usage of newView for special cases or when non of the 2 mentioned before, are available.
That said lets see the structure of newView() code
//Use the newView() function to get a view object  
def vo = newView(‘OBJECT_NAME)  
//Create a new view criteria  
def vc = newViewCriteria(vo)  
//Create a new view criteria row  
def vcr = vc.createRow()  
//Create a view criteria item   
def vci1 = vcr.ensureCriteriaItem(‘FIELD_API_NAME_FROM_OBJECT‘)  
//Set the operator of the first view criteria item  
//Set the value of the first view criteria item  
vci1.setValue(A VALUE)  
//Set the text field criteria to be case-insensitive  
//Add the new view criteria row  
//Append the view criteria to the view object  
//Execute the query  
def row  
//extract the value from a field on the object the newView was made on  
varName += row.FieldNAme  
So that’s a basic structure of using newView()
There is a siplified version of the code above as below
* Query all ‘Working’-status trouble tickets assigned to a staff member 
with id 100000000089003 */
// 1. Use the newView() function to get a view object
def vo = newView(‘TroubleTicket_c’)
// 2. Append a view criteria using a filter expression
vo.appendViewCriteria(“AssignedTo_Id_c = 1089003 and Status_c = ‘Working'”)
// 3. Execute the query
// 4. Process the results
if (vo.hasNext()) {
 def row = 
// Do something here with the current result row
But Lets take an Actual Example to demonstrate usage of this code. Lets presume you have a field in your object that hold a Resource Name And also for the sake of the Example, that between the Object, on which this field is, there is no connection what so ever with the Resource Object(This is the object all Resources are in) So we have a field with a Resource name and for some reason we need to get the ‘ResourceProfileId’ . Name of the object in which this field is is called abc How would the code look like?
Now in this last part, we are actually expectation only 1 record to come so, we can skip the while loop and use one of this 2 methods first() or last(). Those 2 methods used on a  view are expected to return the first row or the lat one, but since we are expecting  only 1 record to be found in Resource Directory then we can safely use either of them. So the code would be like:  return view_Resource.first()?.’ResourceProfileId’ And that line can replace the entire while loop.
The newView() explanation :

Leave a Reply

Your email address will not be published. Required fields are marked *