You need to sign in to do that
Don't have an account?
sunny.kapoor1306
Trigger
I want a trigger:
where if the phone number starts with 011 it automatically takes delhi as a city
how can i achieve this?
Hi,
Are both the fields present on the same object ?
If yes you can use before insert and before update trigger event.
For example:
trigger ContactUpdate on Contact (before insert, before update)
{
for(Contact obj : trigger.new)
{
if(obj.phone startswith('011'))
obj.City = 'Delhi';
}
}
Note: In before insert and before update you do not need to explicitly perform DML operations it automatically updates the value which are binded with the fields in the before insert and update events of the same object.
Let me know if it works for you!
Error: Compile Error: expecting a right parentheses, found 'startswith' at line 5 column 13
Code:
trigger city on Lead (before insert, before update)
{
for(Lead obj : trigger.new)
{
if(obj.phone startswith('011'))
obj.City = 'Delhi';
}
}
Hi,
I was missing some syntax there:
You can go throught the below code:
trigger ContactUpdate on Contact (before insert, before update)
{
for(Contact obj : trigger.new)
{
system.debug('%%%%%%%%%obj.Phone%%%%%'+obj.Phone);
if(obj.Phone != null && obj.Phone.startsWith('(011)'))
{
obj.MailingCity = 'Delhi';
}
else
obj.MailingCity = '';
}
}
Let me know if it works for you!
Code has no error
but mailing city is still blank even if i enter 011 in phone field
Can you share me your Debug log what value you are getting in 'Phone field'...
So you are getting obj.Phone value without the '()'
So just change this line in the code:
if(obj.Phone != null && obj.Phone.startsWith('(011)'))
To:
if(obj.Phone != null && obj.Phone.startsWith('011'))
Let me know if it works for you!
Change your code like this