I am having trouble trying to set my account id. This keeps giving me an error saying that it is invalid. Any one has any advice to go about this? This is my testcase that i am trying to insert an account.

@istest private class Account_Contactc_Tests{

static testMethod void testCreateAccount(){
    Account act = TestDataFactory.BuildAccount('Recruter');
    Contact cnt = TestDataFactory.BuildContact();

    insert act;

    Account_Contact__c ac = new Account_Contact__c();
    cnt.id = act.id;
    insert cnt;
    ac.AccountContacts__c = act.id;
    ac.name = 'Account_Contact 1';
    insert ac;

    Account  actCon = [SELECT Name, BillingStreet, AccountNumber From Account ];
    Contact  cont =[SELECT Name, Address__c,AccountId From Contact WHERE AccountId = :actCon.id];

   System.assertEquals(actCon.BillingStreet, cont.Address__c);


TestDataFactory Class

 public class TestDataFactory {
public static Account BuildAccount (String accountName){

    Account a = new Account();
    a.Name = accountName;
    a.BillingCity = 'Oakland';
    a.BillingCountry = 'US';
    a.BillingState = 'CA';
    a.BillingStreet = '123 Raiders Boulevard';
    a.BillingPostalCode = '97502';

    return a;

public static Contact BuildContact (){

    Contact c = new Contact();
    c.FirstName = 'Homer';
    c.LastName = 'Simpison';
    c.Department = 'IT';

    return c;


  • looks like the issue is at Account creation. Can you post your TestDataFactory.BuildAccount method
    – highfive
    Commented Oct 25, 2015 at 5:34
  • This is where, posting the LINE NUMBERS as well as indication which line that number belongs to, is highly encouraged when asking questions. Otherwise its just a guess, sometimes obvious, sometimes it is not.
    – Eric
    Commented Oct 25, 2015 at 5:41

3 Answers 3


For me, your

cnt.id = act.id

make no sense. Change it into

cnt.AccountId = act.id;

The problem is with this line:

cnt.id = act.id;

You've assigned the Account.Id to the Contact.Id.

It should read:

cnt.id = ac.id;

The problem is with below code extract:

    cnt.id = act.id;
  • act is Account Type
  • cnt is of Contact Type

And your are trying to set cnt's id which is Contact Type with act 's id which is of Account Type. And this is not possible in salesforce.

  • How would i go about this? I need to create a account and a contact that has a relationship
    – user25720
    Commented Oct 25, 2015 at 5:51
  • 1
    If you want to create a account and a contact relationship then set AccountID relationship field on Contact Object which is lookup of account. cnt.AccountId = act.id;
    – Ajay Gupta
    Commented Oct 25, 2015 at 5:53

You must log in to answer this question.

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