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
cathy369cathy369 

Error: Compile Error: Illegal assignment from SObject to SOBJECT

again, i think i have some things regarding sObjects, methods, classes etc, figured out and apparently I don't... i have one class that loops through a sObject list, and based on criteria, will return the one record i need....  i receive the following error:

Error: Compile Error: Illegal assignment from SObject to SOBJECT:Mfg_Run_Charges_Rate__c at line 38 column 7

CLASS THAT RETURNS RECORD:

public class mfgRunChgRateRec{

   public static sObject getMfgRunChgRateRec(boolean noWin,
                                             boolean punchWin,
                                             boolean stdWin,
                                             boolean spcWin,
                                             boolean dblWin,
                                             boolean furnBlank,
                                             boolean furnPrePrint,
                                             string priceCode,
                                             List<Mfg_Run_Charges_Rate__c> mfgRCR){
                  
      Mfg_Run_Charges_Rate__c mrcrRec = new Mfg_run_Charges_Rate__c();           
     
      for (Mfg_Run_Charges_Rate__c M : mfgRCR){
         if (M.Pricing_Code__c == priceCode){
            if (noWin){
               if (furnBlank){
                  if (M.Window__c == 'N/A' && (M.Furnished__c == 'Blank' || M.Furnished__c == 'Both')){
                     mrcrRec = M;
                     break;
                  }
               } else if (furnPrePrint){
                  if (M.Window__c == 'N/A' && (M.Furnished__c == 'PrePrint' || M.Furnished__c == 'Both')){
                     mrcrRec = M;
                     break;
                  }
               } else{
                  if (M.Window__c == 'N/A' && M.Furnished__c == 'N/A'){
                     mrcrRec = M;
                     break;
                    }
                  }
            } else if (stdWin){
               if (furnBlank){
                  if (M.Window__c == 'Standard Window' && (M.Furnished__c == 'Blank' || M.Furnished__c == 'Both')){
                     mrcrRec = M;
                     break;
                  }
               } else if (furnPrePrint){
                  if (M.Window__c == 'Standard Window' && (M.Furnished__c == 'PrePrint' || M.Furnished__c == 'Both')){
                     mrcrRec = M;
                     break;
                  }
               } else{
                  if (M.Window__c == 'Standard Window' && M.Furnished__c == 'N/A'){
                     mrcrRec = M;
                     break;
                  }
               }
            }
         }
      }
      return mrcrRec;
   }
}
------------------------------------
CLASS THAT RECEIVES COMPILE ERROR WHEN USING THE ABOVE CLASS:
public class MOChargeRate{

   public static decimal[] getMOChargeR(boolean noWin,
                                        boolean stdWin,
                                        boolean spcWin,
                                        boolean MOWinDie,
                                        boolean punchWin,
                                        boolean dblWin,
                                        boolean furnBlank,
                                        boolean furnPrePrint,
                                        decimal wrkPerMQty,
                                        decimal wrkQty,
                                        List<Mfg_Run_Charges_Rate__c> mfgRCR){

      decimal[] MOCharge = new decimal[]{0,0};
      decimal punchChgAmt = 0.0;
      decimal winP = 0;
      decimal furnP = 0;
      decimal smallQtyP = 0;
      decimal sizeP = 0;
      decimal runQty = 0;
      decimal runRate = 0;
      decimal setUp = 0;
      decimal basicRun = 0;
      string priceCode;
      Mfg_Run_Charges_Rate__c MO = new Mfg_Run_Charges_Rate__c();
     
      system.debug('**MO params: ' + noWin + ' ' + MOWinDie + ' ' + punchWin  + ' ' +
                   dblWin + ' ' + furnBlank + ' ' + furnPrePrint + ' ' + wrkPerMQty + ' ' +
                   wrkQty);
                  
      if (wrkQty < 50000){
         priceCode = 'MO';
      }else{
         priceCode = 'MO-50M';
      }
     
      MO = mfgRunChgRateRec.getMfgRunChgRateRec(noWin, punchWin, stdWin, spcWin,
                                                dblWin, furnBlank, furnPrePrint,
                                                priceCode, mfgRCR);
      runQty = MO.Run_Qty__c;
      runRate = MO.Run_Rate__c;
      setUp = MO.Set_Up__c;
      winP = MO.Window_Penalty__c;
      sizeP = MO.Size_Penalty__c;

      if (furnBlank){
         furnP = furnBlankPenalty.getFurnBlankPenalty(wrkQty, MO);
      }else{
         furnP = furnPrePrintPenalty.getFurnPrePrintPenalty(wrkQty, MO);
      }
     
      if (wrkQty < 50000){
        smallQtyP = smallQtyPenalty.getSmallQtyPenalty(wrkQty, MO);
      }

      basicRun = (setUp/wrkPerMQty) + (runRate/(runQty/1000));
      smallQtyP *= basicRun;
      winP *= basicRun;
      sizeP *= basicRun;
      furnP *= basicRun;
      MOCharge[0] = (basicRun + sizeP + smallQtyP + winP) * wrkPerMQty;
      MOCharge[1] = furnP * wrkPerMQty;
      return MOCharge;
   }
}

