sfvfapex

A placeholder for my Salesforce and Apex notes

Useful Apex Utility Methods

Get a list containig the Ids from a list of Objects

 /*
  * Return a list of ids from a list of sObjects
  */
  public List getListOfIds(List objects){
  	if(objects == null){
  		return new List();
  	}
  	List idList = new List();

  	for(Sobject o : objects){
  		idList .add((ID)o.get('Id'));
  	}
  	return idList;
  }

Useful method for getting “foreign keys” from a list of object.

 /*
  * Returns set of ids for given field name.
  */
  public static Set getReferenceIDSet(List objects, String fieldName){
    Set ids = new Set();
    if(objects == null){
  		return ids;
    }
  	for(Sobject o : objects){
  	  try{
  	    if(o.get(fieldName) != null){
  		  ids.add((ID)o.get(fieldName));
  	    }
  	  }catch(Exception e){
  	  	//Inavlid field, so this one is skipped
  	  }
  	}
  	return ids;
  }
1 Comment »

Follow

Get every new post delivered to your Inbox.