Wednesday, November 2, 2022

Apex life hacks

Dynamic string formation: --> \'\' --> \'''\' -->  \''+ +'\' --> \''+ test.Name +'\'

Query Custom Setting Records :

String regionQuery = 'SELECT Id, Name  FROM TestCustomSetting__c';

List<TestCustomSetting__c>  regionCustomSettings= Database.query(regionQuery);


Map<String, TestCustomSetting__c> countryMatrix = new Map<String, TestCustomSetting__c>();

        for(TestCustomSetting__c region : regionCustomSettings){

            countryMatrix .put(region.Name, region);

        }

system.debug('countryToRegionTaskMatrix==>'+countryToRegionTaskMatrix);

MAP: 

https://www.infallibletechie.com/2012/10/map-datat-type-in-salesforce.html

https://salesforce.stackexchange.com/questions/223189/get-values-from-a-map-in-apex

SObject Query:

         String typeName = 'Test__c';

        SObjectType objectType = Schema.getGlobalDescribe().get(typeName);

        Map<String,Schema.SObjectField> objectFields = objectType.getDescribe().fields.getMap();

        String query = '';

        Integer fieldCount = 0;

        for (Schema.SObjectField objectField : objectFields.values()) {

            if ('' + objectField != 'Test__c') {

                query += objectField + ' ';

                if ( fieldCount < objectFields.values().size() - 1) {

                    query += ', ';

                }

            }

            fieldCount++;

        }

     system.debug('query==>'+query);


Try catch exception Handling using System. debug:
       try{
           
        }
        catch(Exception objEx){
            System.debug(LoggingLevel.ERROR,objEx.getStackTraceString() +'\n'+ objEx.getMessage());
        }


Resolve RowCause in Apex class:
Remove "With sharing" from apex class 

For loop:
https://salesforce.stackexchange.com/questions/129706/which-one-is-better-loop-with-soql-or-create-a-list-then-loop

No comments:

Post a Comment

CSS Styling

Styling: 1. Inline Styling: < div style = "font-size: 2rem;font-weight: 400;" > Good Evening </ div > 2. Usi...