• stsram
  • NEWBIE
  • 25 Points
  • Member since 2016

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hi, I wonder if anyone can help please? I know only enough about Apex to create a trigger and class but not to write the code...
I would the ability to automatically create an Account Record when entering a new Contact. (Account Name based on the Last Name of the Contact). Ideally the Account Name to be in the format "The " & Last_Name_& " Household"

(Please note Accounts renamed to 'Households' & Contacts to 'Persons')

I copied and tried this Trigger:

trigger CreateAccountFromContact on Contact (before insert) {
    //Collect list of contacts being inserted without an account
    List<Contact> needAccounts = new List<Contact>();
    for (Contact c : trigger.new) {
        if (String.isBlank(c.accountid)) {
            needAccounts.add(c);
        }
    }
    
    if (needAccounts.size() > 0) {
        List<Account> newAccounts = new List<Account>();
        Map<String,Contact> contactsByNameKeys = new Map<String,Contact>();
        //Create account for each contact
        for (Contact c : needAccounts) {
            String accountName = c.firstname + ' ' + c.lastname;
            contactsByNameKeys.put(accountName,c);
            Account a = new Account(name=accountName);
            newAccounts.add(a);
        }
        insert newAccounts;
        
        //Collect a new list of deal__c objects for new accounts
        //List<Deal__c> newRecsForAccounts = new List<Deal__c>();
        for (Account a : newAccounts) {
            //Put account ids on contacts
            if (contactsByNameKeys.containsKey(a.Name)) {
                contactsByNameKeys.get(a.Name).accountId = a.Id;
            }
            //Deal__c newRec = new Deal__c(name=a.Name, accountId=a.Id);
        }
        
        //insert newRecsForAccounts;
        
    }
    
With this Class:
@isTest
public with sharing class CreateAccountFromContact_UnitTest {
    @isTest
    public static void runTest(){
        String firstname = 'first';
        String lastname = 'last';
        String email = 'firstlast@test.com';
        
        //Create contact
        Contact c = new Contact(firstname=firstname, lastname=lastname, email=email);
        insert c;
        
        //Verify account
        c = [select id, accountid, firstname, lastname, email from Contact where id =:c.Id][0];
        Account a = [select id, name from Account where id = :c.accountId][0];
        
        system.assertEquals(firstname + ' ' + lastname, a.Name);
    }
}
The Class passes the test ok and trigger is active but when I try to save the Contact record the system issues the message:

Contact Creation
I was hoping not to create the Account first,

Many thanks for any help, Rob 
I have problem understanding the requirement in step 2  where the"account name cannot be changed" which of these right for thne requirement
1)The account name is created  the first time and cannot be edited or updated later (or)
2)the person who created the acccount record can edit but no any other person(or)
3)The acount name is balnk and cannot be updated during creation.
Which one of the above requirement is right?
  • August 28, 2017
  • Like
  • 0
Hi, I wonder if anyone can help please? I know only enough about Apex to create a trigger and class but not to write the code...
I would the ability to automatically create an Account Record when entering a new Contact. (Account Name based on the Last Name of the Contact). Ideally the Account Name to be in the format "The " & Last_Name_& " Household"

(Please note Accounts renamed to 'Households' & Contacts to 'Persons')

I copied and tried this Trigger:

trigger CreateAccountFromContact on Contact (before insert) {
    //Collect list of contacts being inserted without an account
    List<Contact> needAccounts = new List<Contact>();
    for (Contact c : trigger.new) {
        if (String.isBlank(c.accountid)) {
            needAccounts.add(c);
        }
    }
    
    if (needAccounts.size() > 0) {
        List<Account> newAccounts = new List<Account>();
        Map<String,Contact> contactsByNameKeys = new Map<String,Contact>();
        //Create account for each contact
        for (Contact c : needAccounts) {
            String accountName = c.firstname + ' ' + c.lastname;
            contactsByNameKeys.put(accountName,c);
            Account a = new Account(name=accountName);
            newAccounts.add(a);
        }
        insert newAccounts;
        
        //Collect a new list of deal__c objects for new accounts
        //List<Deal__c> newRecsForAccounts = new List<Deal__c>();
        for (Account a : newAccounts) {
            //Put account ids on contacts
            if (contactsByNameKeys.containsKey(a.Name)) {
                contactsByNameKeys.get(a.Name).accountId = a.Id;
            }
            //Deal__c newRec = new Deal__c(name=a.Name, accountId=a.Id);
        }
        
        //insert newRecsForAccounts;
        
    }
    
With this Class:
@isTest
public with sharing class CreateAccountFromContact_UnitTest {
    @isTest
    public static void runTest(){
        String firstname = 'first';
        String lastname = 'last';
        String email = 'firstlast@test.com';
        
        //Create contact
        Contact c = new Contact(firstname=firstname, lastname=lastname, email=email);
        insert c;
        
        //Verify account
        c = [select id, accountid, firstname, lastname, email from Contact where id =:c.Id][0];
        Account a = [select id, name from Account where id = :c.accountId][0];
        
        system.assertEquals(firstname + ' ' + lastname, a.Name);
    }
}
The Class passes the test ok and trigger is active but when I try to save the Contact record the system issues the message:

Contact Creation
I was hoping not to create the Account first,

Many thanks for any help, Rob 
We're instructed to send an email to the finance group -- that's pretty vague. I'm going to create a Finance group, made up of the CFO and subordinates. Am I missing something in the instructions?
I'm unable to check step 2: Automate Accounts because I'm receiving this error: Challenge Not yet complete... here's what's wrong: 
There was an unexpected error while verifying this challenge. Usually this is due to some pre-existing configuration or code in the challenge Org. We recommend using a new Developer Edition (DE) to check this challenge. If you're using a new DE and seeing this error, please post to the developer forums and reference error id: ZKCVFIZH