Posts

Showing posts from July, 2021

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.