function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Carrlos BoydCarrlos Boyd 

Show Full Name of record owner, or queue, in Related List

I have two custom objects: the parent is Object1 and the child is Object2. The child records can be owned by a person or a queue. In the related list on Object1, I need to combine FirstName and LastName into one field, therefore utilizing one column, but keep getting an error that "the field does not exist". Also, when the child record is owned by a queue, in the first name and last name columns is a hyphen (-). How can I show the full name or the queue name in the same column?
Best Answer chosen by Carrlos Boyd
Carrlos BoydCarrlos Boyd
This is what I ended up doing. I hid the standard owner field that has the [Change] button and created a custom field for the owner and put in this code:
IF(Owner:Queue.QueueName = "", (Owner:User.FirstName + " " + Owner:User.LastName), Owner:Queue.QueueName)
So now my custom field shows the owner whether it is a user or a queue and with FLS I made it Read-only.

Thanks for the help!

All Answers

Abdul KhatriAbdul Khatri
The ownership is inherited from the parent object. See below for more details about relationships between objects: 

http://www.salesforce.com/us/developer/docs/api/Content/relationships_among_objects.htm

If you wanted to show the owner on the child object, you can create a formula field on child object reference like below. In formula, Insert Field will help you how reference parent Owner field
parent.Owner.FirstName  + '  ' + parent.Owner.LastName
Let me know if this helped
Carrlos BoydCarrlos Boyd
So I mis-spoke in my explanation. I don't have a parent/child relationship because this would not let me assign the child object to a queue so I changed the relationship to just a look up. That being said....

When an Object2 record is created and related to Object1 and the owner is a queue, I would like to change the queue owner of Object1 to the same queue owner of Object2. I can figure out the related list view later. The owner is more important. No matter if Object1 is owned by a user or a queue, when the Object2 record is created and is owned by a queue, I need the Object1 record to update to be owned by the same queue.

Hope this helps explain things better.
Abdul KhatriAbdul Khatri
I suspect because while creating any record the owner is the one who is basically creating a record. You can later change either through a trigger after insert to a Queue based on certain criteria. Being this said they way I can see you can achieve this using trigger before update on Object 2

See if the below code help you. Since I don't have the actual object name and fields I have reference them accordingly as per your given reference. Please change them and I hope it help you 
trigger UpdateOwner on Object2 (before update) {
    Map<Id, Group> idQueueMap = new Map<Id, Group>([SELECT Id FROM Group WHERE Type = 'Queue']);
    List<Object2> obj2List = new List<Object2>();
    List<Object1> obj1ListtoUpdate = new List<Object1>();
    for(Object2 obj2 : [SELECT Id, Name, OwnerId, object1field__c FROM Object2 WHERE Id in :Trigger.new]) {
        if(idQueueMap.containsKey(obj2.OwnerId))
            obj2List.add(obj2);
    }
    
    if(obj2List.size() == 0) return;
    
    for(Object2 obj2 : obj2List) {
        Object1 obj1toupdate = new Object1 (Id = obj2.object1field__c);
        obj1toupdate.OwnerId = obj2.OwnerId;
        obj1ListtoUpdate.add(obj1toupdate);
    }
    
    if(obj1ListtoUpdate.size() > 0)
        update obj1ListtoUpdate;
}
I hope it help
 
Carrlos BoydCarrlos Boyd
I will try this. Just for clarification, object1 is 'Region__c' and object2 is 'Area__c'. Can you please show me again using actual names?
Abdul KhatriAbdul Khatri
Here you go
 
trigger UpdateOwner on Area__c (before update) {
    Map<Id, Group> idQueueMap = new Map<Id, Group>([SELECT Id FROM Group WHERE Type = 'Queue']);
    List<Area__c> areaList = new List<Area__c>();
    List<Region__c> regionListtoUpdate = new List<Region__c>();
    for(Area__c area : [SELECT Id, Name, OwnerId, Region__c.OwnerId FROM Area__c WHERE Id in :Trigger.new]) {
        if(idQueueMap.containsKey(area.OwnerId))
            areaList.add(area);
    }
    
    if(areaList.size() == 0) return;
    
    for(Area__c area : areaList) {
        Region__c regiontoupdate = new Region__c (Id = area.Region__c);
        regiontoupdate.OwnerId = area.OwnerId;
        regionListtoUpdate.add(regiontoupdate);
    }
    
    if(regionListtoUpdate.size() > 0)
        update regionListtoUpdate;
}

 
Carrlos BoydCarrlos Boyd
This should update the queue owner of Region__c anytime the Area__c record is assigned to a queue, correct?
Abdul KhatriAbdul Khatri
Yes, therefore the first line in that trigger to get all the queues

