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

VSCode ForceCode

VS Code
https://codecracksblog.wordpress.com/2017/10/30/use-visual-studio-code-for-salesforce-developmentide/

Good plugin, need to remember this link

Posted in Uncategorized | Leave a comment

Determine if field is a component of a compound field

Sometimes there is a need to determine if a field is a component of a compound field.
For example, compound fields components are not permissionable even though field describe isPermissionable method for them returns true. There might be other cases like making correspondence between parts of compound fields and even many more.

There are two tables in Salesforce which correspond to compound fields and its components.

If a field can be found in FieldDefinition table, it is a real (permissionable) field. If a field cannot be found in FieldDefinition table, but can be found in EntityParticle table, it is a part of compound field and not a separate independent field.

If you know DeveloperName of a SObject and DeveloperName of a field, you can use the following query to determine. For example, let’s assume we want to know if standard field MailingStreet on a Contact is a real field or a part of a compound field and also if Geolocation__Latitude__s field on Test__c custom object is a real field or a part of a compound field.

So these two queries will return no results, meaning that these fields are not real fields.

SELECT DurableId FROM FieldDefinition where EntityDefinitionId = 'Contact' AND QualifiedApiName = 'MailingStreet'
SELECT DurableId FROM FieldDefinition where EntityDefinitionId = 'Test__c' AND QualifiedApiName = 'Geolocation__Latitude__s'

Also we can find out all field components for an object using the following query. Let’s assume that we wonder about field components for Contact standard object or Test__c custom object.

SELECT QualifiedApiName, DeveloperName, DurableId FROM EntityParticle where EntityDefinitionId = 'Contact' AND IsComponent = true
SELECT QualifiedApiName, DeveloperName, DurableId FROM EntityParticle where EntityDefinitionId = 'Test__c' AND IsComponent = true

Finally, we can simply make direct queries to find details about these field component using the following queries.

SELECT QualifiedApiName, DeveloperName, DurableId FROM EntityParticle where EntityDefinitionId = 'Contact' AND QualifiedApiName = 'MailingStreet'
SELECT QualifiedApiName, DeveloperName, DurableId FROM EntityParticle where EntityDefinitionId = 'Test__c' AND QualifiedApiName = 'Geolocation__Latitude__s'

Also, another way to determine if a field is component or not is to check field describe getCompoundFieldName method results like following

System.debug(LoggingLevel.ERROR, '@@@ v: ' + Test__c.Geolocation__Latitude__s.getDescribe().getCompoundFieldName() );
System.debug(LoggingLevel.ERROR, '@@@ v: ' + Test__c.Geolocation__c.getDescribe().getCompoundFieldName() );
System.debug(LoggingLevel.ERROR, '@@@ v: ' + Contact.MailingStreet.getDescribe().getCompoundFieldName() );
System.debug(LoggingLevel.ERROR, '@@@ v: ' + Contact.MailingAddress.getDescribe().getCompoundFieldName() );

Hit like if you find this post super awesome.

Posted in apex, field, SOQL | Leave a comment

Using SFDX to auto login to your organizations.

SFDX provides an interesting way to login automatically to the organization you need.
The only disadvantage of this approach is that you would need to reconnect each time when you sandbox organization is refreshed.

To authenticate sandbox you need to execute the following command
sfdx force:auth:web:login -a BOND -r https://test.salesforce.com
when instead of BOND you provide the alias you need and instead of test.salesforce.com you might provide your own custom domain if login from generic test.salesforce.com is prohibited.

On the browser tab opened you need to enter your credentials and allow the connected app to keep authentication token.

Next time you need to login to your sandbox, you can just execute command
sfdx force:org:open -u BOND

and a new browser tab will be opened when your user will be automatically logged in.

Posted in salesforce, sfdx | Leave a comment

Preparing package.xml for custom metadata records and sending email containing it to the current user

Preparing package.xml for custom metadata records and sending email containing it to the current user


public class MD {
public static String buildPackageXMLForMD(SObjectType mdt, String whereClause){
String mdtName = [ select DeveloperName FROM EntityDefinition where QualifiedAPIName = :mdt.getDescribe().getName() ].DeveloperName;

List records = Database.query(‘SELECT DeveloperName FROM ‘ + mdt + whereClause );
List items = new List();
for (SObject r: records) {
items.add( mdtName + ‘.’ + r.get( ‘DeveloperName’ ) );
}
return ‘ \r\n ‘ + String.join( items, ‘\r\n ‘ ) + ‘\r\n CustomMetadata\r\n ‘;
}

public static void sendPackageXMLEmail(String attachmentContent) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new List{UserInfo.getUserId()});
mail.setSubject(‘Package.xml prepared’);
mail.setHtmlBody(”);
if (attachmentContent != null) {
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setFileName(‘package.xml’);
attachment.setBody(Blob.valueOf( attachmentContent));
mail.setFileAttachments(new Messaging.EmailFileAttachment[]{
attachment
});
}
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{
mail
});
}
}

and

MD.sendPackageXMLEmail(MD.buildPackageXMLForMD(Custom_Metadata__mdt.sObjectType,
‘ where Parent_Metadata__r.DeveloperName = \’Parent_Dev_Name\”));

Posted in salesforce | Leave a comment

Create package.xml for custom metadata records

Sometimes after creation of custom metadata records there is need to deploy them to other organization or to backup them in sandbox. Today I have written a code to build package.xml for that

Posted in salesforce, Uncategorized | Leave a comment