Posts

Populate child records field value on parent record

  Populate child records field value on parent record by using apex trigger when child   record is inserted. *** trigger ContactTrigger on Contact (after insert) {     if(Trigger.isAfter && Trigger.isInsert){         ContactTriggerHandler.populateNameOnParent(Trigger.new);     } } *** *** // populate child records name on parent field when child record is inserted     public static void populateNameOnParent(List<Contact> contactList){         Map<Id,List<Contact>> contactWithAccId = new Map<Id,List<Contact>>();         Set<Id> accountIds = new Set<Id>();         List<Account> accountsList = new List<Account>();         String concatenateString;         for(Contact cont : contactList){             if(cont.AccountId != null)             {                 accountIds.add(cont.AccountId); // get account id from contacts             }         }          // get all contacts related to account ids         if(accountIds.size() > 0

Get picklist values in apex

  We can get picklist values and their labels in apex. *** //get picklist values     public static void getPicklistValues(){         Map<String, String> pickListValuesMap = new Map<String, String>();         Schema.DescribeFieldResult fieldResult = Account.Industry .getDescribe();         List<Schema.PicklistEntry> pEntry = fieldResult.getPicklistValues();         for( Schema.PicklistEntry pickListVal : pEntry){             pickListValuesMap.put(pickListVal.getValue(),pickListVal.getLabel());         }              System.debug('pickListValuesMap ##'+ pickListValuesMap);     } By using this method you can get any picklist values from any object(Standard or Custom). ***

Get current user information in apex

Image
  We can get current logged in user full information from predefined methods in apex. *** // get current user id         String loggedUserId = UserInfo.getUserId(); // get current user name         String loggedUserName = UserInfo.getName();   // get current user username         String loggedUserUserName = UserInfo.getUserName();          // get current user email         String loggedUserEmail = UserInfo.getUserEmail();          // get current user language         String loggedUserLanguage = UserInfo.getLanguage();          // get current user profile id         String loggedUserProfileId = UserInfo.getProfileId();          // get current user role id         String loggedUserRoleId = UserInfo.getUserRoleId();          // get current user locale         String loggedUserLocale = UserInfo.getLocale();                  System.debug('loggedUserId $$ ' + loggedUserId);         System.debug('loggedUserUserName $$ ' + loggedUserUserName);         System.debug('loggedUs

Show only DML exception message from parent exception

Image
In below code we just put account name is blank as we know it is required field so when DML will be execute then it will throw an exception. *** public static void showDMLException(){         Account acctRecord = new Account();         acctRecord.Name = '';         try{             INSERT acctRecord;         }catch(Exception ex){             System.debug('exception$$ ' + ex);             if(ex.getTypeName() == 'System.DMLException' || ex.getTypeName() == 'System.MailException'){                 System.debug('dml exception$$ ' + ex.getDMLMessage(0));                }         }     } *** System.debug('exception$$ ' + ex); This statement will show complete exception message. But we need only DML exception message so this message we can get from below conditions. if(ex.getTypeName() == 'System.DMLException' || ex.getTypeName() == 'System.MailException'){      System.debug('dml exception$$ ' + ex.getDMLMessage(0));    }

Get sObject prefix

  Pass object name in method parameter. *** public static void getPrefix(String objName){         Schema.sObjectType sObjType = Schema.getGlobalDescribe().get(objName);         String prefixValue = (sObjType.getDescribe().getKeyPrefix());         System.debug('prefixValue##' + prefixValue);     } *** OR you can get prefix value from here *** Schema.DescribeSObjectResult r = Account .sObjectType.getDescribe(); String keyPrefix = r.getKeyPrefix(); System.debug('keyPrefix##' + keyPrefix); *** Replace Account object from your object name.