How to get Base URL in Salesforce from managed package context?

I have modified an answer which I have found here.

public class URLService {
public static String getBaseURL() {
PageReference pr = new PageReference('/id/' + UserInfo.getOrganizationId() + '/' + UserInfo.getUserId() + '?oauth_token=' + UserInfo.getSessionId() );
System.debug(LoggingLevel.ERROR, '@@@ v: ' + pr.getContent().toString() );
String data = pr.getContent().toString();
Map result = (Map)Json.deserializeUntyped(data);
Map urls = (Map)result.get('urls');
System.debug(LoggingLevel.ERROR, '@@@ v: ' + (String)urls.get('rest') );
String rest = (String)urls.get('rest');
String[] items = rest.split('/');
return items[0] + '//' + items[2] + '/';

Posted in salesforce | Tagged | Leave a comment

Determine by Apex if Custom Field allows setting Field Level Security

There is FieldDefinition standard object, which is available in usual SOQL and in Tooling API. However, this object has different fields available in different ways of accessing it.

It has a field IsFlsEnabled which is only available in Tooling API, but it is possible to mitigate this problem by checking if IsNillable is true and if PublishedId is not System.

Field Level Security is not available for standard fields (which have PublisherId equal to System and LastModifiedDate null) and for required fields which are not Nillable.


So, basically speaking, IsFLSEnabled = IsNillable AND PublisherId != System and even though we can’t access IsFLSEnabled  field in usual SOQL, we could calculate its value based on values of field IsNillable and PublisherId.

Like this post if it helps you or you find it interesting or beneficial in any way. Thanks for reading.

Posted in apex, field, salesforce | Tagged , , , , , , , , | Leave a comment

Efficient dynamic SObjectType retrieval in Salesforce

It is interesting how often we use inefficient code.

For example, if we want dynamically get SObjectType for given custom object by its String representation, how do we usually implement it?

Have you seen code like following?

public static SObjectType getSObjectTypeByName( String name ) {

return Schema.getGlobalDescribe().get( name );


Let’s assume we have totally 1907 standard and custom objects in the organization instance. You can determine the size by calling the following code:

System.debug(LoggingLevel.ERROR, ‘@@@ v: ‘ + Schema.getGlobalDescribe().size());

This code yields 1907 for client organization I am working on.

If we just measure how much CPU time is used for one call of such a method by executing the following code

SObjectType soType = getSObjectTypeByName(‘Custom_Object__c’);
System.debug(LoggingLevel.ERROR, ‘@@@ v: ‘ + Limits.getCpuTime() );

we will see that the time varies between 51 and 71 milliseconds for my organization.

While if we implement this in a different way like this
soType = ((SObject) Type.forName(‘Custom_Object__c’).newInstance()).getSObjectType();
System.debug(LoggingLevel.ERROR, ‘@@@ v: ‘ + Limits.getCpuTime() );

we will see that in this case time varies between 0 and 1 millisecond.

So it is 50-70 times more efficient on organization with large about of custom and standard objects.

Like this post if you find it beneficial or interesting for you. Thanks for reading.

Posted in apex, salesforce | Tagged , , , | Leave a comment

Trailhead API and Global Leaderboard

There is no public API to access Traihead data, but someone has implemented a service to retrieve that data
Also this is the public page to try that service
Another one has implemented a global Leaderboard

This is interesting since I also implemented a service to access public Trailhead data but I didn’t make it public yet.

Posted in salesforce, Trailhead, Uncategorized | Leave a comment

Shortcut to pass even handler to direct children Lightning components

Today I have discovered that this is possible to pass event handlers to children components like attributes.

Instead of writing code

<!-- parent.cmp -->
<aura:handler name="sampleComponentEvent" event="c:compEvent"
<c:child />

it is possible to write just

<!-- parent.cmp -->
<c:child sampleComponentEvent="{!c.handleChildEvent}"/>

assuming child has corresponding event

<!-- c:child -->
<aura:registerEvent name="sampleComponentEvent" type="c:compEvent"/>


Posted in lightning | Leave a comment

Apex Data Generation library

Today I am opensourcing and introducing my Apex Data Generation library, which can be called as a framework or as builder as well.

More than year back, I was considering of introducing my Salesforce unit test framework, however, I didn’t open source that version. I have reconsidered it for usage outside of unit tests and renamed to DataGenerator and later I decided that it is worth to make it extendable so I refactored that class to DataBuilder, AbstractDataBuilder classes and moved auxiliary methods into Helper class. This is a new version, still this might not be perfect but I guess it might useful in many applications.

I am interested to get a feedback from you.


Posted in salesforce, Uncategorized | Tagged , , , | Leave a comment