function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TehNrdTehNrd 

Can't write Apex tests against the LMA

We are buiding a system that will automatically provision License Seats when a customer purchases our product from our custom store front. All of this logic is in Apex and we are trying to write tests to ensure this works correclty but it seems this may not be possible.

 

In its simplest form the test would pefrom the following:

- Create a License

- Update the Seats__c field on the license

- Assert Seat__c field was updated correctly

 

Seem simple but it doesn not appear to work as this is the error received when trying to accomplish this: 

Update failed. First exception on row 0 with id a07e0000000ySK2AAM; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Argument 1 cannot be null: []

 

Below is the code to reproduce:

 

//Create account license can be attached to
Account acct = new Account(Name = 'test');
insert acct;

//Create package
sfLma__Package__c pack = new sfLma__Package__c(
	Name = 'test',
	sfLma__Developer_Name__c = 'test',
	sfLma__Developer_Org_ID__c = 'abc',
	sfLma__Latest_Version__c = '3',
	sfLma__Lead_Manager__c = UserInfo.getUserId(),
	sfLma__Package_ID__c = '3',
	sfLma__Release_Date__c = system.today().addDays(-30)
);
insert pack;

//Create a package version
sfLma__Package_Version__c packVersion = new sfLma__Package_Version__c(
	Name = 'test',
	sfLma__Package__c = pack.id,
	sfLma__Is_Beta__c = false,
	sfLma__Release_Date__c = system.today(),
	sfLma__Sequence__c = 1,
	sfLma__Version__c = '3',
	sfLma__Version_ID__c = '3'
);
insert packVersion;

//Ceate a license record
Id recordTypeId = [select Id from RecordType where Name = 'Active' and SobjectType = 'sfLma__License__c'].Id;
sfLma__License__c lic = new sfLma__License__c(
    RecordTypeId = recordTypeId,
    sfLma__Status__c = 'Active',
    sfLma__Seats__c = 1,
    sfLma__License_Type__c = 'Editable',
    sfLma__Account__c = acct.Id,
    sfLma__Expiration__c = system.today().addDays(365),
    sfLma__Install_Date__c = system.today(),
    sfLma__Package_Version__c = packVersion.Id
);
insert lic;

//Update the Seats field on the license
lic.sfLma__Seats__c = 5;

//UPDATE FAILS
update lic;

 

Any ideas?

 

Thanks,

Jason

Vinita_SFDCVinita_SFDC

Hello Jason,

 

The error 'FIELD_CUSTOM_VALIDATION_EXCEPTION' occurs when there is a validation rule and one of the tests running on org is not conforming to it.

 

Possible resolutions are to either modify the test or disable the validation rule temporarily.

TehNrdTehNrd

That is not what is happening in this case, there are no validation rules. That type of error, 'FIELD_CUSTOM_VALIDATION_EXCEPTION',  can also be displayed with the Apex .addError() method. The error message itself "Argument 1 cannot be null: []" is also a system generated message that happens when you call an Apex method with the incorrect arguments defined.

 

This is most likey waht is happening inside the LMA:

 

Account acct = new Account(Name = 'test');

try{

   acct.substring(null,5);

}catch(Exception e){
    acct.addError(e.getMessage());
}

 

Something like that would also cause the error message I am seeing to be displayed.

 

Really, I just need some internal expertise and guidance on how to write Apex tests for updating the Seats__c field on a License record.

 

Michael MarkhamMichael Markham
Howdy --

Did you ever make any progress on this?  I'm in the same boat now, trying to accomplish exactly what you were, and getting exactly the same result.

Brenda S FinnBrenda S Finn
Has anyone been able to resolve this issue? We are experiencing the same issue in production. It works in our sandbox which uses version 1.15 of the app but production fails and it is at version 1.17.

thanks in advance
brenda
Michael PaisnerMichael Paisner
Any progress on this?  This page marks it as fixed for Winter '15 Patch 13.0, but we are still getting this error when trying to deploy to production.

https://success.salesforce.com/issues_view?id=a1p300000008XTRAA2

Mike
Brenda S FinnBrenda S Finn
Mike

Our situation is somewhat unique in that we simply needed to get the required code coverage for our trigger on the sfLma_License__c object. We did not need a functional unit test at this point. So this may not be of help if you HAVE to do verification of the functionality. This is what we did.

As we could not create an instance of the sflMa Package in Production  and did not want to roll back to previous version (1.15),  we simply retrieve all existing licenses and call update on the first one returned, which ensures that our trigger for the License is executed and we have our code coverage.    Here is the code for getting all the licenses 

        List<sfLma__License__c> lstLicenses = [select Id, sfLma__Lead__c, sfLma__Package_Version__c FROM sfLma__License__c  
            where sfLma__License_Type__c='Editable'];
        if (!lstLicenses.isEmpty()) {
            // Add your functionality here for each license you find. For us, invoking update fires our trigger
            update lstLicenses.get(0);
        }

Hope this helps!
Brenda
JVIowaJVIowa
We are having the same issue.  It says it is fixed on both our sandbox org and production org but we still get the error.  Has anyone else figured this out?  Has anyone logged a case against the known issue saying it is not fixed in all cases?
Michael PaisnerMichael Paisner
We tried to open a case with them, but basically got the run around.  We developed a work around by querying on existing licenses.  Not pretty, but it works.
Mike
JVIowaJVIowa
I was able to open a case with developer support and they gave me the fix.  When you are creating the Package Version record you need to make sure you set the version id field.  Also, it has to be a valid version id.  So I copied on from one of our existing package version records and hard coded in the test.  Not sure what object the id is for because when I pasted it in the URL it took me to an All Package Versions page and not a custom object page.  I would have created that record in the test class if I could figure out what it was, since I couldn't I just hard coded.  Anyway, setting that field with a valid id solved the issue and the test passes now without having to query data from the org.
 
sfLma__Package_Version__c.sfLma__Version_ID__c = 'INSERT VALID ID HERE';