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
sabelstromsabelstrom 

Possible to create SELECT with variable fields

Given that queries in Apex code are performed differently, is it possible to make the fields requested from an object variable?  As the documentation implies, a "bind" expression cannot be used.  If this is not possible by some other approach, it would be a valuable enhancement/improvement.
 
Also, is there a way to investigate the fields on an object (akin to a DescribeSObject) when a trigger fires?  Ideally something like a .js 'for ... in ... ' loop:
for(thisField in thisAccount)
{
    //do something w/ thisField.name
}
 
Thanks!
Ron HessRon Hess
all the fields must be known when you create your Apex Code, no object introspection is available.
brad4dbrad4d
The lack of object introspection is unfortunate.  Is this something that will become available in the future?
Ron HessRon Hess
Yes, this is a highly requested feature, you can vote for this on the idea exchange.  At this time I don't know for sure if it will be included in our next release, but it is possible.
kpitkpit

Ron,

I've voted for this on the idea exchange.  I'm using SF Apex in order to create "Deep Clone Opportunity" functionality, which not only clones the opportunity, but it also carries over related objects such as Contact Roles, Sales Teams, and custom objects.  Currently, I have to list hundreds of the fields in each of the SOQLs. Also, the function would be broken if the fields are removed/renamed and the newly added fields won't be picked up without updating the SOQL.

I am able to implement the functionality in Ajax by using DescribeSObject(table), but it is very slow if there are many related items. I'm just wondering if I can get original object (dynamic fields) in the s_control (Ajax) first , then pass into Apex code to only do the cloning part (faster)? I got error message with "Type". Here is my code, can you please help?

var clonedOpptyId = sforce.apex.execute('OpptyDeepClone' , 'opptyDeepClone', {arg:orgOppty});

global class OpptyDeepClone {

    WebService static String opptyDeepClone(opportunity orgOppty) {

          Opportunity clonedOppty = orgOppty.clone(false);

          .............

  }

}

 

 

kpitkpit
Ron,
 
Could you please help me on a related issue? When the Sales Team members are cloned along with the opportunity, the OpportunityAccessLevel is set to "Read Only" to all of the memebers except the member with "Full access". I then attempted to update the OpportunityShare table. I was able to do it in Ajax, however I am receiving the following error message in Apex.
 
{faultcode:'soapenv"Client', faultstring:'System.DmlException: update faild..............FIELD_INTEGRITY_EXCEPTION, field integrity exception: RowCause (cannot update sharing row with this cause):: [rowCause].
 
 
Here is my Apex code:

List<OpportunityShare> newAccessLevels = new List<OpportunityShare>();

for (OpportunityShare oldOpptyShare:[Select o.UserOrGroupId, o.OpportunityAccessLevel From OpportunityShare o where o.OpportunityId =:opptyId]) {

Id userId = oldOpptyShare.UserOrGroupId;

String oldOpptyAccessLevel = oldOpptyShare.OpportunityAccessLevel;

for(OpportunityShare newOpptyShare:[Select o.OpportunityAccessLevel From OpportunityShare o where o.OpportunityId =:newOpptyId and o.UserOrGroupId=:userId limit 1]) {

newOpptyShare.OpportunityAccessLevel = oldOpptyAccessLevel;

newAccessLevels.add(newOpptyShare);

}

}

if(newAccessLevels.size() > 0) {

update newAccessLevels;

}

if(newAccessLevels.size() > 0) {

update newAccessLevels;

}