Access SObjects names dynamically from a List<SObject>

Access SObjects names dynamically from a List

Apex
Access SObjects names dynamically from a List A simple way to do this is by using getSObjectType().getDescribe().getName(); You can run that piece of code in your developer console. [code lang="php"] /********************************************************************** * * Access SObjects names dynamically from a List ************************************************************************/ List<SObject> objects = new List<SObject>(); Account account = new Account(Name = 'AccountSObject'); Contact contact = new Contact(LastName = 'ContactSObject'); objects.add(account); objects.add(contact); System.debug(objects.size()); for(SObject sObjectNames : objects) { String sObjName = sObjectNames.getSObjectType().getDescribe().getName(); System.debug(sObjName); } /* * */ [/code]
Read More
Populating an inner List in a Map

Populating an inner List in a Map

Apex
Populating a inner List in a Map collection A simple way to populate a list that inside a map collection could be done like this.You can run that piece of code in your developer console. [code lang="php"] /********************************************************************** * * Populating a List inside a Map collection * ************************************************************************/ List<Account> acclist = [SELECT Id, Name, (SELECT Id, lastName FROM Contacts) FROM Account LIMIT 50]; // This map will be contain the list of contacts as values and the // account ids as key. Map<Id,List<Contact>> accConMap = new Map<Id,List<Contact>(); for(account acc : acclist) { List<Contact> innerList = new List<Contact>(); for(contact c: acc.contacts) innerList.add(c); accConMap.put(acc.id, innerList); } // accConMap.put(acc.Id, innerList) will insert key and values to our map // [/code] As you can see it is quite simple.All you need to do…
Read More
Aborting Apex Jobs

Aborting Apex Jobs

Apex
Aborting Apex Jobs THere is a way to abort Apex Jobs that can be easily acomplish from the developer console. If you open an anonymous window in your developer console you can use the following SOQL. [code lang="html"] /*************************************************************************** * * Running this SOQL in an anonymous window will get information about all * active apex jobs in your org. * ****************************************************************************/ List&amp;amp;amp;lt;CronTrigger&amp;amp;amp;gt; crontList = [SELECT CreatedById, CreatedDate, CronExpression, CronJobDetailId, EndTime,Id, LastModifiedById,NextFireTime, OwnerId, PreviousFireTime, StartTime, State, TimesTriggered FROM CronTrigger]; // // [/code] CronTrigger "Contains schedule information for a scheduled job". Get the Id of the job you want to abort and execute this line on an anonymous window. [code lang="html"] // This line will abort the first entry on the crontList. // You need to make sure you have the…
Read More
You got to love them, Maps ..

You got to love them, Maps ..

Apex
Maps vs For loops, Round 1 In this example I want to use a technique that will help us to avoid inserting for loops inside other for loops. Let's say you want to compare a couple of fields from different objects, in many situations you will find something similar to this: [code lang="php"] //SOQL queries to retrieve our values List<Lead> leadList = [SELECT Id, Email FROM Lead WHERE Email!= null]; List<Contact> contactList = [SELECT Id, Email FROM Contact WHERE Email != null]; for(Lead l: leadList) { for(Contact c: contactList) { if(l.Email == c.Email) { system.debug('There are emails matching: ' + 'Contact Email:' + c.Email + ' with Lead Email: ' + l.email); } } } [/code] If you run this code in an anonymous window it will run fine. The…
Read More
Wrapper Class

Wrapper Class

Apex
Wrapper Class A Wrapper Class is an extremely useful class that you can use when you need to group a number of objects in one "container". You will be using this class especially in Visualforce controllers. A more technical definition will be something like: "A wrapper or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects" It is just a temporary container that can hold objects with their records for further calculations or logics within your code structure. We are going to build a table in a Visualforce page and display Contact and Account information in that same table using a wrapper class. Here is the initial class, class name WrapperDemoController and like you can see it contains on…
Read More