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
BrandiTBrandiT 

Problem with Apex Class - Test for Update Trigger

I am writing a test class for a trigger.  I can save with no error messages, but when I run the test I get an error message:

MISSING_ARGUMENT, Id not specified in an update call

 

 

Here is the test class.  The part in red below is what I'm getting the message on.  I think I'm just not formatting it correctly.

 

 

@isTest
private class ContractTestFactory{
  private static TestMethod void ContractsonAccount()
    {    
    test.startTest(); 
      
    Account myAccount=new Account(name='testacct',recordtypeid='01270000000Dvnf',
    lead_converted_date__c=date.today());
    insert(myAccount);
    
    List<Contract> Contract= new List<Contract>();
    Contract myContract = new Contract(recordtypeid='01270000000DwX4',
            account=myAccount,StartDate=date.today());
    
    myContract.RPM_Signed_Contract__c=1;
    update(myContract);
    
    List<Account> insertedAccounts = [SELECT Name, RPM_Signed_Contracts__c 
                                      FROM Account];
    
    System.assertEquals(1,myAccount.RPM_Signed_Contracts__c);
              
    test.stopTest();  
    }    

 

Can anyone help me fix this test so that it runs correctly.  I really am very new to apex and have no idea how to go about fixing this message.

 

Thanks!!!

Best Answer chosen by Admin (Salesforce Developers) 
John De SantiagoJohn De Santiago

 

If you are trying to create a new Contract record then you need to use an insert command:

 

insert myContract;

 

...but if you really are trying to update an existing contract record then you need to add the Id when constructing the object:

 

Contact myContract = new Contract(id=contractId);

update myContract;

 

Also, you should put all of your test setup outside the "test.StartTest()" method. That way you can make sure the code that you are really trying to test doesn't run in to governor limits. If you do it the way you have it then it will count creating the account as part of your limits. 

 

Also, you shouldn't hardcode your record type ids. You should perform a lookup by name to get the recordtype ids. That way if your sandbox and production versions of the ids do not match or are changed your unit tests will still work as long as the entry still exists by name.

 

Hope this gets you started.

 

 

 

All Answers

John De SantiagoJohn De Santiago

 

If you are trying to create a new Contract record then you need to use an insert command:

 

insert myContract;

 

...but if you really are trying to update an existing contract record then you need to add the Id when constructing the object:

 

Contact myContract = new Contract(id=contractId);

update myContract;

 

Also, you should put all of your test setup outside the "test.StartTest()" method. That way you can make sure the code that you are really trying to test doesn't run in to governor limits. If you do it the way you have it then it will count creating the account as part of your limits. 

 

Also, you shouldn't hardcode your record type ids. You should perform a lookup by name to get the recordtype ids. That way if your sandbox and production versions of the ids do not match or are changed your unit tests will still work as long as the entry still exists by name.

 

Hope this gets you started.

 

 

 

This was selected as the best answer
BrandiTBrandiT

Ok I forgot my Insert statement.  That makes sense now.

 

thank you so much!