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
ashish jadhav 9ashish 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?
Best Answer chosen by ashish jadhav 9
mritzimritzi
Usually the name field is a standard field having API name "Name". If you are using the standard name field then write test.Name = 'testy';
​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:
trigger TestVehicle on Vehicle__c (before insert) {
    for(Vehicle__c gadi : Trigger.New){
        if(gadi.Name.contains('Honda')){
           gadi.addError('Honda is already exists');
        }
    }
}
(assuming that you are using standard name field)


Mark this as BEST ANSWER, if it helps.

All Answers

Nachu RY 4Nachu RY 4
Please provide the column name in the line 3.
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;
 
ashish jadhav 9ashish jadhav 9
Thanks for reply Nachu, but still getting below error

Line: 2, Column: 1
Invalid field Vehicle_Name__c for SObject Vehicle__c

I'll be testing in execute anonymous block.
Nachu RY 4Nachu RY 4
please see the field name present in the object Vehicle__c.
create-->object --> vehicle
Scroll down to field section
mritzimritzi
Usually the name field is a standard field having API name "Name". If you are using the standard name field then write test.Name = 'testy';
​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:
trigger TestVehicle on Vehicle__c (before insert) {
    for(Vehicle__c gadi : Trigger.New){
        if(gadi.Name.contains('Honda')){
           gadi.addError('Honda is already exists');
        }
    }
}
(assuming that you are using standard name field)


Mark this as BEST ANSWER, if it helps.
This was selected as the best answer
nagendra 6989nagendra 6989
Hi ashish jadhav 9,

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
 
Siddharth ManiSiddharth Mani
If your only intention is to avoid duplicates, why not try it with customizable logic like Validation Rules/Process Builder. And if its going to be specific to a field, I guess it can be simply done by making the field as unique.
Please check your business logic before going for triggers.
ashish jadhav 9ashish jadhav 9
Hi Mritzi, thanks for the help, can you please tell me how to know the system field & its name?
mritzimritzi
For standard Objects:
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
ashish jadhav 9ashish jadhav 9
Thanks once again :)

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.