+ Start a Discussion
Shubham Sinha 49Shubham Sinha 49 

Create a handler class on Account Object

I need to write a class which creates and updates the Account object. Criterias are first it will search the existing record in the database if the Last Name and ZIP code fields of the record do not match, Creates the Account record and if matches then updates the existing record . Updating field would be "Related".
How to do this. Please help
Christan G 4Christan G 4
It sounds like you want a trigger rather than a class. Based on what you've stated, you would like a trigger such when a new account is either inserted your updated, you would like for the system to compared the new account to all other accounts that already exist in the database. If the new account's last name or zip code matches to any of the existing record, update the field within the existing account to related and prevent the new record from being created. If there are no matches, make one create a new account. Can you please confirm my understanding? Thanks in advance
Shubham Sinha 49Shubham Sinha 49
Yes , you are right. Not exactly the trigger, I will create the handler first then will call my method from trigger but you can help me by writing trigger.
Christan G 4Christan G 4

Okay sure. Sorry for the grammar mistakes in my original post. When creating the trigger below, I assumed that your handler will have only one method dedicated for this requirement. I also assumed that your handler method will only have one parameter of a list of Account sObjects. Please replace the generic terms in caps with the name of your handler class and its method  from your org. If you need help writing the handler class, feel free to reach out. Also, feel free to post any questions that you may have. I suggest naming the handler class, AccountTriggerHandler so that they are easy to associate one another.

trigger AccountTrigger on Account (before insert) {

HANDLER_CLASSNAME.HANDLER_CLASS_METHOD_NAME(Trigger.new);

}
Shubham Sinha 49Shubham Sinha 49
Thanks Christan .Yes I need help in writing handler only . Please help me on that