I am trying to create a test for an Apex class that displays how many of a particular item have been sold. I have no trouble creating the Apex class but am having trouble creating the test class. I need to create SO headers in the test class but can not create the SO header and or the SO line. I receive the following error when I try to create the SO header:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Cannot insert a Sales Order Header outside of the Rootstock Package.: []

Test Code:

 rstk__sohdr__c custH= new rstk__sohdr__c(rstk__sohdr_custno__c='a9w36000000GpJeAAK');
insert custh;

I receive the error on the execution of the insert statement.

How can I create a Rootstock SO from within a Salesforce Apex class?

  • 1
    Think you will need to contact the package vendor as that validation message looks like something they have built into their package.
    – Keith C
    Feb 4, 2018 at 17:33

1 Answer 1


I found the answer in Rootstocks documentation

// instantiate a new header
rstk__sohdr__c sohdr = new rstk__sohdr__c();
// identify a sales division for the order
rstk__sydiv__c salesDivision = [select Id, rstk__sydiv_mainsite__c from rstk__sydiv__c where rstk__sydiv_div__c = 'BTN'];
// populate division, order number, date
sohdr.rstk__sohdr_div__c = salesDivision.Id;
// Comment out the line below, using a double slash, as in this line
// when a system auto-generated order number is desired
sohdr.rstk__sohdr_order__c = 'K-1207-1';
sohdr.rstk__sohdr_orderdate__c = Date.Today();
// identify a customer for the order
rstk__socust__c customer = [Select Id From rstk__socust__c where Name = 'Regal Cinemas - Warren East (REG1444)'];
// set the customer ID
sohdr.rstk__sohdr_custno__c = customer.Id;
// identify a product for the order
rstk__soprod__c product = [select Id from rstk__soprod__c where 
rstk__soprod_prod__c = '10000D100062R' and rstk__soprod_div__c = :salesDivision.id];
// add a line
rstk__soline__c[] solines = new rstk__soline__c[] {};
rstk__soline__c line = new rstk__soline__c();
line.rstk__soline_firm__c = true;
line.rstk__soline_line__c = 1;
line.rstk__soline_prod__c = product.Id;
line.rstk__soline_qtyorder__c = 6;
line.rstk__soline_price__c = 101.99;
line.rstk__soline_taxamount__c = 47.23;
line.rstk__soline_duedate__c = Date.Today() + 10;
line.rstk__soline_shipdiv__c = salesDivision.Id;
line.rstk__soline_shipsite__c = salesDivision.rstk__sydiv_mainsite__c;
// create the order

You must log in to answer this question.

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