You need to sign in to do that
Don't have an account?
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 ?
Semi-colons have come to haunt you... Gotta watch out for the syntax gotchas
Like so:
All Answers
Semi-colons have come to haunt you... Gotta watch out for the syntax gotchas
Like so:
Error: Compile Error: unexpected token: ';' at line 2 column 38
Try this one out... I also made it bulk safer (avoid DML / SOQL in loops)
Hi ,Please try with below:
@isTest
private Class Updatephone_TestClass{
static testMethod void myUnitTest() {
Contact c = new (Name = 'Test contact', Accounid='001C0000012eA41', phone= '817-777-7784');
insert(c);
}
}
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
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();
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 :(
It is working thank you guys :)