July, 2017
sObjectUtils class, utility
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
global with sharing class sObjectUtils { /** * This method accepts the ID of any object type and returns the full name, including prefix of the sObject * type to which it belongs. * @author cpeterson **/ public static Schema.SObjectType getObjectType(id subject){ Schema.SObjectType result; string target = subject; Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); string keyPrefix; for(Schema.SObjectType describe: gd.values() ){ keyPrefix = describe.getDescribe().getKeyPrefix(); if(keyPrefix != null && target.startsWith(keyPrefix)){ result = describe; break; //no need to keep looking } } return result; } public static sObject getAllFields(id subject){ Schema.SObjectType objectType = getObjectType(subject); Map<String, Schema.SObjectField> fields = objectType.getDescribe().fields.getMap(); string queryString = 'SELECT '; for(Schema.sObjectField field:fields.values()){ queryString += ' '+field.getDescribe().getName()+', '; } queryString = queryString.substring(0, queryString.length()-2)+' FROM '+objectType.getDescribe().getName()+' WHERE id = \''+subject+'\' LIMIT 1'; sObject result = Database.query(queryString); return result; } } |
Favorite
Validating an Id length(15 or 18)
1 2 3 4 5 6 7 |
static public String validateId(String Idparam) { String id = String.escapeSingleQuotes(Idparam); if((id.length() == 15 || id.length() == 18) && Pattern.matches('^[a-zA-Z0-9]*$', id)) { return id; } return null; } |
Favorite
Check InstanceOf of String or ID
1 2 3 4 |
static public Boolean isValidId(String strId) { Boolean isValid = (strId InstanceOf ID) ? true : false ; return isValid ; } |
Favorite
Position elements side by side within a single pageBlockSection column
LINK
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<apex:pageBlockSection title="Search Payment" columns="2" id="pbs0"> <!--script>twistSection(document.getElementById("{!$Component.pb0.pbs0}").childNodes[0].childNodes[0]); </script--> <apex:inputField value="{!PaymentResponseObj.Source_System__c}" /> <apex:inputField value="{!PaymentResponseObj.Invoice_Number__c}" /> <!--apex:inputField value="{!PaymentResponseObj.Invoice_Date__c}" label="Invoice Date" /--> <!--apex:pageblockSection > <apex:inputField value="{!PaymentResponseObj.Invoice_Date__c}" label="From Invoice Date" /> <apex:inputText value="{!invoiceDateTo}" id="dateTo" label="To Invoice Date" onfocus="DatePicker.pickDate(false, this, false);"> <apex:param value="{!invoiceDateTo}" /> </apex:inputText> </apex:pageblockSection--> <apex:pageBlockSectionItem> <apex:outputLabel value="Invoice Date" for="first" /> <apex:panelGrid columns="2"> <apex:inputField value="{!PaymentResponseObj.Invoice_Date__c}" style="width: 140px;" html-placeholder="From Invoice Date" /> <br></br> <apex:inputText value="{!invoiceDateTo}" id="dateTo" html-placeholder="To Invoice Date" onfocus="DatePicker.pickDate(false, this, false);"> <apex:param value="{!invoiceDateTo}" /> </apex:inputText> </apex:panelGrid> </apex:pageBlockSectionItem> <apex:inputField value="{!PaymentResponseObj.Vendor_Number__c}"/> <apex:inputField value="{!PaymentResponseObj.Vendor_Name__c}"/> <apex:inputField value="{!PaymentResponseObj.WBS__c}"/> <apex:inputField value="{!PaymentResponseObj.GL_Code__c}"/> <apex:inputField value="{!PaymentResponseObj.Cost_Center__c}"/> <apex:inputField value="{!PaymentResponseObj.Amount__c}"/> <apex:inputField value="{!PaymentResponseObj.Amount_Currency__c}" /> <apex:inputField value="{!PaymentResponseObj.Custom_32__c}"/> <apex:inputField value="{!PaymentResponseObj.Rep__c}" /> <apex:selectList value="{!PaymentResponseObj.Payee_Country__c}" size="1" multiselect="false"> <apex:selectOptions value="{!listOfCountry}" /> </apex:selectList> <apex:pageblockSectionItem > </apex:pageblockSectionItem> <apex:pageblockSectionItem > <apex:outputpanel > <div style="text-align: right; margin-bottom: 10px;"> <apex:commandButton style="text-align: center;" reRender="pb0" value="Clear Search" action="{!reset}" /> <apex:commandButton reRender="pb0" value="Search" action="{!search}" status="blockElement"/> </div> </apex:outputpanel> </apex:pageblockSectionItem> <apex:pageblockSectionItem ></apex:pageblockSectionItem> </apex:pageBlockSection> |
Favorite
creating dropdownlist from custom field in apex
LINK LINK2
1 2 3 4 5 6 7 8 9 10 11 12 13 |
List<Payment_Response__c> payResponseList = [select Payee_Country__c from Payment_Response__c] ; List<SelectOption> optionList = new List<SelectOption>(); optionList.add(new SelectOption( ' ' ,'---Select---')); Set<SelectOption> setCountry = new Set<SelectOption>(); for(Payment_Response__c r : payResponseList ) { setCountry.add(new SelectOption(r.Payee_Country__c, r.Payee_Country__c)); //ProductCatalogList1.add(new SelectOption(pc.Category__c,pc.Category__c)); //optionList.add(new SelectOption(u.Id , u.Name)); } optionList.addAll(setCountry); system.debug('..'+optionList.size()); |
Favorite
add the field api name in ApexPages.StandardController
LINK
1 2 3 4 5 6 7 8 9 10 11 |
public Payment_Response__c PaymentResponseObj {get;set;} public PaymentControllerExt(ApexPages.StandardController stdController) { stdController.addFields(new List<String>{'Payee_SAP_No__c', 'Payee_Country__c', 'Invoice_Date__c', 'Amount_Currency__c',stdController.addFields(new List<String>{'Payee_SAP_No__c', 'Payee_Country__c', 'Invoice_Date__c', 'Amount_Currency__c', 'Amount__c', 'WBS__c', 'GL_Code__c', 'Cost_Center__c', 'Company_Code__c'}); this.payReqObject = (Payment_Request__c)stdController.getRecord(); } |
Favorite
Use Set in dynamic SOQL IN-clause in method without Database.query()
1 2 3 4 5 |
public String inClausify(Set<Id> ids) { String inClause = String.format( '(\'\'{0}\'\')', new List<String> { String.join( new List<Id>(ids) , '\',\'') }); return inClause; } |
LINK LINK2 Favorite
get recordtype id, name
1 2 3 |
SELECT Id, Name, DeveloperName FROM RecordType WHERE SobjectType = 'obj_name__c' SELECT Id, RecordtypeId, RecordType.name FROM obj_name__c WHERE Id = '012i00000015pm9AAA' |
Favorite
From the Object Api name retrieving the SObject
1 |
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(sObjectType); |
or
1 2 3 4 5 6 7 8 |
id myid = 'a1a0n00000092jb'; String sObjectType = ((Id)myid).getSobjectType().getDescribe().getName(); public void from_string_object_name_to_sObject() { Schema.SObjectType targetType = Schema.getGlobalDescribe().get(sObjectType);//From the Object Api name retrieving the SObject Sobject Object_name = targetType.newSObject(); } |
Favorite