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 amend not latest commit message in pushed branch

git rebase -i HEAD~3
Replace pick with reword
Press Esc then :x! to save and close
Change commit message
Press Esc then :x! to save and close
git push –force origin feature branch

Posted in Git, Uncategorized | Leave a comment

My Aura to LWC migration article is published

My Aura to LWC migration article is finally published on corporate blog

I have written this article on February.

Also I have prepared the presentation on the same topic for internal knowledge sharing meeting inside a company and also for a Lviv Salesforce Development Meetup earlier this yeat.

Posted in Uncategorized | Leave a comment

Mapping Salesforce Profile Name to Profile Developer API Name

Earlier I have post a question on SSE about how to map Salesforce Profile Name to Profile Developer API Name.

Finally I have found solution.

While one of the possible point-and-click way is suggested by @user3375426 to create an outbound changeset and click add Profiles there, another programmatic way is to query Fullname field on Profile using Tooling API.

In Tooling APIname field corresponds to the displayed name of Profile and Fullname field corresponds to Developer API Name of Profile. It is not possible to query by Fullname and query to retrieve Fullname are limited by 1 row, so it is not possible to get Developer API Name of every profile at once, however, it is possible to query one by one and retrieve their API name in such way.

For example, to find ‘System Administrator` profile, the following query can be used

select id, name, fullName from Profile where name like '%admin%' limit 1

enter image description here

Also, we can verify that StandardAul is the Standard Platform User profile by making query

select id, name, fullName from Profile where name='Standard Platform User'

enter image description here

So the query results confirm that StandardAul is the Standard Platform User profile.

Just to summarize, the mappings for Standard Profiles in Professional Edition

Name                 API Name
Contract Manager     ContractManager
Marketing User       MarketingProfile
Solution Manager     SolutionManager
Read Only            ReadOnly
Standard User        Standard
System Administrator Admin

and the mappings for Standard Profiles in Enterprise Edition

Name                               API Name
Contract Manager                   ContractManager
Marketing User                     MarketingProfile
Solution Manager                   SolutionManager
Read Only                          ReadOnly
Standard User                      Standard
System Administrator               Admin
External Identity User             External Identity User
Guest License User                 Guest License User
High Volume Customer Portal User   High Volume Customer Portal User
Customer Community User            Customer Community User
Customer Community Plus User       Customer Community Plus User
Partner Community User             Partner Community User
Partner Community Login User       Partner Community Login User
Work.com Only User                 Work.com Only User
Chatter Only User                  Chatter Only User
Analytics Cloud Security User      Analytics Cloud Security User
Analytics Cloud Integration User   Analytics Cloud Integration User
Customer Community Login User      Customer Community Login User
Company Communities User           Company Communities User
Customer Community Plus Login User Customer Community Plus Login User
Standard Platform User             StandardAul

I have added here two screenshots, one for Professional Edition standard profiles mappings Profile name and API names for Professional Edition

and another one for Enterprise Edition standard profile mappings Profile name and API names for Enterprise Edition

Hit like if you find this post super-awesome.

Posted in salesforce, Uncategorized | 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