Posts

Showing posts from August, 2016

How to Query all fields in Apex ( SELECT (ALL) * FROM SObject )

Hello Guys, We have situations where we need to query all fields from any SObject in salesforce and because in SOQL we have to maintain resource allocation therefore we don't have option such as to query like SELECT * FROM SObjectName like we used to have in SQL. So here is the utility method that you can use to fetch all field names for a particular SObject and return as string: Method:   public static String fetchFieldNames(String sObjectName){       String SobjectApiName = sObjectName;       Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();       Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();       String commaSepratedFields = '';       for(String fieldName : fieldMap.keyset()){           if(commaSepratedFields == null || commaSepratedFields == ''){               commaSepratedFields = fieldName;           }else{               commaSepratedFields = commaSe

Utiity method to get week number of month by passing a Datetime value

Hi Techies, Here below is the Method that you can use in order to fetch week number in a month: for ex: Pass                ->     Result .......................................... 1July2016                 1 8July2016                 2 22July2016               4 and so on.... Method :     public Integer weekOfMonth( Datetime dateVar){                 Date currentDate = dateVar.Date();                 Integer weekCount = 0;                 Integer startWeekResidue = 0;                 Integer endWeekResidue = 0;                 //Calculating startWeekResidue                 Date dt = currentDate.toStartOfMonth().addDays(-1);                 Date dtFirstWeekend = dt.toStartOfWeek().addDays(6);                 startWeekResidue = dt.daysBetween(dtFirstWeekend);                 //Calculating endWeekResidue                 Date dtLastWeekend = currentDate.toStartOfWeek().addDays(-1);                 endWeekResidue = dtLastWeekend.daysBetween(currentDate);