When writing Test Class, sometimes need a batch of ID data or ID as parameters, etc., in the case of complex data relations to build real data some trouble, so find a way to generate ID can be used to assist the test!
private static Integer Fakeidcount = 0;
private static final String Id_pattern = ' 000000000000 ';
/**
* Generate a fake Salesforce Id for the given Sobjecttype
*/
public static Id Generatefakeid (Schema.sobjecttype sobjecttype) {
String Keyprefix = Sobjecttype.getdescribe (). Getkeyprefix ();
fakeidcount++;
String fakeidprefix = id_pattern.substring (0, 12-fakeidcount.format (). Length ());
Return id.valueof (Keyprefix + fakeidprefix + fakeidcount);
}
Invoke Demo:
Id Fakeid = Testhelper.generatefakeid (Account.sobjecttype);
This does not apply to any situation, there are some Object Id type value is not allowed to manually assign values, the only good to build a true record!!!
Salesforce Build test Available IDs