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
chanbasha nbskchanbasha nbsk 

How to assign new cases to custom object records?

Hi,

I have taken two objects Case and one custom object.i have created some records in custom object with availability checkbox(just checkbox only) is checked and some records are unchecked with availability checkbox.

I wrote a soql query to get availability checkbox records:
List<CaseUser__c> cuser =[SELECT id,Name FROM CaseUser__c where Availability__c=true ORDER BY Name ASC];

by this query i can get all availability records but my point is when i created a new case it is assign to first record in custom object and next if i create a new case then assign to a second record like this furher.....

by using the trigger how can i achieve this

my trigger is

trigger CaseUserAssiginingTrigger on Case (after insert,after update){
 
  List<ID> ids = new List<ID>();
 
  for(Case c : Trigger.new){
  if (c.Reason != null) {
      ids.add(c.Id);
      
      }
  }
   
  //getting all available users
  List<CaseUser__c> cuser =[SELECT id,Name FROM CaseUser__c WHERE Availability__c=true
                            ORDER BY Name ASC];

//add some logic here

Thanks


Thanks

  
NagendraNagendra (Salesforce Developers) 
Hi Chanbasha,

Please find the sample code below and tweak it as per your requirement.
  •  Considering Case__c as Lookup field on CaseUser__c object
trigger CaseUserAssiginingTrigger on Case (after insert,after update){
//part-1 
  List<ID> ids = new List<ID>();

  for(Case c : Trigger.new){
  if (c.Reason != null) {
      ids.add(c.Id);
      }
  }

//part-2
   //getting all available users 
  List<CaseUser__c> cuserUpdateList = new List<CaseUser__c>();
  //Considering Case__c as the Lookup field you can change as per your field name
  for(CaseUser__c caseObj: [SELECT id,Case__c FROM CaseUser__c WHERE Availability__c = true  ORDER BY Name ASC]) {
      if(!ids.isEmpty()) {
         caseObj.Case__c = ids[0];
         cuserUpdateList.add(caseObj);
         //Remove the Case Id from the List once assigned to the CaseUser
         ids.remove(0);
      }
  }
  if(!cuserUpdateList.isEmpty()) {
      update cuserUpdateList;
  }
}
Hope this helps.

Please mark this as solved if the information helps so that it gets removed from the unanswered queue which results in helping others who are encountering similar issue.

Best Regards,
Nagendra.