Revision: 29997
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 6, 2010 02:55 by d3developer
Initial Code
// see: http://community.salesforce.com/t5/Apex-Code-Development/Building-a-reusable-JSON-component/m-p/172428 // and: http://joe-ferraro.com/2009/04/extjs-vf-json/ public static String getJsonFromObject(String objectName, List<String> columns, String filterString) { if(filterString == null) filterString = ''; String jsonData = '[ '; try { //System.debug('getJsonifiedObjectRecords for:' + objectName); String queryString = 'Select id,'; for(String c: columns) queryString += c + ','; queryString = queryString.substring(0,queryString.length() - 1); queryString += ' from ' + objectName + ' ' + filterString; queryString += ' limit 10000'; //System.debug('QUERYSTRING:' + objectName); List<sObject> sos = Database.query(queryString); //Map<String, Schema.SObjectField> columns = sos.getSObjectType().getDescribe().fields.getMap(); //jsonData += '['; for (sObject so : sos) { jsonData += ' { '; for(String column : columns) { System.debug('jsonData:' + jsonData); try { String cellval = ''; List<String> cParts = column.split('[.]'); if(cParts.size() == 2) { String outerObjectName = cParts.get(0); //if(outerObjectName.endsWith('__r')) // outerObjectName = outerObjectName.substring(0,outerObjectName.length() - 1) + 'c'; cellval = (String) so.getSObject(cParts.get(0)).get(cParts.get(1)); } else { cellVal = String.valueOf( so.get(column)); } jsonData += '"'+column+'" : "'+cellVal+'",'; } catch(Exception ex) { System.debug(ex); } } jsonData = jsonData.substring(0, jsonData.length() - 1); jsonData = jsonData + '},'; } jsonData = jsonData.subString(0,jsonData.length() - 1); } catch(Exception e) { System.debug('Bad query in Jsonify Call ' + e); } jsonData += ']'; System.debug('jsonData:' + jsonData); return jsonData; }
Initial URL
Initial Description
Initial Title
@fractastical Jsonify SObject
Initial Tags
Initial Language
Other