0

I'm trying to insert Accounts and Contacts in the same class. I'm inserting the account first, then inserting contacts, based on the account name.

The account field in contacts is not getting populated. Is what I'm trying to achieve possible?

First I insert accounts, then contacts using:

insert DTC_Accs; insert DTC_Contact;

Accounts are inserted by reading a csv:

public List<Account> createAccs(String[] filelines) {
    List<Account> Accs = new List<Account>();
    for (Integer i=1;i<filelines.size();i++)
    {
        String[] inputvalues = filelines[i].split(',');


        Account temp = new Account();
        temp.Industry = inputValues[0];
        temp.Name = inputValues[1];
        // Add the Account to the list
        Accs.add(temp);
    }

    return Accs;
}`

Contacts are inserted with:

    public List<Contact> createContact(String[] filelinesContact) {        List<Contact> Conts = new List<Contact>();
    for (Integer j=1;j<filelinesContact.size();j++)
    {
        String[] inputvaluesContact = filelinesContact[j].split(',');


        //set the value of the contact 
        Contact tempContact = new Contact();
        tempContact.Salutation = inputvaluesContact[1];
        tempContact.FirstName = inputvaluesContact[2];
        tempContact.LastName = inputvaluesContact[3];
        tempContact.Phone = inputvaluesContact[4];
        tempContact.MobilePhone = inputvaluesContact[5];
        tempContact.Email = inputvaluesContact[6];
        tempContact.AccountID = objectIdbyName.get('Account').get(inputValuesContact[7]);
        // Add the Contact to the list
        Conts.add(tempContact);
    }

    return Conts;
}`

The code that sets the account on contacts is in another class and is:

        objectIdbyName.put('Account', new map<string, id>());
    for(Account a : [Select id, Name from Account])
        objectIdbyName.get('Account').put(a.Name,a.Id);
  • Yes, it's possible, but you will need to include some code to see where you went wrong. Please edit your question, or it is likely to be closed. – sfdcfox Apr 5 '18 at 18:57
  • What do you mean by 'inserting the contacts based on the account name'? Can you provide a code sample of what you have tried? On Contacts you can set the AccountId field, have you tried that? – Kyle Apr 5 '18 at 18:58
  • @sfdcfox edited! – SF2321 Apr 5 '18 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.