You need to sign in to do that
Don't have an account?
Beekman
Passing and receiving an sObject
Hello all,
I need help on a solution and for some reason I can not figure it out. We have to many Admin (don't know why) here on Salesforce, with that I need to create my SOQL on the fly. I currently have the module below for a few major sObjects and it works awesome.
However, I would like it to work with all my objects but don't want to repeat the code over and over. What I would like to be able to do but cannot figure it out (me stupid) is pass the sObject to one single piece of code. I can not figure out how to send it or recieve the sObject like show below as <so>.
Any suggestions?
I need help on a solution and for some reason I can not figure it out. We have to many Admin (don't know why) here on Salesforce, with that I need to create my SOQL on the fly. I currently have the module below for a few major sObjects and it works awesome.
/***************************************************************************************************** Class Abreviations: S - Field, q - Built Query, c - Count Code class calling examples: Opportunity o = Database.query(Utils.getOpportunityFields() + ' limit 1'); list<Opportunity> o = Database.query(Utils.getOpportunityFields() + ' where Id in: trigger.new'); *****************************************************************************************************/ public static string getOpportunityFields() { List<String> f = new List<String>(); Map<String, Schema.SobjectField> fields = Opportunity.getSObjectType().getDescribe().fields.getMap(); string q = 'SELECT '; integer c = 1; for (String s: fields.keySet()) { if (fields.get(s).getDescribe().isAccessible()) { if(c == 1) q += s; else q += ', ' + s; c ++; } } return q + ' FROM Opportunity '; }
However, I would like it to work with all my objects but don't want to repeat the code over and over. What I would like to be able to do but cannot figure it out (me stupid) is pass the sObject to one single piece of code. I can not figure out how to send it or recieve the sObject like show below as <so>.
Any suggestions?
/***************************************************************************************************** Class Abreviations: S - Field, q - Built Query, c - Count Code class calling examples: Opportunity o = Database.query(Utils.getOpportunityFields() + ' limit 1'); list<Opportunity> o = Database.query(Utils.getOpportunityFields() + ' where Id in: trigger.new'); *****************************************************************************************************/ public static string getOpportunityFields(sObject so) { List<String> f = new List<String>(); Map<String, Schema.SobjectField> fields = so.getSObjectType().getDescribe().fields.getMap(); string q = ''; // required otherwise the first one is null. integer c = 1; for (String s: fields.keySet()) { if (fields.get(s).getDescribe().isAccessible()) { if(c == 1) q += s; else q += ', ' + s; c ++; } } return q; }
All Answers