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
EUSEUS 

Cast generic sObject without hardcoding object type

Hi,

 

I needed to sort a custom sObject Pocket__c by a date field, (Pocket_Date__c) however since the SELECT also needs to use a WHERE condition based ID from a Master Relationship I was not able to use ORDER BY.  I followed the example of the Wrapper using COMPARABLE and after loading the Wrapper list and having SORT  the resulting list Pl ,  I need to CAST back the List to the original sObject type Pocket__c. Can someone help me do that or suggest another way of accomplishing the ORDER BY in this context?  Follows the code I used:

 

List<Pocket__c> pKo  = [SELECT p.Id, p.Pocket_Balance__c, p.certRed_Ref__c, p.certRedValue__c, p.certRedDT__c
                                 FROM Pocket__c p
                                 WHERE Wallet__r.Member__c   =: sList.rId_Ref__c
                                 AND   Wallet__r.Merchant__c =: sList.rId2_Ref__c 
                                // ORDER BY p.Pocket_Date__c    It doesn't allow me to use the ORDER BY ....
                                 FOR UPDATE ];
        PocketWrapper[] Pl = new List<PocketWrapper>();
        for (integer i=0; i>pKo.size(); i++) {
         
         Pl.add( new PocketWrapper(pKo[i]));
        }
        Pl.sort();

 

***  I now need to return  PocketWrapper  Pl  back to  sObject  Pocket__c ...... and update the pKo List of sObjects Pocket__c ....

 

 

This is the PocketWrapper class:

 

global class PocketWrapper implements Comparable {
 public Pocket__c expDT;
 // Constructor
 public PocketWrapper(Pocket__c eD) {
 expDT = eD;
 }
 // Compare Pockets based on the expiration Date (Pocket_Date__c).
 global integer compareTo(Object compareTo) {
  // Cast argument to PocketWrapper
  PocketWrapper compareToExpDT = (PocketWrapper)compareTo;
  // The return value of 0 indicates that both elements are equal.
  Integer returnValue = 0;
  if (expDT.Pocket_Date__c > compareToExpDT.expDT.Pocket_Date__c) {
  // Set return value to a positive value.
  returnValue = 1;
  } else if (expDT.Pocket_Date__c < compareToExpDT.expDT.Pocket_Date__c) {
  // Set return value to a negative value.
  returnValue = -1;
  }
  return returnValue;
 }
  }

 

I would appreciate any help! ....

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
Pocket__c[] records = new Pocket__c[0];
for(PocketWrapper item: pl) {
    records.add(item.expDT);
}

 

 

All Answers

sfdcfoxsfdcfox
That's not necessary. You can insert/update a List<SObject> just as easily as you can a List<Pocket__c>. In fact, as of recently, you can have up to ten different types of objects in a single List<SObject> in a single DML statement.
EUSEUS

Thank you sfdcfox,   the thing is the validation method already exists and it is based on the Pocket__c sObject .... here is a short sample of the method:

 

for (Pocket__c pcT : pKs) {
            if (lineItemPrice<=pcT.Pocket_Balance__c) {
             
             pcT.Pocket_Balance__c     -= lineItemPrice;                  // Pocket Balance is deducted.
             pcT.certRed_Ref__c         = sList.rId4_Ref__c;              // Ref to Certificate being sourced.
             pcT.certRedValue__c        = lineItemPrice;                  // Value substracted from Balance.
             pcT.certRedDT__c           = dateTime.now();                 // Date & Time of withdrawal.

 

... and keeps going and going.....    so the question is how would I use the code since the PocketWrapper class doesn't seem to understand both are based upon the same sObject  Pocket__c ?

 

Any ideas?

 

sfdcfoxsfdcfox
Because it's not the same object type. You'll just have to iterate over your PocketWrapper list and extract the values back into a list, then pass this list into your validation routine.
EUSEUS

Thank you sfdcfox,

 

I have no idea as to how to iterate over the PocketWrapper and make a list that will be accepted as a Pocket__c object...

Can you give me a hint?

 

Thank you very much!

sfdcfoxsfdcfox
Pocket__c[] records = new Pocket__c[0];
for(PocketWrapper item: pl) {
    records.add(item.expDT);
}

 

 

This was selected as the best answer
EUSEUS

Thank you!