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
Lee_CampbellLee_Campbell 

Complicated List Query...

If I created a class such as:

 

public class myClass{
    public double myMethod(List<Object> a, integer b){
        //some logic
    }
}

 

And a list such as: [Select id, some_double__c, some_String__c, some_other_double__c from Object where blah blah blah],

 

could I use those doubles in the class?

 

i.e. logic that said something generic like:

 

a new double x is found by finding the doubles on row i of the list, multiply the first double by 5 then subtract the second double, which in the case above would be:

 

x = 5 * a[i].some_double__c - a[i].some_other_double__c;

 

Any ideas?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Yes, although you'll have to give the class a way to identify which double is #1 and which is #2, through a naming convention or the like.

 

I think what you are looking for is dynamic DML - this allows you to extract field values from an sobject based on a string containing the name rather than hardcoding the field.

 

E.g.

 

String dbl1='Double_1__c';
String dbl2='Double_2__c';
List<MyObject__c> objList=[select here];

double result=MyMethod(dbl1, db12, objList, 4);

...

public double MyMethod(String db1, String dbl2, List<Sobject> objs, integer intVal)
{
  for (Integer idx=0; idx<objs.size(); idx++)
  {
     double dblval1=(double) objs[idx].get(dbl1);
     double dblval2=(double) objs[idx].get(dbl2);

     double x=5 * dblval1 - dblval2;
  }
}

 

 

All Answers

bob_buzzardbob_buzzard

You should be able to do exactly as you have written, so I'm guessing I'm not understanding your problem.  Are you trying to refer to the double fields generically - i.e. not specify the exact name or type of sobject?

Lee_CampbellLee_Campbell

My intention was to specify the sObject that this is a list of, but not the SOQL query items, so that whatever list I use in this class, the class can find the doubles in that list and perform some operation on them.

 

Does that make more sense?

bob_buzzardbob_buzzard

Yes, although you'll have to give the class a way to identify which double is #1 and which is #2, through a naming convention or the like.

 

I think what you are looking for is dynamic DML - this allows you to extract field values from an sobject based on a string containing the name rather than hardcoding the field.

 

E.g.

 

String dbl1='Double_1__c';
String dbl2='Double_2__c';
List<MyObject__c> objList=[select here];

double result=MyMethod(dbl1, db12, objList, 4);

...

public double MyMethod(String db1, String dbl2, List<Sobject> objs, integer intVal)
{
  for (Integer idx=0; idx<objs.size(); idx++)
  {
     double dblval1=(double) objs[idx].get(dbl1);
     double dblval2=(double) objs[idx].get(dbl2);

     double x=5 * dblval1 - dblval2;
  }
}

 

 

This was selected as the best answer
Lee_CampbellLee_Campbell

Brilliant, thanks!

Lee_CampbellLee_Campbell

Hmmm... I've just tried it and I'm getting a "method does not exist or incorrect signature" error when I try to make use of this new class in a Trigger. Any thoughts?

Lee_CampbellLee_Campbell

One word: static. My bad.