You can give a try testing it and if you see any issues let me know
Carrlos BoydCarrlos Boyd
Ok, will test now and report back shortly.
Abdul KhatriAbdul Khatri
Did you test?
Carrlos BoydCarrlos Boyd
I'm getting multiple errors. I had to update the object names and when I plug them in, I must be doing something wrong.
  1. Object1 is now "Sales_Region__c"
  2. Object2 is now "Area_Sales_History__c"
As I start trying to plug those values in, I get errors and I'm not familiar enough with triggers to figure them out.

Also, just for clarification, I am putting the trigger in Area_Sales_History__c (Object2) although it's Sales_Region__c (Object1) that I need to be updated.
Abdul KhatriAbdul Khatri
You haven't shared the errors but keep changing the object names. I would love to help you out. Please give me the details about the objects and also the errors you are referring.
Carrlos BoydCarrlos Boyd
Here is what I used:
trigger UpdateOwner on Area_Sales_History__c (before update) {
    Map<Id, Group> idQueueMap = new Map<Id, Group>([SELECT Id FROM Group WHERE Type = 'Queue']);
    List<Area_Sales_History__c> Area_Sales_History__cList = new List<Area_Sales_History__c>();
    List<Sales_Region__c> Sales_Region__cListtoUpdate = new List<Sales_Region__c>();
    for(Area_Sales_History__c Area_Sales_History__c : [SELECT Id, Name, OwnerId, Sales_Region__c.OwnerId FROM Area_Sales_History__c WHERE Id in :Trigger.new]) {
        if(idQueueMap.containsKey(Area_Sales_History__c.OwnerId))
            Area_Sales_History__cList.add(Area_Sales_History__c);
    }
    
    if(Area_Sales_History__cList.size() == 0) return;
    
    for(Area_Sales_History__c Area_Sales_History__c : Area_Sales_History__cList) {
        Sales_Region__c Sales_Region__ctoupdate = new Sales_Region__c (Id = Area_Sales_History__c.Sales_Region__c);
        Sales_Region__ctoupdate.OwnerId = Area_Sales_History__c.OwnerId;
        Sales_Region__cListtoUpdate.add(Sales_Region__ctoupdate);
    }
    
    if(Sales_Region__cListtoUpdate.size() > 0)
        update Sales_Region__cListtoUpdate;
}


Error message:​
"Error: Compile Error: Invalid character in identifier: Area_Sales_History__cList at line 3 column 40"

Carrlos BoydCarrlos Boyd
I think I see what I did wrong and made corrections:
trigger UpdateOwner on Area_Sales_History__c (before update) {
    Map<Id, Group> idQueueMap = new Map<Id, Group>([SELECT Id FROM Group WHERE Type = 'Queue']);
    List<Area_Sales_History__c> areaList = new List<Area_Sales_History__c>();
    List<Sales_Region__c> regionListtoUpdate = new List<Sales_Region__c>();
    for(Area_Sales_History__c area : [SELECT Id, Name, OwnerId, Sales_Region__c.OwnerId FROM Area_Sales_History__c WHERE Id in :Trigger.new]) {
        if(idQueueMap.containsKey(area.OwnerId))
            areaList.add(area);
    }
    
    if(areaList.size() == 0) return;
    
    for(Area_Sales_History__c area : areaList) {
        Sales_Region__c regiontoupdate = new Sales_Region__c (Id = area.Sales_Region__c);
        regiontoupdate.OwnerId = area.OwnerId;
        regionListtoUpdate.add(regiontoupdate);
    }
    
    if(regionListtoUpdate.size() > 0)
        update regionListtoUpdate;
}
Also on line 5, Salesforce told me to use "Sales_Region__r.OwnerId" instead of "Sales_Region__c.OwnerId" which I did and then was able to save the trigger.

When my Processor Builder process ran on the Area_Sales_History__c record and assigned the owner as a queue, the owner of the related Sales_Region__c record did not change to the same queue. It did nothing.

Also, I noticed that when I tried to manually change the owner to a user or to a queue, this is the error I received:
Error: Apex trigger UpdateOwner caused an unexpected exception, contact your administrator: UpdateOwner: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a6w3D0000008SuDQAU; first error: INVALID_OPERATION, Queue not associated with this SObject type: []: Trigger.UpdateOwner: line 19, column 1

 
Abdul KhatriAbdul Khatri
try to run it After Update. Change the first line to this.
 
