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
AtherisAtheris 

Apex Class 'OR' expression confusion

Hi

We have a custom visualforce page that has it's content pulled to it by different Apex Classes.
In short, the records pulled are determined by their record type + a certain field entry. 

Right now the Apex Class pulls in RecordTypeA results. 
I can edit it (and the Test Class) to pull in RecordTypeB results.

However, when I use the 'OR' expression to have both of the RecordType results show up, only RecordTypeA results are shown up.

This is the code piece (syntax gave no errors)
 
for(recordtype rt:[select id from recordtype where (name=:'1893 Ambassador Request' OR name=:'WebAMB Request') and Sobjecttype =: 'Case' limit 1]) caserecordtype=rt.id; for(recordtype rt:[select id from recordtype where name=: 'CustomerUK' and Sobjecttype =: 'Account' limit 1]) accountrecordtype=rt.id;

Is there something else that needs to be added to the code for it to process both record type results? 

Thank you
Squire Q KershnerSquire Q Kershner
What is the content of the variable RT?  What are you looping though?

As far as the SOQL, I believe you can just use IN to make the queries work instead of OR.  Not that it helps the issue, but a thought.

for(recordtype rt:[select id from recordtype where name IN('1893 Ambassador Request', 'WebAMB Request') and Sobjecttype =: 'Case' limit 1])
{caserecordtype=rt.id};
for(recordtype rt:[select id from recordtype where name = 'CustomerUK' and Sobjecttype = 'Account' limit 1])
{accountrecordtype=rt.id};

Also, I've not used "=:" in SOQL.  Does it work as you expect?
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_comparisonoperators.htm
Jithin Krishnan 2Jithin Krishnan 2
Hi Atheris,
SOQL fetches records randomly in no particular order. If you want the records to be fetched in some order like the one that is the most recently created, you have to use ORDER BY. Else, it is just random. So any one record which satisfy the conditions in your where clause will be fetched. You got only RecordTypeA when you tried doesn't mean that only RecordTypeA will be fetched always.
Thanks
AtherisAtheris
Hello

@Squire HomerJ Kershner

Variable rt refers to recordtype and it is looping through Cases where the Status field is set to Accepted.
I've tested the suggested idea with 'IN' expression, but the result are still the same, it will only display the '1893 Ambassador Request' records in the list. 

As for the "=:" in SOQL, it does seem to work as expected so far. 


@Jithin Krishnan 2

Yes that is true, but on the current list, there will be no RecordTypeB results at all. Ever. 
It doesn't seem to matter if I use 'OR' or 'IN' expression for the list to include both results. 

Individually I can set the Class to bring up either only RecordTypeA results, or only RecordTypeB results, but any sort of expression to include both of them will result only RecordTypeA results to show up.


Here is the full code if that may be useful:
 
