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
erin ryan 8erin ryan 8 

Create a list of contacts - Trailhead

After copying from Trailhead ; I'm recieving an error for line 9. expecting right curly bracket , found "insert". Please advise.

// Create a list of contacts
02List<Contact> conList = new List<Contact> {
03    new Contact(FirstName='Joe',LastName='Smith',Department='Finance'),
04        new Contact(FirstName='Kathy',LastName='Smith',Department='Technology'),
05        new Contact(FirstName='Caroline',LastName='Roth',Department='Finance'),
06        new Contact(FirstName='Kim',LastName='Shain',Department='Education')};
07             
08// Bulk insert all contacts with one DML call
09insert conList;
10 
11// List to hold the new contacts to update
12List<Contact> listToUpdate = new List<Contact>();
13 
14// Iterate through the list and add a title only
15//   if the department is Finance
16for(Contact con : conList) {
17    if (con.Department == 'Finance') {
18        con.Title = 'Financial analyst';
19        // Add updated contact sObject to the list.
20        listToUpdate.add(con);
21    }
22}
23 
24// Bulk update all contacts with one DML call
25update listToUpdate;
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
// Create a list of contacts
List<Contact> conList = new List<Contact> {
		new Contact(FirstName='Joe',LastName='Smith',Department='Finance'),
        new Contact(FirstName='Kathy',LastName='Smith',Department='Technology'),
        new Contact(FirstName='Caroline',LastName='Roth',Department='Finance'),
        new Contact(FirstName='Kim',LastName='Shain',Department='Education')
		};
             
// Bulk insert all contacts with one DML call
insert conList;
 
// List to hold the new contacts to update
List<Contact> listToUpdate = new List<Contact>();
 
// Iterate through the list and add a title only
//   if the department is Finance
for(Contact con : conList) 
{
    if (con.Department == 'Finance') 
	{
        con.Title = 'Financial analyst';
        listToUpdate.add(con);
    }
}
 
update listToUpdate;

I tested the same and same is working fine in my developer org
User-added image
 
Ramssf70Ramssf70
Hi erin ryan ,

if you want to create a list of contact you can create one other way also it is like above but minor difference

List<contact> conlist= new List<contact>();
contact con = new contact();
con.lastname='myname';
con.phone='123456';
conlist.add(con);

contact con1 = new contact();
con.lastname='myname1';
con.phone='123544';
conlist.add(con1);


Now conlist having two records in  this way also we can add records


 
sagar077sagar077
Create an Apex Trigger that will count the number of contacts associated with an account(create a field at account level). Must update the count in insertion and deletion of a contact.

CODE-

trigger CountonAccountofcontact on Contact (after insert,after delete)
{
    Set<Id> mysetid = new Set <Id>();
        if(Trigger.isinsert)
        {
            System.debug('Insert new contact for trigger.new '+ Trigger.new);
            for(Contact contac :trigger.new)
                {
                    mysetid.add(contac.Accountid);
                }
            List<Account> Acc = [Select Id,Number_Of_Contact_Count__c from Account where Id in : mysetid];
            List<Contact> Con = [Select Id from Contact where Accountid in : mysetid];
            for(Account A: Acc)
                {
                    A.Number_Of_Contact_Count__c = Con.size(); 
                }
            update Acc;
            System.debug('Number of count is ' + Acc);
        }

     if(Trigger.isdelete)
        {
            System.debug('The Delete Contact Name For Trigger.old'+ Trigger.Old); 
            for(Contact contac : Trigger.Old)
                {
                    mysetid.add(contac.Accountid);
                }          
            List<Account> Acc = [Select id,Number_Of_Contact_Count__c from Account where id in: mysetid];
            List<Contact> Con = [Select id from Contact where Accountid in : mysetid];
           
            for(Account A :Acc)
                {
                    A.Number_Of_Contact_Count__c = Con.size();
                }
                update Acc;
            System.debug('The Update number is '+ Acc);
        }
    }

NOTE- This code is running but I want for After Update event also and Plz Help me in that