Wednesday, January 02, 2019

Summer 18 : Better way to retrieve RecordTypeId

Salesforce Summer ‘18 has added new method to get Developer Name for Record Types which give me such a huge relieve.

my sketch
Previously, we were only able to get RecordType using Id or Name. Usually in our code we always need to retrieve the Id but retrieving Id with Name (Label) using getRecordTypeInfosByName is not good idea as Name can be changed easily.

To solve the issue, we created one Global class to run SOQL just to retrieve ID, DeveloperName. It exposed our code to SOQL limit and we need to be careful with the design when we decided to use Static because the global class can be called by different method in the same transaction.

Now we can forget about that *tedious part*. I started to clean up my code to use Salesforce method instead of using SOQL to retrieve record type.

Caution : Update your class API version to the latest version to prevent error as this new method not recognized in old version.

Retrieve RecordType ID using DeveloperName

Id newRecordType = Schema.SObjectType.Opportunity.getRecordTypeInfosById().get(opportunityId).getDeveloperName();

Retrieve RecordType DeveloperName using ID

String recordType= Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get(recordTypeDeveloperName).getRecordTypeId();

Please update the underlined word above with correct value.

Tuesday, December 04, 2018

Test.loadData on Pricebook, Product, PriceBookEntry and RecordType

I remember when I was writing test class; Pricebook and the gang (Pricebookentry, Product) always giving me headache before I able to grab the concept. I also try to experiment on how to populate test data using Test.loadData on those object and quickly gave up as I was chasing for the dateline.

When I have a dateline, I always finding myself to choose familiar road although it can be slow because I know I can choose either to walk or run instead trying other new road which I unsure whether I able to walk or run or maybe eaten by monster.*who knows*

 I was once asked question here , the answer give me a hope that Test.loadData seem possible to be try on those object. After doing some experiments, here my conclusion :

1. Define relationship with Id and the Id is not necessary all integer. 

If you notice in my previous post, you will notice that I use all number to represent Id BUT it does not mean that we need to use all number. For Id, we can use alphanumeric so for example below. Or we can define Account Id like this; Account1, Account2 whatever we think it convenient and readable as we might have lot of records to populate.

Example 1

2. Use correct API Name regardless what.

Yes, that is the mantra.*what on earth you were thinking if you are not using API NAME* However common mistake on relationship –use correct API Name that define the Id. For standard object for example Opportunity, it use standard name such AccountId store Account Id. In case we create custom object, please check what is API Name that define the relationship. Some of common mistakes is using AccountId while actually it is Account__c .

3. RecordType IS NOT INSERTABLE from Test.loadData, therefore USE REAL ID

There is no such thing like creating csv file with all RecordType record *at least for this time being* and call Test.loadData. It will throw compilation error :

Method does not exist or incorrect signature: void loadData(Schema.SObjectField, String) from the type System.Test

If our object requires Record Type, then use real RecordTypeId . Example above in Example 1 shows how I populate record type id in csv file. The disadvantage of this : Id might be different so you need to update your csv file but the advantage is it easy to update and I found that unlikely to have different recordtype id in sandbox.*I leave this for you to decide*

4. Standard PriceBook IS NOT INSERTABLE , therefore USE REAL ID

*Do not even dream to insert Standard PriceBook * .Please remember in test class we need to use Test.getStandardPriceId() .HOWEVER we still able to populate PriceBookEntry using Test.loadData by using real Standard Price Book Id as example below. Note we still can populate custom PriceBook using Test.loadData .

5. First is first , last is last.Follow the sequence

Please ensure the sequence of insertion.This rule applies regardless using csv or code. PriceBookEntry cannot be inserted without PriceBook and Product , Opportunity cannot be inserted without Account.Follow the rules,  please put proper comment so other developer will not move the code. Put helpful Description in Static Resource and what it takes to help future generation.

I hope you find this article helpful. Let me know any method that you find useful.