Debug “Validation Errors While Saving Record(s)” error message when calling Test.loadData()

Sometimes when you use Test.loadData() you might receive some confusing error message saying “Validation Errors While Saving Record(s)”, it is hard to understand what is wrong in data so it is possible to use the following code to debug the problem

StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'TestData' LIMIT 1];
String body = sr.Body.toString();
String[] fields = body.split('\r\n')[0].split(',');
SObject[] records = new List();
String[] lines = body.split('\r\n');
for ( Integer j = 1; j < lines.size(); j++ ) {
SObject r = new Custom_Object__c();
String[] data = lines[j].split(',');
for (Integer i = 0; i < fields.size(); i++ ) {
System.debug(LoggingLevel.ERROR, '@@@ fields[i]: ' + fields[i] + ' + data[i] ' + data[i] );
r.put( fields[i], data[i]);
records.add( r );
insert records;

So when you run a code like this you will find out the exact problem you face

Posted in salesforce | Leave a comment

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