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
Ricardo OrtegaRicardo Ortega 

Trigger before insert

Hello, I am new to the Salesforce programming world and I'm trying to create an apex trigger before insert. My object is a trip__c and It has a lookup to an account. I want to validate that before insertion is done, that the account name doesn't have the word "region" in it. Also, I would like if you could provide me with a guidance as to where I can learn the Salesforce apex language to create triggers, classes, pages, etc... Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
gautam_singhgautam_singh

Hi,

Agreed , You dont need to create a Trigger. Validation will be the best approach without Code.

Follow the following Steps :-

1. Name  Setup > AppSetup(Build) > Object > Trip__c{Click on label}.

2. Go down and find the Validation Rule Related List, Click on New. Give the Validation Rule a Unique Valid Name .

3. In the Formula Sectio. Select the "REGEX" formula from the right Pane .

    It have a Syntax REGEX(Text, RegEx_Text) , Returns TRUE if Text matches the regular expression RegEx_Text. Otherwise, it returns FALSE
    Insert your Lookup field from the Insert button replacing "Text" in the above formula and write the string beside it which you are looking for.
    Ex-: It should be like  REGEX(Account.Name, 'region)

4. Specify the position as Top of the Page. Click Save.

Your validation works correctly. You can learn more about Validations Clicking here or by looking at some good Examples here.

Learning Apex , Visualforce and Triggers doesnt have a end.

You can start the touchbase by downloading the Apex Manual from here. And Start learning the basics of it, first going forward are you certified with DEV 401 Course of Salesforce ?
I always suggest that you focus on building something for yourself. It doesn't have to be very sophisticated or complicated, but should be a real application that you can explore building out while covering as many aspects of the platform as possible.


For example, build yourself a time tracking system that you can enter your billable hours into, and create Visualforce pages....you get the idea.


The more you build, the more you learn, the more confidence you gain over your experience as you further learn the

platform.

Start there...gain experience and knowledge and create a synergy...Also , There are many Online Trainers available , You can approach any of them , they can teach you Apex Coding and Development with their Experience. Getting Trained is a
shortcut to experience.

Let me know if you need furthur assitance or help regarding the same.

Important :

Hit Kudos[Click Star Icon aside] if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You


All Answers

liron169liron169
Hello,

You can search Google for code example, there are many.

For the requirement you have you can create validation rule on the object. Not need for a trigger.
gautam_singhgautam_singh

Hi,

Agreed , You dont need to create a Trigger. Validation will be the best approach without Code.

Follow the following Steps :-

1. Name  Setup > AppSetup(Build) > Object > Trip__c{Click on label}.

2. Go down and find the Validation Rule Related List, Click on New. Give the Validation Rule a Unique Valid Name .

3. In the Formula Sectio. Select the "REGEX" formula from the right Pane .

    It have a Syntax REGEX(Text, RegEx_Text) , Returns TRUE if Text matches the regular expression RegEx_Text. Otherwise, it returns FALSE
    Insert your Lookup field from the Insert button replacing "Text" in the above formula and write the string beside it which you are looking for.
    Ex-: It should be like  REGEX(Account.Name, 'region)

4. Specify the position as Top of the Page. Click Save.

Your validation works correctly. You can learn more about Validations Clicking here or by looking at some good Examples here.

Learning Apex , Visualforce and Triggers doesnt have a end.

You can start the touchbase by downloading the Apex Manual from here. And Start learning the basics of it, first going forward are you certified with DEV 401 Course of Salesforce ?
I always suggest that you focus on building something for yourself. It doesn't have to be very sophisticated or complicated, but should be a real application that you can explore building out while covering as many aspects of the platform as possible.


For example, build yourself a time tracking system that you can enter your billable hours into, and create Visualforce pages....you get the idea.


The more you build, the more you learn, the more confidence you gain over your experience as you further learn the

platform.

Start there...gain experience and knowledge and create a synergy...Also , There are many Online Trainers available , You can approach any of them , they can teach you Apex Coding and Development with their Experience. Getting Trained is a
shortcut to experience.

Let me know if you need furthur assitance or help regarding the same.

Important :

Hit Kudos[Click Star Icon aside] if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You


This was selected as the best answer