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
anas mendilianas mendili 

How append text depending on type of reccord using triggers and trigger handler

Anytime a new Customer account is created, and the Account Name is filled, the Customer

The name should be filled with the same name as the account Name, appended with “- Customer”

  • E.g.: Customer Account
  • Account Name: Recruitment
  • Customer Name: Recruitment – Customer
 

Similarly, every time a new partner account is created, the partner name should be Account

Name appended with “- Partner”

o Eg: Partner Account

o Account Name: Salesforce

o Partner Name Salesforce – Partner

Using triggers in apex I should am trying to do that but I don't know how to write the logic I have in mind which is to get the record type and then check if it is a Customer_Account or Partner_Account depending on that either append '-Customer' or '-Partner' as text in the respective name field   

trigger AccountTrigger on Account (before insert) {
    if(trigger.isInsert && trigger.isBefore){
        AccountTriggerHandler.AppendAccountName(trigger.new);
    }
}
this is the trigger 
and this is the handler class I am stuck on 
public class AccountTriggerHandler {
    public static void AppendAccountName(List<Account> newAccountList){

---------------------------------
I am thinking of smth like this but don't know how to code it write I am very new to apex 
 if{ recordtypeid.name==customer account
 then account a =[SELECT Customer_Name FROM account LIMIT 1];
a.Customer_Name = a.Name +'-Customer';
update a;
a.Partner_Name = a.Name +'-Customer';
update a;
 Account.Customer_name= Account.name +'-Customer'
 else Account.Partner_name= account.name +'-Partner'
 }
PriyaPriya (Salesforce Developers) 
Hey Anas,

The way you are appending is correct, but just do this little change :- 
a.Customer_Name = a.Name + '-' + 'Customer';


Kindly mark it as the best answer if it works.

Thanks,

Priya Ranjan

Antoninus AugustusAntoninus Augustus
Hi Anas,

Try this
public static void AppendAccountName(List<Account> lstAccounts) {
    Map<Id, Schema.RecordTypeInfo> mapIdWithRT = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosById();

    for (Account objAccount : lstAccounts) {
        RecordTypeInfo recordInfo = mapIdWithRT.get(objAccount.recordTypeId);
        if (recordInfo != null) {
            String recordName = recordInfo.getName();
            if (recordName = 'PARTNER ACCOUNT RT NAME') {
                objAccount.Partner_Name = objAccount.Name + '-Partner';
            }
            if (recordName = 'CUSTOMER ACCOUNT RT NAME') {
                objAccount.Customer_Name = objAccount.Name + '-Customer';
            }
        }
    }
}