+ Start a Discussion
BeekmanBeekman 

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. 
/*****************************************************************************************************
	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;
	}

 
Best Answer chosen by Beekman
surasura
As you said it is really simple , just creat a instance of the object you want to qyery and pass it to the method , sample code is below
 
Opportunity opp= new Opportunity() ;

String QueryString = ClassNameOfYourMethod.getOpportunityFields(opp);

 

All Answers

surasura
As you said it is really simple , just creat a instance of the object you want to qyery and pass it to the method , sample code is below
 
Opportunity opp= new Opportunity() ;

String QueryString = ClassNameOfYourMethod.getOpportunityFields(opp);

 
This was selected as the best answer
BeekmanBeekman
Thanks, figured it was easy and my mind is not with it today.  That was the key.  :)