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
Abhishek Sharma 527Abhishek Sharma 527 

add result into another list based on condition

Hello there, I have list of case on which I'm getting my case query result, now based on condition that - OldValue not null condition I want to get those records into new List.
Basically my aim is to filter out those record which has OldValue, newValue (which is coming from Histories field tracking )
 
public static void SessionHistory(){
        try {

            Map<id,list<case>> UserEvent = new Map<id,list<case>>();
            List<case> tempCaseList = null;
            List<case> newEvents1 = new List<case>();

           List<case> newEvents = [SELECT OwnerId,(SELECT OldValue, id, NewValue, CreatedById, CreatedBy.Name, caseId,  case.Ownerid, case.DOS__c FROM Histories where field='status'), Account.Name, Patient_Customer__r.dob__c, CaseNumber,Status,Appointment_Start_Time__c,Appointment_End_Time__c, 
            DOS__c,FacilitiesV3__c,Place_of_Service__c,Practice_Procedure_Code__c,Practice_Procedure_Code__r.Type_of_Service__c,
            Insurance__c,Duration__c, Actual_Duration__c, Unit__c, Actual_Unit__c, Practice_Procedure_Code__r.name, Billed_Amount__c,
            Id,Authorizations__c,Facility_Type__c, Patient_Customer__c, PA_NO_PA__c,Provider__c, Client_Insurance__c,Authorization_Number__c,
            Client_Name__c, Provider_Name__c, Location_Name__c,Insurance_Name__c, Sup_Provider__r.Name FROM Case WHERE Recordtype.Name ='Session'
             ORDER BY OwnerId limit 1800];


             if(newEvents.contains(OldValue)){
               newEvents1.addAll(newEvents);
             }

             system.debug('new history'+ newEvents1);
I tried with listname.contains('OldValue') but it's not working. Please if someone can suggest right approach. It will be very helpful.
 
Best Answer chosen by Abhishek Sharma 527
Nithyanandha NimmalaNithyanandha Nimmala
try this once...
set<string> myset = new set<string>();
For(case c : newEvents )
{
myset.add(c.OldValue);
}
if(myset.contains(OldValue))
{
newEvents1.addAll(newEvents);
}

please tell me whether it is working

All Answers

Nithyanandha NimmalaNithyanandha Nimmala
try this once...
set<string> myset = new set<string>();
For(case c : newEvents )
{
myset.add(c.OldValue);
}
if(myset.contains(OldValue))
{
newEvents1.addAll(newEvents);
}

please tell me whether it is working
This was selected as the best answer
Abhishek Sharma 527Abhishek Sharma 527
Thanks for help Nithyanandha