Tag Archives: Apex

Schedule An Apex Job To Run Every 15 Minutes

Sample Code:

YourScheduledApexClass obj = new YourScheduledApexClass();

String sch1 = '0 0 * * * ?';
System.schedule('Schedule Job1', sch1, obj);

String sch2 = '0 15 * * * ?';
System.schedule('Schedule Job2', sch2, obj);

String sch3 = '0 30 * * * ?';
System.schedule('Schedule Job3', sch3, obj);

String sch4 = '0 45 * * * ?';
System.schedule('Schedule Job4', sch4, obj);

Schedule An Apex Job To Run Every 10 Minutes

Sample code:

YourScheduledApexClass obj = new YourScheduledApexClass();

String sch1 = '0 0 * * * ?';
System.schedule('Schedule Job1', sch1, obj);

String sch2 = '0 10 * * * ?';
System.schedule('Schedule Job2', sch2, obj);

String sch3 = '0 20 * * * ?';
System.schedule('Schedule Job3', sch3, obj);

String sch4 = '0 30 * * * ?';
System.schedule('Schedule Job4', sch4, obj);

String sch5 = '0 40 * * * ?';
System.schedule('Schedule Job5', sch5, obj);

String sch6 = '0 50 * * * ?';
System.schedule('Schedule Job6', sch6, obj);

Return Custom Error Messages From Apex Controller In Salesforce Lightning

Sample Code:

We can use System.AuraHandledException to return custom error message from server side apex controller to lightning component in Salesforce.

string errorMessage = 'Your Error Message';
AuraHandledException auraEx = new AuraHandledException(errorMessage);
auraEx.setMessage(errorMessage);
throw auraEx;

Get Record Type Id With Dynamic sObject Name & Record Type Name

Sample Methods:

 //Get recordTypeId by Object Name & Record Type Name
    public static Id recordTypeIdByName(string objAPIName, string recTypeName){
        Id recTypeId;
        if(String.isNotBlank(objAPIName) && String.isNotBlank(recTypeName)){
            recTypeId= Schema.getGlobalDescribe().get(objAPIName).getDescribe().getRecordTypeInfosByName().get(recTypeName).getRecordTypeId();
        }  
        return recTypeId;  
    }
    
    //Get recordTypeId by Object Name & Record Type Developer Name
    public static Id recordTypeIdByDevName(string objAPIName, string recTypeDevName){
        Id recTypeId;
        if(String.isNotBlank(objAPIName) && String.isNotBlank(recTypeDevName)){
            recTypeId= Schema.getGlobalDescribe().get(objAPIName).getDescribe().getRecordTypeInfosByDeveloperName().get(recTypeDevName).getRecordTypeId();
        }  
        return recTypeId;  
    }
    
    //Get all recordTypeId Map (Name as key and recordTypeId as value) by Object Name
    public static Map<String,Id> recordTypeMap(string objAPIName){
        Map<String, Id> recTypeNameIdMap = new Map<String,Id>();
        if(String.isNotBlank(objAPIName)){
            for(Schema.RecordTypeInfo recInfo : Schema.getGlobalDescribe().get(objAPIName).getDescribe().getRecordTypeInfosByName().values()){
                recTypeNameIdMap.put(recInfo.getName(),recInfo.getRecordTypeId());
            }
        }
        return recTypeNameIdMap;
    }

//Get Record Type Id or developer name by passing developer name or Id
public static string getRecordTypeInfoByIdOrDeveloperName(String recordType){
    string returnValue = '';
    Boolean isValidId = recordType instanceOf Id;
    
    for(Schema.SObjectType objType : Schema.getGlobalDescribe().Values()){
        Schema.DescribeSObjectResult doResult = objType.getDescribe(SObjectDescribeOptions.DEFERRED);
        if(isValidId){
            if(doResult.getRecordTypeInfosById().get(recordType) != null){
                returnValue = doResult.getRecordTypeInfosById().get(recordType).getDeveloperName();
                break;
            }
        }else{
            if(doResult.getRecordTypeInfosByDeveloperName().get(recordType) != null){
                returnValue = doResult.getRecordTypeInfosByDeveloperName().get(recordType).getRecordTypeId();
                break;
            }
        }
    }
    returnValue;
}