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
Rishabh Patel 1Rishabh Patel 1 

Update Custom Object Field with List Results -Apex

I have made an @invocableMethod , And I grab some values like Queue ID and Record ID form my Custom Object (QueueDistroHelper__c) . 
All I want to do is Get all users who are in  the Queue (I grab the queue ID from custom field on the Custom object) . And then show all that users on  a field called Queue_Members__c on same Custom object . 
I am not able to add the results to custom field 
Here is my code 
 
global class lookUpAccountAnnotation {
public class inputValues{
        @InvocableVariable(label='Queue ID' required=true)
        public string QueueId;
        @InvocableVariable(label='Record Id' required=true)
        public Id recordid;
       
    }
   @InvocableMethod
    public static void deletePackageLicense(List<inputValues> inputs){
    
      for (inputValues i : inputs)
   {
      List<QueueDistroHelper__c> getcurrentobject =  [SELECT Queue_Members__c FROM QueueDistroHelper__c WHERE Id=:i.recordid  LIMIT 1];
      List<User> users =  [SELECT
                    id,name
                FROM
                    user
                WHERE
                    id IN ( SELECT userOrGroupId FROM groupmember WHERE groupId = :i.QueueID )
                    AND
                    isActive = true
                ORDER BY
                    firstName];
      for (User test : users ){
     //Add User.name in Queue_Members__c
      }
      }
   }
}

So all I want to do is add the test.name to  Queue_Members__c
Best Answer chosen by Rishabh Patel 1
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Rishabh

A small change needs to be done in the piece of code shared earlier
if(getCurrentObject != null && !getCurrentObject .isEmpty()) {
if(getCurrentObject[0].Queue_Members__c == null)
   getCurrentObject[0].Queue_Members__c = ‘’;
getCurrentObject[0].Queue_Members__c =+ test.name;
}

Cheers!!!

All Answers

Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Rishabh

Add this piece of code in your loop and then update the record.

if(getCurrentObject.Queue_Members__c == null)
   getCurrentObject.Queue_Members__c = ‘’;
getCurrentObject.Queue_Members__c =+ test.name;

Cheers!!!
 
Rishabh Patel 1Rishabh Patel 1
Hey Syed ,So when I used your piece of code that is  
if(getCurrentObject.Queue_Members__c == null)
   getCurrentObject.Queue_Members__c = ‘’;
getCurrentObject.Queue_Members__c =+ test.name;
I got  - Variable does not exist: Queue_Members__c

So I changed it to 
            
      for ( User test : users){
   if(QueueDistroHelper__c.Queue_Members__c == null)
   QueueDistroHelper__c.Queue_Members__c = '';
   QueueDistroHelper__c.Queue_Members__c =+ test.name;
    
But now I get  - A value cannot be stored to Queue_Members__c in type QueueDistroHelper__c 
I dont know its not storing the value to that field. The field is a text box . 
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Rishabh

A small change needs to be done in the piece of code shared earlier
if(getCurrentObject != null && !getCurrentObject .isEmpty()) {
if(getCurrentObject[0].Queue_Members__c == null)
   getCurrentObject[0].Queue_Members__c = ‘’;
getCurrentObject[0].Queue_Members__c =+ test.name;
}

Cheers!!!
This was selected as the best answer
Rishabh Patel 1Rishabh Patel 1

Hey Syed , Thanks a lot for replying . Really appriciate it. This one didnt return any errors. But do you have any idea why the code is not returning anything. I mean the field is still not updating. I also tried to hard code the Queue ID and Record ID. But field is still blank . 

Then I tried to limit both queries to 1 So that I get one user in a queue . But thats not working too . 

Any help much appriciated . 

Thanks 
 

Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Rishabh

Have you added the update statement for the record ?

Cheers!!!
Rishabh Patel 1Rishabh Patel 1
Yes, I missed that part! Finally its working. Thanks a lot again