1

In the Lightning Components Developer Guide documentation on Testing Your Apex Code, it gives this code example:

@isTest
class TestExpenseController {
    static testMethod void test() {
        //Create new expense and insert it into the database        
        Expense__c exp = new Expense__c(name='My New Expense',
                         amount__c=20, client__c='ABC',
                         reimbursed__c=false, date__c=null);
        ExpenseController.saveExpense(exp);
     // ^^This line here*******************

    //Assert the name field and saved expense
    System.assertEquals('My New Expense',
                       ExpenseController.getExpenses()[0].Name,
                      'Name does not match');
    System.assertEquals(exp, ExpenseController.saveExpense(exp));
    }
}

What's the purpose of that line of code that I commented below it? The reason I ask is it was throwing errors when I replicated it in my test class.

Here is my test class

@isTest
class WhiteboardItemsApexControllerTest {
    static testMethod void newWhiteboardItemDBTest() {

    User u      = new User();
    u.UserName  = '[email protected]';
    u.Email     = '[email protected]';
    u.FirstName = 'FirstName';
    u.LastName  = 'LastName';
    u.Alias     = 'flast';
    u.ProfileId = '00ed0000000uSiX';

    u.TimeZoneSidKey = 'GMT';
    u.LocaleSidKey = 'en_US';
    u.EmailEncodingKey = 'ISO-8859-1';
    u.LanguageLocaleKey = 'en_US';
    u.UserPermissionsMobileUser = false;

    insert u;

    TouchpointDev__Whiteboard_Item__c item = new TouchpointDev__Whiteboard_Item__c (
        Name = 'Test Item',
        TouchpointDev__Item_Due_Date__c = Date.today(),
        TouchpointDev__AssignedTo__c = u.Id,
        TouchpointDev__High_Priority__c = false,
        TouchpointDev__PersonalItem__c = true,
        TouchpointDev__Completed__c = false
    );

    WhiteboardItemsApexController.newWhiteboardItemDB(item);
//  ^^When I comment out this line I don't get the error.    

    System.assertEquals(item,  WhiteboardItemsApexController.newWhiteboardItemDB(item));
    }
}

This is the error

System.DmlException: Insert failed. First exception on row 0 with id a00d000000k2r8tAAA; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Here is newWhiteboardItemDB method on the Apex class I'm testing

 @AuraEnabled
public static TouchpointDev__Whiteboard_Item__c newWhiteboardItemDB(TouchpointDev__Whiteboard_Item__c whiteboardItem) {

    insert whiteboardItem;

    return whiteboardItem;
}
8
  • What's in method newWhiteboardItemDB? You might inserting record with id, which is not allowed. Aug 18, 2016 at 18:30
  • I added the newWhiteboardItemDB method to my original post. Sorry I didn't do that originally.
    – Tyler Zika
    Aug 18, 2016 at 18:36
  • you're inserting same record two times. Remove your assert, it doesn't make any sense Aug 18, 2016 at 18:38
  • 3
    they're doing upsert Aug 18, 2016 at 18:43
  • 1
    Oh wow, that's it. I think they need to give the full method being used on that doc page.
    – Tyler Zika
    Aug 18, 2016 at 18:48

1 Answer 1

2

Error that you have:

cannot specify Id in an insert call

Tells that you're trying to insert record which already has id. This operation is not allowed. you can modify insert call in method newWhiteboardItemDB to upsert. If you refer to Lightning test documentation, you'll found that they are doing upsert in their example:

public static Expense__c saveExpense(Expense__c expense) {
    // Perform isUpdateable() check here 
    upsert expense;
    return expense;
}

You must log in to answer this question.

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