/** @ Name SFApexUtilFunctions @ Author Aslam Bari @ Created Date 31 March, 2011 @ Description A utility methods class for helping developers community */ public class SFApexUtilFunctions{ /** @ Name join @ Author Aslam Bari @ Created Date 31 March, 2011 @ Desctiption Join one list of String to a full string @ Param List: list of string data @ Param separator: the string by which the list is joined */ public static string join(List lstArg, String separator){ String fullString = ''; for(String item: lstArg){ fullString += item + separator; } if(fullString.length() > 0) fullString = fullString.substring(0, fullString.length() - 1); return fullString; } /** @ Name getUrlContents @ Author Aslam Bari @ Created Date 31 March, 2011 @ Desctiption Fetch the contents of remote url @ Param url: remote url with all parameters attached to url for GET call */ public static string getUrlContents(String url){ HttpRequest request = new HttpRequest(); request.setEndPoint(url); request.setMethod('GET'); Http call = new Http(); HttpResponse response = call.send(request); return response.getBody(); } /** @ Name makeCondition @ Author Akhielsh Soni @ Created Date 31 March, 2011 @ Desctiption Used to create condition part for dynamic query generation @ Param name of field on which the condition is to be applied @ Param type of field for determining the way of condition generation @ Param value to be searched @ Caution when passing date or date time values to be searched in range then that will be concatenated with ~ symbol ie. startdate~enddate and these must be in dd/MM/yyyy format */ public static String makeCondition(String fieldName,String fieldType,String value){ String criteria =''; if(value != null && value != ''){ if(fieldType=='STRING'){ criteria = fieldName + ' like \'%' + value + '%\''; } else if(fieldType=='BOOLEAN'){ if(value!='All'){ criteria = fieldName + '=' + boolean.valueOf(value); } } else if(fieldType=='DATE'){ List dateRange = value.split('~'); if(dateRange.size()>0){ List lstFirstDate ; List lstSecondDate ; //making sure to date ranage is present in list if user have not passed if(dateRange.size()==1){ dateRange.add(''); } //when from date range is given if(dateRange[0] != null && dateRange[0] != ''){ lstFirstDate = dateRange[0].split('/'); criteria = fieldName + '>=' + lstFirstDate[2] + '-' +lstFirstDate[0] + '-' +lstFirstDate[1]; //when to date range is given if(dateRange[1] != null && dateRange[1] != ''){ lstSecondDate = dateRange[1].split('/'); criteria += ' AND ' + fieldName + '<=' + lstSecondDate[2] + '-' +lstSecondDate[0] + '-' +lstSecondDate[1]; } } //when from date range is not given handle on to date range else{ if(dateRange[1] != null && dateRange[1] != ''){ lstSecondDate = dateRange[1].split('/'); criteria = fieldName + '<=' + lstSecondDate[2] + '-' +lstSecondDate[0] + '-' +lstSecondDate[1]; } } } } else if(fieldType=='DATETIME'){ List dateRange = value.split('~'); if(dateRange.size()>0){ List lstFirstDate ; List lstSecondDate ; //making sure to date ranage is present in list if user have not passed if(dateRange.size()==1){ dateRange.add(''); } //when from date range is given if(dateRange[0] != null && dateRange[0] != ''){ lstFirstDate = dateRange[0].split('/'); criteria = fieldName + '>=' + lstFirstDate[2] + '-' +lstFirstDate[0] + '-' +lstFirstDate[1] + 'T00:00:00Z'; //when to date range is given if(dateRange[1] != null && dateRange[1] != ''){ lstSecondDate = dateRange[1].split('/'); criteria += ' AND ' + fieldName + '<=' + lstSecondDate[2] + '-' +lstSecondDate[0] + '-' +lstSecondDate[1] + 'T00:00:00Z'; } } //when from date range is not given handle on to date range else{ if(dateRange[1] != null && dateRange[1] != ''){ lstSecondDate = dateRange[1].split('/'); criteria = fieldName + '<=' + lstSecondDate[2] + '-' +lstSecondDate[0] + '-' +lstSecondDate[1] + 'T00:00:00Z'; } } } } } return criteria; } }