trigger UpdateOwner on Area_Sales_History__c (after update) {
Carrlos BoydCarrlos Boyd
"Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger UpdateOwner caused an unexpected exception, contact your administrator: UpdateOwner: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a6w3D0000008SuDQAU; first error: INVALID_OPERATION, Queue not associated with this SObject type: []: Trigger.UpdateOwner: line 19, column 1"
Abdul KhatriAbdul Khatri
Can you check if the Owner field on both the object looks like this. Can you share the screen shot, how it looks like

User-added image
Abdul KhatriAbdul Khatri
Please also make sure while defining the Queue you mentioned the Custom Object Names in the Selected Box as shown below

User-added image
Carrlos BoydCarrlos Boyd
The owner fields are correct on each object. But I was missing one of the custom objects for the queues so I added them. The queue did not update while testing but I didn't get an error message this time. 
Carrlos BoydCarrlos Boyd
I tried changing the first line back to
trigger UpdateOwner on Area_Sales_History__c (before update) {
and it still didn't work. The queue does not update on the region (obj1) record although the area (obj2) is assigned to a queue.
Abdul KhatriAbdul Khatri
Please execute the following code and share the debug log
 
trigger UpdateOwner on Area_Sales_History__c (before update) {
    Map<Id, Group> idQueueMap = new Map<Id, Group>([SELECT Id FROM Group WHERE Type = 'Queue']);
    system.debug('>>>>idQueueMap : ' + idQueueMap);
    List<Area_Sales_History__c> areaList = new List<Area_Sales_History__c>();
    List<Sales_Region__c> regionListtoUpdate = new List<Sales_Region__c>();
    for(Area_Sales_History__c area : [SELECT Id, Name, OwnerId, Sales_Region__c.OwnerId FROM Area_Sales_History__c WHERE Id in :Trigger.new]) {
        if(idQueueMap.containsKey(area.OwnerId))
            areaList.add(area);
    }
    system.debug('>>>>areaList : ' + areaList);
    if(areaList.size() == 0) return;
    
    for(Area_Sales_History__c area : areaList) {
        Sales_Region__c regiontoupdate = new Sales_Region__c (Id = area.Sales_Region__c);
        regiontoupdate.OwnerId = area.OwnerId;
        
        regionListtoUpdate.add(regiontoupdate);
    }
    system.debug('>>>>> regionListtoUpdate : ' + regionListtoUpdate);
    if(regionListtoUpdate.size() > 0)
        update regionListtoUpdate;
}

 
Abdul KhatriAbdul Khatri
You still use after update
Carrlos BoydCarrlos Boyd
Nothing showed up in the debug log. The most recent entry is from 1/9/2018 10:23 AM
Abdul KhatriAbdul Khatri
You have update the date range of the debug log for the user who is running the code and then it will show up in the log.

User-added image

User-added image

After doing this run the code and the log will showup
Carrlos BoydCarrlos Boyd
This is what I ended up doing. I hid the standard owner field that has the [Change] button and created a custom field for the owner and put in this code:
IF(Owner:Queue.QueueName = "", (Owner:User.FirstName + " " + Owner:User.LastName), Owner:Queue.QueueName)
So now my custom field shows the owner whether it is a user or a queue and with FLS I made it Read-only.

Thanks for the help!
This was selected as the best answer
Abdul KhatriAbdul Khatri
Well this all endup very strange and you finally endup what you intially request (Your startup question) which I helped you initially with but after that you change the whole scenario with the following

"So I mis-spoke in my explanation. I don't have a parent/child relationship because this would not let me assign the child object to a queue so I changed the relationship to just a look up. That being said....

When an Object2 record is created and related to Object1 and the owner is a queue, I would like to change the queue owner of Object1 to the same queue owner of Object2. I can figure out the related list view later. The owner is more important. No matter if Object1 is owned by a user or a queue, when the Object2 record is created and is owned by a queue, I need the Object1 record to update to be owned by the same queue.

Hope this helps explain things better
"

I have been helping you with this and you haven't told me what happened to this one. Just for the curiousity would you kind to let me know, was my code able to help you with this logic and you never needed that. Thanks 
Carrlos BoydCarrlos Boyd
Abdul, I really appreciate your help. Unfortunately everything I tried did not work. I would have preferred the trigger but it was just not working. I never got anything in the logs. What I ended up doing is just a work around because I have so many other issues I'm working on I felt as though I spent too much time on just this one. I hope you understand. Thanks for your kindness and expertise.