Posts

Showing posts from March 28, 2023

Dynamic SOQL based on Sobject Name and Where Clause

 public class DynamicSOQLUtil {     // Returns a dynamic SOQL statement for the whole object, based on object name and where clause as a dynamic     public static string getCreatableDynamicFieldsSOQL(String objectName, String whereClause){                  String selects = '';         // Check whereclause if null then return null         if (whereClause == null || whereClause == ''){ return null; }                  // Get a map of field name and sobject field         Map<String, Schema.SObjectField> objFieldMap = Schema.getGlobalDescribe().get(objectName.toLowerCase()).getDescribe().Fields.getMap();         list<string> selectFields = new list<string>();                  if (objFieldMap != null){             for (Schema.SObjectField objFt : objFieldMap.values()){ // loop through all field tokens                 Schema.DescribeFieldResult fd = objFt.getDescribe(); //field describe  for each field                //if (fd.isCreateable()){ // if needed onl

Get fields in SOQL from FIeldSet

global class BatchclassName implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts{       global Database.QueryLocator start(Database.BatchableContext bc) {         return gAllRecords();     public Database.QueryLocator gAllRecords(){      try{ //Create fieldset on Object and get fields in SOQL query, BankFieldSet this fieldset name on bank object String queryString = 'Select Id'; for(Schema.FieldSetMember fieldMember : sObjectType.Bank__c.FieldSets.BankFieldSet.getFields()){ queryString += ', '+fieldMember.getFieldPath();} system.debug('queryString@@@'+queryString); queryString+=' FROM Bank__c WHERE isActive__c =true AND Location__c=\'Noida\' AND IsNeeded__c= true AND Status__c IN (\'Failed\',\'Unsuceess\') LIMIT '+Integer.valueOf(Label.LabelName); return Database.getQueryLocator(queryString); }catch(Exception ex){ ErrorLog__c log = new ErrorLog__c(); log.Message

In Schedule class run next job from configuration level

global class Schedularclassname implements Schedulable {   global void execute(SchedulableContext SC) {        List<AsyncApexJob> jobs = new List<AsyncApexJob>(); Id thisClassId = [SELECT Id, Name FROM ApexClass WHERE name = 'BatchClassName' LIMIT 1].Id; //Setup Custom Setting Schedular_Settings__c store the time and no of record process in batach class  //Scheduled_Time__c  store time when next batch will run //Schedular_Records__c add batch size how many record will process Schedular_Settings__c objschedule = Schedular_Settings__c.getInstance('Schedular'); if(objschedule!=null && objschedule.Scheduled_Time__c != null && objschedule.Schedular_Records__c != null){ jobs = [select id from AsyncApexJob where (status = 'Processing' OR status = 'Holding' OR status = 'Queued' OR status = 'Preparing') AND ApexClassId = :thisClassId AND JobType = 'BatchApex']; if(jobs == null  || jobs.size()

Get Last day of Month

Get Last day of a month (28, 29, 30 or 31)? Formula DAY(ADDMONTHS(DATE(YEAR(DateField__c), MONTH(DateField__c),1),1)-1) The trick here is to take the 1st of the month and year of the date field, add 1 month to it and subtract 1 day. For example, if my date field is March 3rd 2022, the formula will add 1 month to March 1st 2022 and will return April 1st 2022. We then subtract 1 day to get March 31st 2022.