public class ambassadorukrow
{
private id caserecordtype;

private id accountrecordtype;
Transient Map<id,case> casemap=new map<id,case>();
list<case> clist=new list<case>();
list<case> clist1=new list<case>();
list<case> clist22=new list<case>();
set<id> caseset=new set<id>();

public ambassadorukrow()
{

clist22.clear();
casemap.clear();
getreasons();
system.debug(pickvalue+'pick===');
for(recordtype rt:[select id from recordtype where name IN('1893 Ambassador Request', 'WebAMB Request') and Sobjecttype =: 'Case' limit 1])
caserecordtype=rt.id;
for(recordtype rt:[select id from recordtype where name=: 'CustomerUK' and Sobjecttype =: 'Account' limit 1])

accountrecordtype=rt.id;

Map<id,id> accmap=new map<id,id>();
set<id> accset=new set<id>();


for(case c:[select id,CaseNumber,AccountId,Account.Name,recordtypeid,account.lastname,
Origin,CreatedDate,status,Brochure__c,Brochure__r.name,Brochure__r.Brochure_Code__c 
from case where status='Send to Fulfilment - Accepted' And recordtypeid=:caserecordtype limit 50000]) 
{
accset.add(c.accountid);
if(c.accountid!=null)
casemap.put(c.id,c);
}
for(account a:[select id,recordtypeid from account where
recordtypeid=:accountrecordtype and id in:accset limit 50000])
accmap.put(a.id,a.recordtypeid);
for(id cid:casemap.keyset())
if(accmap.get(casemap.get(cid).AccountId)!=null){
caseset.add(cid);

}
/*for(Case_Brochure_link__c c1:[select id,case__C,Brochure__c,case__r.CaseNumber,case__r.account.name,
brochure__r.name,brochure__r.brochure_code__C from Case_Brochure_link__c 
where case__c in :caseset and Pre_Order__c='False' and export_date__c=null order by case__r.caseNumber limit 50000])
cblist.add(c1);*/
/*if(cblist.size()>0)
{
for(case cc:[select id from case where id in: caseset])
clist.add(cc);
}*/
for(case cc:[select id,CaseNumber,AccountId,Account.Name,recordtypeid,Account.CRMid__c,Account.Firstname,Account.lastname,Account.Salutation,
Ambassador_Request_Reason__c, Account.PersonMobilePhone,Account.ShippingCountry__c,Account.ShippingPOBox__c,Account.ShippingPostalCode__c,
Account.ShippingRegion__c,Account.ShippingStreetNo__c,Account.Region__c,Account.ShippingBuilding__c,Account.Locality__c,
Account.ShippingStreet__c,Account.ShippingCity__c,Account.Mobile__c,Account.Fax,Account.Email__c,
Origin,CreatedDate from case where status='Send to Fulfilment - Accepted' And id in :caseset limit 50000]) 
clist22.add(cc);
}

public list<Case> getcaserec1()
{
return clist22;
}
list<Case> cblist1=new list<Case>();
public pagereference export()
{
cblist1.clear();
for(Case c21:clist22)
{
//c21.export_date__C=system.today();
c21.status='Close Accepted';
cblist1.add(c21);
}

if(cblist1.size()>0)
update cblist1;

//pagereference p=new pagereference('/apex/ambassadorukrow?type=xls');
pagereference p=new pagereference('/apex/ambassadorukrow1');
return p;
}
public string pickvalue{set;get;}
list<case> casepicklist=new list<case>();
list<case> casecloselist=new list<case>();
public List<SelectOption> getreasons()
{
  List<SelectOption> options = new List<SelectOption>();
        
   Schema.DescribeFieldResult fieldResult = case.Ambassador_Request_Reason__c.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
     options.add(new SelectOption('None', 'None'));   
      options.add(new SelectOption('All', 'All'));  
   for( Schema.PicklistEntry f : ple)
   {
      options.add(new SelectOption(f.getLabel(), f.getValue()));
   }       
   return options;
}
public void applyfilter()
{
casepicklist.clear();
if(pickvalue =='All')
{
for(case casepick:[select id,CaseNumber,AccountId,Account.Name,recordtypeid,Account.CRMid__c,Account.Firstname,Account.lastname,Account.Salutation,
Ambassador_Request_Reason__c, Account.PersonMobilePhone,Account.ShippingCountry__c,Account.ShippingPOBox__c,Account.ShippingPostalCode__c,
Account.ShippingRegion__c,Account.ShippingStreetNo__c,Account.Region__c,Account.ShippingBuilding__c,Account.Locality__c,
Account.ShippingStreet__c,Account.ShippingCity__c,Account.Mobile__c,Account.Fax,Account.Email__c,
Origin,CreatedDate from case where status='Send to Fulfilment - Accepted' And id in :caseset limit 50000]) 
casepicklist.add(casepick);
}
else
{
for(case casepick:[select id,CaseNumber,AccountId,Account.Name,recordtypeid,Account.CRMid__c,Account.Firstname,Account.lastname,Account.Salutation,
Ambassador_Request_Reason__c, Account.PersonMobilePhone,Account.ShippingCountry__c,Account.ShippingPOBox__c,Account.ShippingPostalCode__c,
Account.ShippingRegion__c,Account.ShippingStreetNo__c,Account.Region__c,Account.ShippingBuilding__c,Account.Locality__c,
Account.ShippingStreet__c,Account.ShippingCity__c,Account.Mobile__c,Account.Fax,Account.Email__c,
Origin,CreatedDate from case where status='Send to Fulfilment - Accepted' And id in :caseset and Ambassador_Request_Reason__c=:pickvalue limit 50000]) 
casepicklist.add(casepick);
}
system.debug(casepicklist+'cp===');
}
public list<case> getfilterlist()
{
return casepicklist;
}
public pagereference exportpick()
{
casecloselist.clear();
for(Case c22:casepicklist)
{
//c21.export_date__C=system.today();
c22.status='Close Accepted';
casecloselist.add(c22);
}

if(casecloselist.size()>0)
update casecloselist;


//pagereference p=new pagereference('/apex/ambassadorukrow?type=xls');
pagereference p=new pagereference('/apex/ambassadorukrow1');
return p;
}
}


 
AtherisAtheris

Hi

Some updates.

Removing the LIMIT will make only RecordTypeB records visible. 
Using ID's instead of names for RecordTypes makes no difference.