0

I have created unlocked package with all of necessary meta data. The problem is I've test class which content code with different object fields and to create package it's asking to add those fields in object folder. I don't want to add these custom fields in my unlocked package as it's not my feature.

Ex test class

Public class objectUtilTest{
 static Account setupAccount(String name, String customField) {
   Account objSetupAccount = new Account () ;
  objSetupAccount.Name = name;
  objSetupAccount.customField__c = customField
 }

  //some test methods for my feature

}

Here Account has custom field called customField__c. Which is used in my feature test class. 
In order to create package I've to add Account object meta data like below:

ProjectDirectory.. 
|__ force-app
      |__default
           |__main
                |__objects
                     |__Account
                          |__customField meta data

I don't want to add Account meta data in my package. Is there any way someone can suggest.

2 Answers 2

1

You have three basic choices, depending on your end goal. Each of your options will have a caveat, so you must choose your limitations.

First, you can make the package an org-dependent package. This means it won't validate that all the metadata exists during packaging. It also means it has to run all tests during install or upgrade, and will only complete if all the missing metadata validates during install time. This will limit the orgs the package can be installed to, and installations will take longer.

Second, you could specify the missing data as a dependency in another unlocked package (see the docs). When you do this, the other package must contain all of the metadata. During validation, that package will be installed into the same phantom org before your package is. This will allow your package to be installed in any org, as long as the dependencies are already installed at the minimum package version that your package version requires. Many packages can share the same dependency, though you'll have to coordinate with the people who are not working on your project.

Third, you could use dynamic apex to set the possibly unknown field:

Map<String, sObjectField> accountFields = sObjectType.Account.fields.getMap();
if(accountFields.containsKey('customField__c')) {
  objSetupAccount.put('customField__c', customField);
}

Of course, the last option is only viable if your code doesn't actually depend on that field at all, or can operate even without that field present.

1
  • To have a workaround for my package I've done by your ans from third point. In future I would like to get it done by two or dependent package as per your Second point. Thanks for suggesting options....
    – whoisnilu
    Commented Mar 30, 2023 at 10:54
0

If someone is looking for how I did it currently by doing some changes in testclass.

Public class objectUtilTest{
    static Account setupAccount(String customFieldAVar,String customFieldBVar) {
        Account objSetupAccount = new Account();
        Map<String, sObjectField> accountFields = sObjectType.Account.fields.getMap();
        Map<String,String> accCustomFieldsWithClassVariables = new Map<String,String>{
            'customFieldA__c'=>customFieldAVar,'customFieldB__c'=>customFieldBVar
                
        };
        for(String field : accCustomFieldsWithClassVariables.keySet()){
            if(accountFields.containsKey(field)){
                objSetupAccount.put(field, accCustomFieldsWithClassVariables.get(field));
            }
        }
        return objSetupAccount;
    }

    //some test methods for my feature

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .