You need to sign in to do that
Don't have an account?
ashish jadhav 9
How to write trigger on custom objects field?
I've written below trigger:
trigger test_vehicle on Vehicle__c (before insert) {
for(Vehicle__c gadi : Trigger.New){
if(gadi.vehicle__c.contains('Honda')){
system.debug('Honda is already exists');
}
}
}
and testing in a developer console like:
Vehicle__c test = new Vehicle__c();
test.Vehicle_Name='testy';
test.user_mail = 'testy@t.com';
insert test;
but getting error, can you please correct me?
trigger test_vehicle on Vehicle__c (before insert) {
for(Vehicle__c gadi : Trigger.New){
if(gadi.vehicle__c.contains('Honda')){
system.debug('Honda is already exists');
}
}
}
and testing in a developer console like:
Vehicle__c test = new Vehicle__c();
test.Vehicle_Name='testy';
test.user_mail = 'testy@t.com';
insert test;
but getting error, can you please correct me?
Check the API names of all the fields and use correct API names at appropraite palces
(API names of custom fields ends with __c ).
Btw, your trigger has just one condition, and the data that you are going to insert won't match that condition, so effectively you won't get any debug log.
please put appropriate conditions in your trigger or matching data for it to work.
if you are trying to stop duplicate entries, your code should be similar to this: (assuming that you are using standard name field)
Mark this as BEST ANSWER, if it helps.
All Answers
You have provided the object name : if(gadi.vehicle__c.contains('Honda'))
Please change to : if(gadi.Vehicle_Name__c.contains('Honda')).
----------------------------------------------------------
Test class:
You cant write the test class in the same trigger written page.
Please write the seperate class.
For custom fields you have to use __c
Vehicle__c test = new Vehicle__c();
test.Vehicle_Name__c='testy';
test.user_mail__c = 'testy@t.com';
insert test;
Line: 2, Column: 1
Invalid field Vehicle_Name__c for SObject Vehicle__c
I'll be testing in execute anonymous block.
create-->object --> vehicle
Scroll down to field section
Check the API names of all the fields and use correct API names at appropraite palces
(API names of custom fields ends with __c ).
Btw, your trigger has just one condition, and the data that you are going to insert won't match that condition, so effectively you won't get any debug log.
please put appropriate conditions in your trigger or matching data for it to work.
if you are trying to stop duplicate entries, your code should be similar to this: (assuming that you are using standard name field)
Mark this as BEST ANSWER, if it helps.
As you want to write trigger on your custom sobject(Vehicle__c) if the record is existing it should display a system.debug(Record is already exisiting) message in the log file.
This can be achieved by following the above steps
Step 1 ==> Write a Trigger for your custom object specifying the condition as below.
trigger vehicletest on Vehicle__c (before insert) {
for(Vehicle__c gadi : Trigger.New){
if(gadi.Vehicle_Name.contains('Honda'))
system.debug('This record is already existing');
}
}
Step 2 ==> Come to your developer console and create a test class as below.
public class vehicletester {
Vehicle__c test = new Vehicle__c ();
test.Vehicle_Name='Honda';
insert test;
}
Step 3 ==> Copy the below code into your anonymous block and execute.
Vehicle__c test = new Vehicle__c ();
test.Vehicle_Name='Honda';
insert test;
Step 4 ==> Now check the debug in the log file it displays This record is already existing.
I have checked the above piece of code in my org on some other custom sobject, its working perfectly.
Kindly mark it as solved if it resolves your concern
Thanks & Regards
Nagendra.P
9848950830
Please check your business logic before going for triggers.
Setup >> Build >> Customize >> (standard object name like Account, Opportunity) >> Fields
(Two sections -> standard fields, custom fields)
For Custom Objects:
Setup >> Build >> Create >> Objects >> (select your desired object names) >> scroll down to see same two sections
In both these sections you will find a colum for API Name.
More info here:
https://help.salesforce.com/apex/HTViewSolution?id=000007594&language=en_US
Can you please let me know example of all trigger events like before insert/update/delete & after insert/update/delete/undelete?
@Siddharth: I'm new to salesforce and practicing the triggers so that's why started learning like this, I know this validation but for learning purpose I was playing with it.