I was trying to setup and activate contract while releasing invoice after creating Sales Order.
But everytime I try to release, I get this error.
The message reads as follows:
Inserting 'Contract' record raised at least one error. Please review the errors. Error: 'Contract ID' cannot be empty.
But before that I have made Contract template and a Customer Contract for the Customer as well which uses the Contract template.
Here is the code that is raising this error.
private Contract CreateActivateContract(ContractMaint contractMaint, DateTime? invoiceDate, int? customerID, int? customerLocationID, string simCardID, string phoneNumber, CTBillEngine engine)
{
contractMaint.Clear();
//initialize new contract
Contract contract = (Contract)contractMaint.Contracts.Cache.CreateInstance();
contract = contractMaint.Contracts.Insert(contract);
//look up contract template id
Contract template = PXSelect<Contract,
Where<Contract.isTemplate, Equal<boolTrue>,
And<Contract.contractCD, Equal<Required<Contract.contractCD>>>>>
.Select(Base, "SIMCARD");
if(template==null)
{
throw new PXException("The SIMCARD contract template was not found.");
}
contract.TemplateID = template.ContractID;
contract.CustomerID = customerID;
contract = contractMaint.Contracts.Update(contract);
contract.LocationID = customerLocationID;
contract.StartDate = invoiceDate;
contract.ActivationDate = invoiceDate;
contract = contractMaint.Contracts.Update(contract);
//store simcardid and phone number into the contract attributes
foreach(CSAnswers attribute in contractMaint.Answers.Select())
{
switch(attribute.AttributeID)
{
case "SIMCARDID":
attribute.Value = simCardID;
contractMaint.Answers.Update(attribute);
break;
case "PHONENUM":
attribute.Value = phoneNumber;
contractMaint.Answers.Update(attribute);
break;
}
}
//save the generated contract
contractMaint.Save.Press();
//setup and actiave the contract
engine.SetupAndActivate(contract.ContractID, contract.ActivationDate);
return contract;
}
Below is the screenshot from Contract Template and Customer Contract screens.


