The code I am attempting to fix today revolves around Contracts and Customer Assets.
The idea being that when a customers asset is generated or updated the account manager will check off "Support Contract" and a contract will automatically be created.
I have a few issues with the code:
Class - Create Contract
public with sharing class CreateContract{
public void CreateContract(List<Customer_Asset__c> assets){
List<Contract> ContractsToCreate = new List<Contract>();
for(Customer_Asset__c acc:assets){
if (acc.Support_Contract__c = true){
Contract ContractToAdd = new Contract();
ContractToAdd.AccountId = acc.Account__c;
ContractToAdd.CustomerSigned = acc.T_Contact__c;
ContractToAdd.ContractTerm = '12';
ContractToAdd.Customer_Asset__c = acc.name;
ContractsToCreate.add(ContractToAdd);
}
}
if (ContractsToCreate.size() > 0)
insert ContractsToCreate;
}
}
The Error I am receiving for this is: 1) Error: Compile Error: Illegal assignment from Id to SOBJECT:Contact at line 10 column 9
Here is the Test Class: CreateContractTest
@isTest
private class CreateContractTest {
static List<Customer_Asset__c> getTestAssets(){
List<Customer_Asset__c> TestAssets = new List<Customer_Asset__c> {};
Customer_Asset__c ta = new Customer_Asset__c();
if (ta.Support_Contract__c = true){
TestAssets.add(ta);
}
return TestAssets;
}
// test CreateContract.ContractstoCreate
private static testMethod void testCreateContract() {
List<Customer_Asset__c> testAssets = getTestAssets();
CreateContract helper = new CreateContract();
Test.startTest();
helper.createContract(testAssets);
Test.stopTest();
}
// test CreateContract.trigger
private static testMethod void testCreateContractTrigger() {
// insert the assets with update available false
List<Customer_Asset__c> testAssets = getTestAssets();
testAssets[0].Support_Contract__c = false;
insert testAssets;
Test.startTest();
// now set update available to true
testAssets[0].Support_Contract__c = true;
// that should cause the trigger to create contracts
update testAssets;
Test.stopTest();
}
}
I am getting two errors when running this:
1. 15:53:21.733 (5733625000)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [AccountId]: [AccountId]
2. 15:53:22.332 (6332402000)|EXCEPTION_THROWN|[37]|System.DmlException: Update failed. First exception on row 0 with id a0OM0000002BNsJMAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateContract: execution of AfterUpdate
Here is the Trigger: CreateContract
trigger CreateContract on Customer_Asset__c (after insert, after update) {
// hold assets for which contracts need to be created
List<Customer_Asset__c> crUpdates = new List<Customer_Asset__c>();
for (Customer_Asset__c record:trigger.new)
{
// make sure we only include assets for which Support_Contract__c has been changed to true
if (record.Support_Contract__c && !trigger.oldMap.get (record.Id).Support_Contract__c){
crUpdates.add(record);
}
}
// call crClass only if there are any assets for which contracts need to be created
if (crUpdates.size() > 0){
CreateContract helper = new CreateContract();
helper.createContract(crUpdates);
}
}
I've been re-using code from my previous attempts, I'm just not sure what is causing the failure(s). Any advice would be greatly appreciated.