+ Start a Discussion
Wylie PeachWylie Peach 

Trigger: Concatenate multiple fields in an External ID

Hi everyone, 
I am trying to write a Trigger (before insert, before update) that will create  a composite key on a Purchases__c custom object. The data is coming in, but has no unique ID. Purchases__c has custom fields like Event_name, section_Name, row_name etc, that I need to concatenate into a custom field Key__c which is an external ID. I am fairly new to triggers and was hoping for some guidance on how to start going about it. I am pretty sure I am way off and probably overthinking this. Any sort of suggestion would help. Here is what I have so far:

trigger Composite_Key on Purchases__c (before insert, before update) {
    //need to assign Key__c field with concatenated values
            //Event_name
            //section_Name
            //row_name
            //seat_num
            //num_seats
            //add_datetime
    if(Trigger.isBefore)
    {
        if(Trigger.isInsert)
        {
           Key__c key = new Key__c();
           key.Event_name = Trigger.new[0].name;
        }
        else if(Trigger.isUpdate)
        {
           //code here
        }
    }
}
Best Answer chosen by Wylie Peach
Maharajan CMaharajan C
HI,

Try the below trigger:

trigger Composite_Key on Purchases__c (before insert, before update) {
    if(Trigger.isBefore)
    {
        for(Purchases__c purchase : trigger.new)
        {
            String eventName = String.isBlank(purchase.Event_name)?'':purchase.Event_name+'-';
            String sectionName = String.isBlank(purchase.section_Name)?'':purchase.section_Name+'-';
            String rowname = String.isBlank(purchase.row_name)?'':purchase.row_name+'-';
            String seatnum = String.isBlank(String.valueOf(purchase.seat_num))?'':String.valueOf(purchase.seat_num)+'-';
            String numseats = String.isBlank(String.valueOf(purchase.num_seats))?'':String.valueOf(purchase.num_seats)+'-';
            String adddatetime = String.isBlank(String.valueOf(purchase.add_datetime))?'':String.valueOf(purchase.add_datetime);
            purchase.Key__c = eventName + sectionName + rowname + seatnum + numseats + adddatetime;
        }
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
HI,

Try the below trigger:

trigger Composite_Key on Purchases__c (before insert, before update) {
    if(Trigger.isBefore)
    {
        for(Purchases__c purchase : trigger.new)
        {
            String eventName = String.isBlank(purchase.Event_name)?'':purchase.Event_name+'-';
            String sectionName = String.isBlank(purchase.section_Name)?'':purchase.section_Name+'-';
            String rowname = String.isBlank(purchase.row_name)?'':purchase.row_name+'-';
            String seatnum = String.isBlank(String.valueOf(purchase.seat_num))?'':String.valueOf(purchase.seat_num)+'-';
            String numseats = String.isBlank(String.valueOf(purchase.num_seats))?'':String.valueOf(purchase.num_seats)+'-';
            String adddatetime = String.isBlank(String.valueOf(purchase.add_datetime))?'':String.valueOf(purchase.add_datetime);
            purchase.Key__c = eventName + sectionName + rowname + seatnum + numseats + adddatetime;
        }
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Wylie PeachWylie Peach
This worked beautifully! Thank you so much! 
Maharajan CMaharajan C
Thanks Wyile. Please mark the best answer !!!