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
sunny.kapoor1306sunny.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? 

nick1505nick1505

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!

sunny.kapoor1306sunny.kapoor1306

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';
}
}

nick1505nick1505

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!

sunny.kapoor1306sunny.kapoor1306

Code has no error

but mailing city is still blank even if i enter 011 in phone field

nick1505nick1505

Can you share me your Debug log what value you are getting in 'Phone field'...

 

sunny.kapoor1306sunny.kapoor1306
24.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
01:14:58.048 (48176000)|EXECUTION_STARTED
01:14:58.048 (48214000)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
01:14:58.048 (48238000)|CODE_UNIT_STARTED|[EXTERNAL]|01q900000006qkz|ContactUpdate on Contact trigger event BeforeUpdate for [0039000000CTnPD]
01:14:58.049 (49361000)|SYSTEM_METHOD_ENTRY|[3]|LIST.iterator()
01:14:58.050 (50232000)|SYSTEM_METHOD_EXIT|[3]|LIST.iterator()
01:14:58.050 (50446000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()
01:14:58.050 (50474000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()
01:14:58.050 (50489000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.next()
01:14:58.050 (50506000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.next()
01:14:58.050 (50575000)|SYSTEM_METHOD_ENTRY|[6]|System.debug(ANY)
01:14:58.050 (50603000)|USER_DEBUG|[6]|DEBUG|%%%%%%%%%obj.Phone%%%%%011
01:14:58.050 (50611000)|SYSTEM_METHOD_EXIT|[6]|System.debug(ANY)
01:14:58.050 (50667000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()
01:14:58.050 (50679000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()
01:14:58.450 (50696000)|CUMULATIVE_LIMIT_USAGE
01:14:58.450|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 3 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

01:14:58.450|CUMULATIVE_LIMIT_USAGE_END

01:14:58.050 (50798000)|CODE_UNIT_FINISHED|ContactUpdate on Contact trigger event BeforeUpdate for [0039000000CTnPD]
01:14:58.054 (54275000)|CODE_UNIT_STARTED|[EXTERNAL]|01q900000006pJS|oruse on Contact trigger event BeforeUpdate for [0039000000CTnPD]
01:14:58.054 (54408000)|SYSTEM_METHOD_ENTRY|[2]|LIST.iterator()
01:14:58.054 (54442000)|SYSTEM_METHOD_EXIT|[2]|LIST.iterator()
01:14:58.054 (54455000)|SYSTEM_METHOD_ENTRY|[2]|system.ListIterator.hasNext()
01:14:58.054 (54466000)|SYSTEM_METHOD_EXIT|[2]|system.ListIterator.hasNext()
01:14:58.054 (54475000)|SYSTEM_METHOD_ENTRY|[2]|system.ListIterator.next()
01:14:58.054 (54484000)|SYSTEM_METHOD_EXIT|[2]|system.ListIterator.next()
01:14:58.054 (54568000)|SYSTEM_METHOD_ENTRY|[2]|system.ListIterator.hasNext()
01:14:58.054 (54581000)|SYSTEM_METHOD_EXIT|[2]|system.ListIterator.hasNext()
01:14:58.454 (54595000)|CUMULATIVE_LIMIT_USAGE
01:14:58.454|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 4 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

01:14:58.454|CUMULATIVE_LIMIT_USAGE_END

01:14:58.054 (54631000)|CODE_UNIT_FINISHED|oruse on Contact trigger event BeforeUpdate for [0039000000CTnPD]
01:14:58.059 (59429000)|CODE_UNIT_STARTED|[EXTERNAL]|01q900000006ozB|mix on Contact trigger event BeforeUpdate for [0039000000CTnPD]
01:14:58.059 (59574000)|SYSTEM_METHOD_ENTRY|[10]|LIST.iterator()
01:14:58.059 (59607000)|SYSTEM_METHOD_EXIT|[10]|LIST.iterator()
01:14:58.059 (59620000)|SYSTEM_METHOD_ENTRY|[10]|system.ListIterator.hasNext()
01:14:58.059 (59630000)|SYSTEM_METHOD_EXIT|[10]|system.ListIterator.hasNext()
01:14:58.059 (59639000)|SYSTEM_METHOD_ENTRY|[10]|system.ListIterator.next()
01:14:58.059 (59648000)|SYSTEM_METHOD_EXIT|[10]|system.ListIterator.next()
01:14:58.059 (59691000)|SYSTEM_METHOD_ENTRY|[10]|system.ListIterator.hasNext()
01:14:58.059 (59702000)|SYSTEM_METHOD_EXIT|[10]|system.ListIterator.hasNext()
01:14:58.459 (59716000)|CUMULATIVE_LIMIT_USAGE
01:14:58.459|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 6 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

01:14:58.459|CUMULATIVE_LIMIT_USAGE_END

01:14:58.059 (59752000)|CODE_UNIT_FINISHED|mix on Contact trigger event BeforeUpdate for [0039000000CTnPD]
01:14:58.150 (150506000)|CODE_UNIT_FINISHED|TRIGGERS
01:14:58.150 (150518000)|EXECUTION_FINISHED
nick1505nick1505

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!

 

Shashikant SharmaShashikant Sharma

Change your code like this

 

trigger city on Lead (before insert, before update)
{
for(Lead obj : trigger.new)
{
if(obj.phone ! = null && obj.phone.startswith('011'))
obj.City = 'Delhi';
}
}