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
Arpita NayakArpita Nayak 

Before Trigger

Hi,
I have written this trigger to append 'Dr' prefix for al lead names.but i am getting some error.Please help me.

"Error: Compile Error: expecting a semi-colon, found 'Name' at line 6 column 27"


trigger PrefixDoctor on Lead (before insert,before update) {
for(Lead l :trigger.new)
{
List<Lead> leadlist=new List<Lead>();
{
l.First Name='Dr.'+l.First Name ;
}
}
}
Best Answer chosen by Arpita Nayak
Mahesh DMahesh D
Hi Arpita,

Please use the below code:
 
trigger PrefixDoctor on Lead (before insert, before update) {
	for(Lead l : Trigger.new) {
		if(l.FirstName != null && !l.FirstName.startsWith('Dr.'))
			l.FirstName='Dr.'+l.FirstName;	
	}
}

I tested the above code and it is working properly.

With your code, every time you update the Lead, it will prefix the Dr.

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

MrTheTylerMrTheTyler
you have a space between First_Name.
Arpita NayakArpita Nayak
Thanx a lot i got it.
Mahesh DMahesh D
Hi Arpita,

Please use the below code:
 
trigger PrefixDoctor on Lead (before insert, before update) {
	for(Lead l : Trigger.new) {
		if(l.FirstName != null && !l.FirstName.startsWith('Dr.'))
			l.FirstName='Dr.'+l.FirstName;	
	}
}

I tested the above code and it is working properly.

With your code, every time you update the Lead, it will prefix the Dr.

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Arpita NayakArpita Nayak
Thanx Mahesh for your reply.It is the best way For prefixing Dr.
Arpita NayakArpita Nayak
Hi,
I have tried this trigger,but i am getting some error .

Error: Compile Error: No such column 'Account' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 9 column 17


trigger accountupdate on Account (before update) {
for(Account a:trigger.new)
{
List<Account> a1=[select AccountNumber,Name from Account where Name=:a.Name];
if(a1.size>0)
{
if(a.SLA__c=='gold')
{
List<Contact> c=[select Phone from Contact where Contact.Account=:a.Name];
if(c.size>0)
{
a.count_c=c.size();
}
}
if(a1.size==0)
{
}
}
}
}
Mahesh DMahesh D
Hi Arpita,

Please find the corrected code:
 
trigger accountupdate on Account (before update) {
	for(Account a:trigger.new) {
		List<Account> a1=[select AccountNumber,Name from Account where Name=:a.Name];
		if(a1.size() > 0) {
			if(a.SLA__c == 'gold') {
				List<Contact> c = [select Phone from Contact where Contact.Account.Name=:a.Name];
				if(c.size() > 0) {
					a.count_c = c.size();
				}
			}
			if(a1.size() == 0) {
			}
		}
	}
}
FYI, I just corrected the errors but didn't fix the bulkification. You still need to work on bulkifying the above code as it will not work if you update 100+ Accounts.

Regards,
Mahesh
Arpita NayakArpita Nayak
Hi ,
I have tried this codeand it is working,but i have a question ,is this trigger exactly doing this??----whenever i am creating Accounts with SLA 'gold' the no of phone numbers from contact object of all account will be populated in Account custom object Count.Am i Right??   
 
Mahesh DMahesh D
Whenever you are updating the Account if SLA is gold then just calculating the number od contacts and assigning it to count field.
Arpita NayakArpita Nayak
But the count field not getting updated with this code.