thank you in advance for any help.
Best Answer chosen by cathy369
cathy369cathy369
i finally got it to compile by changing the first class to return my custom object instead of sObject.. it now returns Mfg_Run_Charges_Rate__c, and the class utilizing the method has been changed as well... see below....

public class mfgRunChgRateRec{

   public static Mfg_Run_Charges_Rate__c getMfgRunChgRateRec(boolean noWin,
                                                                                                                          boolean punchWin,
                                                                                                                         boolean stdWin......){

---------------------------------------

Mfg_Run_Charges_Rate__c MO = mfgRunChgRateRec.getMfgRunChgRateRec(noWin,                                                                                               punchWin, stdWin...);
      runQty = MO.Run_Qty__c;
      runRate = MO.Run_Rate__c;
      setUp = MO.Set_Up__c;
      winP = MO.Window_Penalty__c;
      sizeP = MO.Size_Penalty__c;
                                                                              

thanks again for your help... i think i made this much harder than needed to be...       

All Answers

AshlekhAshlekh
Hi,

Mfg_Run_Charges_Rate__c MO = new Mfg_Run_Charges_Rate__c();

MO is type of object. And mfgRunChgRateRec.getMfgRunChgRateRec() method returning sObject.

Wrong way:
MO = mfgRunChgRateRec.getMfgRunChgRateRec(noWin, punchWin, stdWin, spcWin,  dblWin, furnBlank, furnPrePrint,  priceCode, mfgRCR);
      runQty = MO.Run_Qty__c; 
      runRate = MO.Run_Rate__c;
      setUp = MO.Set_Up__c;
      winP = MO.Window_Penalty__c;
      sizeP = MO.Size_Penalty__c;

MO shuld be sObject 

Right way
sObject MO = mfgRunChgRateRec.getMfgRunChgRateRec(noWin, punchWin, stdWin, spcWin,  dblWin, furnBlank, furnPrePrint,  priceCode, mfgRCR);
      runQty = MO.get('Run_Qty__c');
      runRate = MO.get('Run_Rate__c');
      setUp = MO.get('Set_Up__c');
      winP = MO.get.('Window_Penalty__c');
      sizeP = MO.get.('Size_Penalty__c');

runQty = MO.Run_Qty__c;  wrong way to access

runQty = MO.get('Run_Qty__c;); Right way to access.


IF it helps you than please mark it as a solution and ENJOY APEX
cathy369cathy369
thanks so much for answering... but now i get the following error when i try to compile:

"Error: Compile Error: Illegal assignment from Object to Decimal"    for the line bolded/underlined below....

     sObject MO = mfgRunChgRateRec.getMfgRunChgRateRec(noWin, punchWin, stdWin, spcWin,  dblWin, furnBlank, furnPrePrint,  priceCode, mfgRCR);

      runQty = MO.get('Run_Qty__c');
      runRate = MO.get('Run_Rate__c');
      setUp = MO.get('Set_Up__c');
      winP = MO.get.('Window_Penalty__c');
      sizeP = MO.get.('Size_Penalty__c');
AshlekhAshlekh
Hi,

Just change the below line 

runQty = MO.get('Run_Qty__c');

by below line

 
runQty =MO.get('Run_Qty__c')!=null?Decimal.valueOf(MO.get('Run_Qty__c'));

 MO.get('Run_Qty__c') will return you object and runQty is type of Decimal so you have to convert it to decimal.
 
So need to convert all the variable like above.


IF it helps you than please mark it as a solution and ENJOY APEX 

cathy369cathy369
i finally got it to compile by changing the first class to return my custom object instead of sObject.. it now returns Mfg_Run_Charges_Rate__c, and the class utilizing the method has been changed as well... see below....

public class mfgRunChgRateRec{

   public static Mfg_Run_Charges_Rate__c getMfgRunChgRateRec(boolean noWin,
                                                                                                                          boolean punchWin,
                                                                                                                         boolean stdWin......){

---------------------------------------

Mfg_Run_Charges_Rate__c MO = mfgRunChgRateRec.getMfgRunChgRateRec(noWin,                                                                                               punchWin, stdWin...);
      runQty = MO.Run_Qty__c;
      runRate = MO.Run_Rate__c;
      setUp = MO.Set_Up__c;
      winP = MO.Window_Penalty__c;
      sizeP = MO.Size_Penalty__c;
                                                                              

thanks again for your help... i think i made this much harder than needed to be...       
This was selected as the best answer