2

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.

1 Answer 1

1

Assuming "T_Contact__c" is a look up field below should fix the error:
1. Change "CustomerSigned" to "CustomerSignedid".
2. In line 11: Change the value from String to number.

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.CustomerSignedid = acc.T_Contact__c;
    ContractToAdd.ContractTerm = 12;
     ContractToAdd.Customer_Asset__c = acc.name;
     ContractsToCreate.add(ContractToAdd);
            }       
  } 
 if (ContractsToCreate.size() > 0)
    insert ContractsToCreate;
}

}

For the test class, can you make to sure to populate account field and any other required fields for the test data? Second error is because...well, you cannot update something which did not get inserted so once you resolve the first error, second one should automatically go away.

Updated: This is how your test class should look like:

@isTest
private class CreateContractTest {

   // test CreateContract.trigger
   private static testMethod void testCreateContractTrigger() {
    // insert test Account
    Account ac = new Account(name= 'Test Account');
    insert ac;
    //insert test contact
    Contact con = new Contact(firstName = 'test', lastName = 'Contact');
    insert con;

    // insert asset with update available false
    Customer_Asset__c testAsset = new Customer_Asset__c( name= 'test', account__c = ac.id, T_Contact__c = con.id, Support_Contract__c =  false);
    insert testAsset;

    Test.startTest();
    // now set update available to true
    testAsset.Support_Contract__c = true;
    // that should cause the trigger to create contracts
    update testAssets;
    Test.stopTest();
   }
}
6
  • Hi Sai, Thanks for the help. In regards to making sure I populate the account field and any other required fields, can you help me with that? I thought the helper was supposed to do that. After updating the class with your code (Many Thanks) I am getting the following error: Error:Apex trigger CreateContract caused an unexpected exception, contact your administrator: CreateContract: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Class.CreateContract.CreateContract: line 7, column 1 Jul 2, 2013 at 16:54
  • in line 7, change "acc.Support_Contract__c = true" to "acc.Support_Contract__c == true"... I will update the code.
    – Sai
    Jul 2, 2013 at 17:09
  • No problem.....is your test class working too?
    – Sai
    Jul 2, 2013 at 17:17
  • Not exactly. I'm missing a lot of code coverage. I'm also wrestling with customer signed by as management just removed all contact information from the asset(s) because it was messing up leads / opps. Jul 2, 2013 at 18:18
  • This is the error I am getting. Error Message System.ListException: List index out of bounds: 0 Stack Trace Class.CreateContractTest.testCreateContractTrigger: line 30, column 1 Jul 2, 2013 at 18:25

You must log in to answer this question.

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