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
AzeddineAzeddine 

New to APEX and trigger not working:

I am trying to update the account phone based on the contact phone. See tigger below.

 

trigger UpdatePhone on Contact (after insert, after update) {
List<id> Accountids = new List<id>
list<Account> Accounts = new List<Account>
List <Contact> Contacts = new List <contact>
for(contact c:Trigger.new){
if(c.accountid!=Null){
Accountids.add(c.accountid);
}
}
Accounts=[select Id,Phone from Account where Id in:accountids]
for (contact c:Trigger.new) {
for (Account Acc in:Accounts){
if(c.accountid==acc.id){
acc.phone=c.phone;
}
}
update(acc);
}

 

I am getting the following eror

Error: Compile Error: unexpected token: 'list' at line 3 column 4

 

 

Can anyone help ?

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

Semi-colons have come to haunt you... Gotta watch out for the syntax gotchas

 

Like so:

 

trigger UpdatePhone on Contact (after insert, after update) {
	List<id> Accountids = new List<id>{};
	list<Account> Accounts = new List<Account>{};
	List <Contact> Contacts = new List <contact>{};
	for(contact c:Trigger.new){
		if(c.accountid!=Null){
			Accountids.add(c.accountid);
		}
	}
	Accounts=[select Id,Phone from Account where Id in:accountids];
	for (contact c:Trigger.new) {
		for (Account Acc in:Accounts){
			if(c.accountid==acc.id){
				acc.phone=c.phone;
			}
		}
		update(acc);
	}	
}

 

All Answers

Sean TanSean Tan

Semi-colons have come to haunt you... Gotta watch out for the syntax gotchas

 

Like so:

 

trigger UpdatePhone on Contact (after insert, after update) {
	List<id> Accountids = new List<id>{};
	list<Account> Accounts = new List<Account>{};
	List <Contact> Contacts = new List <contact>{};
	for(contact c:Trigger.new){
		if(c.accountid!=Null){
			Accountids.add(c.accountid);
		}
	}
	Accounts=[select Id,Phone from Account where Id in:accountids];
	for (contact c:Trigger.new) {
		for (Account Acc in:Accounts){
			if(c.accountid==acc.id){
				acc.phone=c.phone;
			}
		}
		update(acc);
	}	
}

 

This was selected as the best answer
AzeddineAzeddine
Thanks for the quick reply Sean but this is the error I am getting now

Error: Compile Error: unexpected token: ';' at line 2 column 38
Sean TanSean Tan

Try this one out... I also made it bulk safer (avoid DML / SOQL in loops)

 

trigger Test on Contact (after insert, after update) {
	List<id> Accountids = new List<id>{};
	list<Account> Accounts = new List<Account>{};
	List <Contact> Contacts = new List <contact>{};
	for(contact c:Trigger.new){
		if(c.accountid!=Null){
			Accountids.add(c.accountid);
		}
	}
	Accounts=[select Id,Phone from Account where Id in:accountids];	
	for (contact c:Trigger.new) {
		for (Account Acc :Accounts){
			if(c.accountid==acc.id){
				acc.phone=c.phone;
			}
		}		
	}	
	update Accounts;
}

 

David Lee(China Developer)David Lee(China Developer)

Hi ,Please try with below:

trigger Test on Contact (after insert, after update) 
{
   List<Account> toBeUpdateAccts = new List<Account>();
   set<Id> toBeUpdateAcctIds = new set<Id>();
   for(Contact ct : Trigger.New)
   {
      if(ct.AccountId != null && !toBeUpdateAcctIds.contains(ct.AccountId))
      {
          toBeUpdateAcctIds.add(ct.AccountId);
          Account toBeUpdateAcct = new Account(Id = ct.AccountId);
          toBeUpdateAcct.Phone = ct.Phone;
          toBeUpdateAccts.add(toBeUpdateAcct);
      }
   }
   if(toBeUpdateAccts.size() > 0)
   {
      update toBeUpdateAccts;
   }
}

 

AzeddineAzeddine
Is this a satisfactory test class

@isTest
private Class Updatephone_TestClass{
static testMethod void myUnitTest() {


Contact c = new (Name = 'Test contact', Accounid='001C0000012eA41', phone= '817-777-7784');
insert(c);


}
}
AzeddineAzeddine

I am getting this error on the test class

 

@isTest
private Class Updatephone_TestClass{
static testMethod void myUnitTest() {


Contact c = new (Name = 'Test contact', Accounid='001C0000012eA41', phone= '817-777-7784');
insert(c);


}
}

 

Error: Compile Error: unexpected token: '(' at line 6 column 24

Sean TanSean Tan

No, I would insert the Account object first instead of hard coding the Id. I'd also validate to make sure your trigger actually worked.

 

And the reason you're getting the error is you're not declaring what type of object you're instantiating:

 

@isTest
private Class Updatephone_TestClass{
	
	static testMethod void myUnitTest() {
		
		Account a = new Account(Name='Test Account');
		insert a;
		
		Contact c = new Contact(LastName = 'contact', FirstName='Test', AccountId=a.Id, phone= '817-777-7784');
		insert(c);
		
		a = [ SELECT Id, Phone FROM Account WHERE Id = :a.Id ];
		System.assertEquals(a.Phone, c.Phone);	
	}
}

 

AzeddineAzeddine
the test above did not make but I made a couple changes and this is what I got

@isTest
private Class Updatephone_TestClass{

static testMethod void myUnitTest() {
Account a = new Account();
Contact c = new contacT();
c = [select ID, Phone from contact where id = '003c0000007Unx0'];
c.phone='817-504-7599';
update(c);


a = [ SELECT Id, Phone FROM Account WHERE Id =:c.accountId ];
System.assertEquals(a.Phone, c.Phone);
}
}
I feel that I am close but the test is still failing :(
AzeddineAzeddine

It is working thank you guys :)