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
ApexLegendApexLegend 

SOQL Query IN not working

I have a Opportunity Trigger Handler were I want to create custom Sportsman__c records related to the created or updated Opportunity. 

In case of Update, to find wheter the updated Opportunity already has Sportsman__c records, i execute the following: 
 
// check if trigger is update
if (oldMap != null) {
	oldRecordList.addAll(oldMap.values());
    System.debug('oldRecordList: ' + oldRecordList);
    // create list with sportsman records related to an opportunity
    sportsmanCheckList = [SELECT Id, Sportsman__c, Opportunity__c FROM Sportsman__c WHERE Opportunity__c IN :oldRecordList];
    System.debug('Checklist: ' + sportsmanCheckList);
    System.debug('oldMap: ' + oldMap);

        for (Opportunity o : oldRecordList) {
        // put opportunities w/o sportsman records in opportunityWithoutSportsmanList
           for (Sportsman__c s : sportsmanCheckList) {
                if (o.Id != s.Opportunity__c) {
               opportunityWithoutSportsmanList.add(o);
                        } 
                    }
                }
The problem is that the sportsmanCheckList never gets filled, I suspect something is wrong with my SOQL query. But when I debug the oldRecordList it contains the updated Opportunity.

Can someone help me with this issue?
 
Raquib SFRaquib SF
Hello,

You woudl want to do something like this.


You would want to use 
// check if trigger is update
if (oldMap != null) {
    oldRecordList.addAll(oldMap.values());
    System.debug('oldRecordList: ' + oldRecordList);

    List<Id> oldRecordId = new List<Id>(); 

    //Get the Id of the Opptys from List of oppties.
    for(Opportunity o: oldRecordList){
        oldRecordId.add(o.Id);
    }

    // create list with sportsman records related to an opportunity
    sportsmanCheckList = [SELECT Id, Sportsman__c, Opportunity__c 
                            FROM Sportsman__c 
                                WHERE Opportunity__c IN: oldRecordId];
    System.debug('Checklist: ' + sportsmanCheckList);
    System.debug('oldMap: ' + oldMap);

    for (Opportunity o: oldRecordList) {
        // put opportunities w/o sportsman records in opportunityWithoutSportsmanList
        for (Sportsman__c s: sportsmanCheckList) {
            if (o.Id != s.Opportunity__c) {
                opportunityWithoutSportsmanList.add(o);
            }
        }
    }
Please let me know if that solves your problem.

Thanks!
 
Ajay K DubediAjay K Dubedi
Hi ApexLegend,    

I have understood your question and I guess You should query with oldRecordId rather than OldRecordList.
 
// check if trigger is update
if (oldMap != null) {
    oldRecordList.addAll(oldMap.values());
    System.debug('oldRecordList: ' + oldRecordList);

    List<Id> oldRecordId = new List<Id>(); 
    for(Opportunity op: oldRecordList){
        oldRecordId.add(op.Id);
    }


    // create list with sportsman records related to an opportunity
    sportsmanCheckList = [SELECT Id, Sportsman__c, Opportunity__c 
                            FROM Sportsman__c 
                                WHERE Opportunity__c IN: oldRecordId];
    System.debug('Checklist: ' + sportsmanCheckList);
    System.debug('oldMap: ' + oldMap);
 List<Sportsman__c> opportunityWithoutSportsmanList = new List<Sportsman__c>();

    for (Opportunity o: oldRecordList) {
        // put opportunities w/o sportsman records in opportunityWithoutSportsmanList
        for (Sportsman__c s: sportsmanCheckList) {
            if (o.Id != s.Opportunity__c) {
                opportunityWithoutSportsmanList.add(o);
            }
        }
    }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com (http://www.ajaydubedi.com  )