Showing posts with label Methods. Show all posts
Showing posts with label Methods. Show all posts

Sunday, October 28, 2012

Apex, check object by Id.

Some times we need to know if Id matches the object we need. This method returns true, if matches:
public static Boolean isIdsObjectTypeEqualToStringObjectType( String objectIdString,
                                                              String objectTypeNameString ){
    Map<String, Schema.SObjectType> globalDescrideMap = Schema.getGlobalDescribe();    
    Boolean isEqual = false;
            
    Map<String,String> keyPrefixMap = new Map<String,String>{};

    for( String objectItem : globalDescrideMap.keySet() ){
       Schema.DescribeSObjectResult objectDescribe =  globalDescrideMap.get(objectItem).getDescribe();
       keyPrefixMap.put( objectDescribe.getKeyPrefix(), objectDescribe.getName() );
    }
    
    if( keyPrefixMap.get( objectIdString.subString( 0, 3 ) ) == objectTypeNameString ) isEqual = true;
    
    return isEqual;
}
Recommend to to move initialization of globalDescrideMap to top of class. And of course test:
@isTest( seeAllData = false ) private static void testIsIdsObjectTypeEqualToStringObjectType(){
    Integer NUMBER_TO_INSERT = 5;
    List<Account> testAccountList = new List<Account>();
    List<Boolean> testEqualsList = new List<Boolean>();
    List<Boolean> testNotEqualsList = new List<Boolean>();
    String accountObjectType = Account.getsObjectType().getDescribe().getName();
    
    for( Integer i = 0; NUMBER_TO_INSERT > i; i++ )
        testAccountList.add( TestingUtils.createTestAccount( null ) );
        
    insert testAccountList;
    
    Test.startTest();
        for( Account accountItem : testAccountList )
            testEqualsList.add( BluewolfDevUtils.isIdsObjectTypeEqualToStringObjectType( ( String )accountItem.Id, accountObjectType ) );
        
        for( Account accountItem : testAccountList )
            testNotEqualsList.add( BluewolfDevUtils.isIdsObjectTypeEqualToStringObjectType( ( String )accountItem.Id, 'Account__c' ) );
    Test.stopTest();
    
    for( Boolean testEqualsItem : testEqualsList )
        System.assertEquals( true, testEqualsItem );
    
    for( Boolean testNotEqualsItem : testNotEqualsList )
        System.assertEquals( false, testNotEqualsItem );
}
Enjoy.

UTD: New method.

Issue with Logs and System.debug in Salseforce 2 Salseforce (S2S) connection.

The issue is that we cannot see any logs or System debugs when work is being done under S2S connection. For example we have Salseforce 2 Salesforce connection. In one of instances , lets call it a guest, we have trigger that fires before Object insert (or after). We add new object to main instance, and connection creates the same object on the guest. Salseforce does not let us see any debug logs while doing something under S2S connection. So what to do if we need to "debug" triggers and methods in connection? We tried to solve this issue with creating a temporary field in the Object. Something like "LogField" - Long Text Area. In triggers and classes add code e.g.:
Object.Log__c  = '@someVariable: '   someVariable   '@';
After the work is done we can see "logs" in LogField of the Object. Also if there were some errors while running the trigger or with creating the Object you can always see them on Connection page in Logs.

Enjoy.

Salesforce: Unified object creator.

Everytime when we write test classes, we need to a create a big number of data to apply them to the methods that we are testing. We create a lot of methods leading to the creation of these or those objects. I have created the method leading to the creation of any type objects we need:
public with sharing class Utilities {
    /*
        Method for creating any Object
    */
    
    public static List<sObject> createObject(Integer numberToCreate, 
                                             Boolean doInsert, 
                                             Schema.SObjectType objectType, 
                                             Map<String, String> fieldsMap){    
        List<sObject> objectsList = new List<sObject>();
        List<String> objectFields = new List<String>();
        List<String> fieldsList = new List<String>();
        
        objectFields.addAll(objectType.getDescribe().fields.getMap().keySet());
        fieldsList.addAll(fieldsMap.keySet());
        
        if(numberToCreate > 0){
            for( Integer countOfObjects = 0; countOfObjects < numberToCreate; countOfObjects++ ){
                SObject currentObject = objectType.newSObject();
                
                for(String fieldFromList : fieldsList){
                    for(String fieldFromObject : objectFields){
                        if(fieldFromObject == fieldFromList){
                            currentObject.put(fieldFromList , fieldsMap.get(fieldFromList));
                        }
                    }
                }    

                objectsList.add(currentObject);
            }

            if ( doInsert ) insert objectsList;
        }
        
        return objectsList;
    }
}
Also I have added test class for it:
@isTest
private class TestUtilities {
    private static final Integer TEST_RECORD_COUNT = 10;
    
    static Map<String, String> fieldsMap;    
    static Schema.SObjectType objectType;
    static List<String> objectFields = new List<String>();
    static String query;
        
    private static void setup(){
        objectType = Account.getsObjectType();
        objectFields.addAll(objectType.getDescribe().fields.getMap().keySet());
        fieldsMap = new Map<String, String>{'Name' => 'Test',            //Fill in fields
                                            'BillingCountry' => 'Usa',    //IMPORTANT You need to fill in requaired fields
                                            'AccountSource' => 'Web'};
    }
    
    private static testmethod void testCreateObject(){
        List<sObject> testCreatedObjects;
        List<sObject> objectsFromDB;
        List<String> fieldsFromBD = new List<String>();
        
        setup();
        
        Test.startTest();
            testCreatedObjects = Utilities.createObject(TEST_RECORD_COUNT,
                                                           true,
                                                           objectType,
                                                           fieldsMap);
        Test.stopTest();
        
        query = 'SELECT Id';
        
        fieldsFromBD.addAll(fieldsMap.keySet());
        
        for(String fieldDB : fieldsFromBD){
            for(String fieldObject : objectFields){
                if(fieldDB == fieldObject){
                    query += ', ' + fieldDB;
                }
            }
        }
        
        query += ' FROM ' + objectType + ' WHERE Id IN : ' + 'testCreatedObjects' +  ' ';
        
        objectsFromDB = Database.query(query);        
        
        System.assertEquals( TEST_RECORD_COUNT , objectsFromDB.size() );
    }
}
You can also add compare of every field it test.

Enjoy.