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
Matthew AllenMatthew Allen 

Distinct trigger on text field

Hi,

i need your help.  I have a field on the account object called “Brands__c” this field can contain multiple brands split by a comma. However this field can contain duplicate brands, for example Sprite, Coca Cola, Fanta, Sprite. I need s trigger that either fixes that field and remove the duplicate sprite or moves the data to another field and removes the duplicate sprite in the process.

it won’t always be sprite and the field could contain 1 brand or 50 brands.

can anyone help? I have never used triggers before but understand this is my only option.

thank you

matt
Jayant DasJayant Das
Hi Matthew,

You can try using the below logic to remove any duplicates entered and save only distinct values during the operation. This is a sample but should be a good starting point for any further logic
 
    Account acct = Trigger.new[0];
    String brand = acct.Brands__c;
    brand = brand.deleteWhitespace(); 
    
    List<String> allItems = brand.split(','); //  you get the list of comma separated values
    Set<String> distinctItems = new Set<String>(); 
    distinctItems.addAll(allItems); // this will only add distinct values from the list

    // now join back those string using a comma
    brand = '';
    for(String item : distinctItems) {
        brand += item + ',';
    }
    brand = brand.removeEnd(','); // remove the last comma which would have been added
    acct.Brands__c = brand; // assign it back to the field

Thanks,
Jayant
Matthew AllenMatthew Allen
Thank you so much. Next question... what do I don’t with this code? (Insert embarrassed face)
Jayant DasJayant Das
:) well if you see the sample, it's basically working on a single record. What you should not be doing is to have it work on bulk of records, say while doing a data load of thousands of records. The logic behind finding the duplicate is a bit resource consuming, so it may lead to limit exceptions. To avoid this, you can have this logic implemented in a batch class, which would take care of cleansing the input with much more limits available.

But if your requirement is to only capture it for UI inputs, i.e., only a single record, then you should be good to use this logic.
Matthew AllenMatthew Allen
thanks for this.

I am getting an error on line 1 though

Error: Compile Error: Unexpected token 'Account'. at line 1 column 1

All I am doing is pasting your code into a New trigger on the account object.
Jayant DasJayant Das
Can you please post the complete trigger code? The only difference in the code that I used and that I pasted here was to remove the namespace prefix from the field name.
Matthew AllenMatthew Allen
pretty much what you gave me? Thanks for taking the time out to help.


Account acct = Trigger.new[0];
String brand = Account.Brands__c;
brand = brand.deleteWhitespace();

List<String> allItems = brand.split(','); //  you get the list of comma separated values
Set<String> distinctItems = new Set<String>();
distinctItems.addAll(allItems); // this will only add distinct values from the list

// now join back those string using a comma
brand = '';
for(String item : distinctItems) {
    brand += item + ',';
}
brand = brand.removeEnd(','); // remove the last comma which would have been added
Account.Brands__c = brand; // assign it back to the field
Jayant DasJayant Das
The last line in the code should be acct.Brands__c instead of Account.Brands__c. Let me know if this works and helps.
Matthew AllenMatthew Allen
doesn't make any difference, the error is still at line 1?

I originally pasted your data exactly and got the same error, then tried to amend to fit my org but still get teh same error. It just doesn't get past teh first line of code?
Jayant DasJayant Das
Do you mind posting the complete trigger file, starting from the declaration.
Matthew AllenMatthew Allen
I don't know what that means sorry. I pasted in exactly what you gave me and I got this error.
Jayant DasJayant Das
I meant the trigger file where you pasted it, which should look similar as -- 
 
trigger AccountTrigger on Account (before insert, before update) {
... all the code ...
}
Matthew AllenMatthew Allen
Account acct = Trigger.new[0];
String brand = Account.Brands__c;
brand = brand.deleteWhitespace(); 

List<String> allItems = brand.split(','); //  you get the list of comma separated values
Set<String> distinctItems = new Set<String>(); 
distinctItems.addAll(allItems); // this will only add distinct values from the list

// now join back those string using a comma
brand = '';
for(String item : distinctItems) {
    brand += item + ',';
}
brand = brand.removeEnd(','); // remove the last comma which would have been added
Acct.Brands__c = brand; // assign it back to the field
I guess I am missing that first line that you have mentioned? this bit - trigger AccountTrigger on Account (before insert, before update) ??
 
Jayant DasJayant Das
Okay, so you should be creating a Trigger first on the Account and then use this code in that trigger. A good place to get started with triggers is trailhead -- https://trailhead.salesforce.com/en/modules/apex_triggers/units/apex_triggers_intro
Matthew AllenMatthew Allen
I have tried my best, but don't really know what I'm doing. I have created the below, but get an error - Error:Apex trigger CentreBrandClean caused an unexpected exception, contact your administrator: CentreBrandClean: execution of AfterUpdate caused by: System.FinalException: Record is read-only: ()
 
trigger CentreBrandClean on Account (after update) 
{Account acct = Trigger.new[0];
String brand = acct.CB_Centre__c;
brand = brand.deleteWhitespace(); 

List<String> allItems = brand.split(','); //  you get the list of comma separated values
Set<String> distinctItems = new Set<String>(); 
distinctItems.addAll(allItems); // this will only add distinct values from the list

// now join back those string using a comma
brand = '';
for(String item : distinctItems) {
    brand += item + ',';
}
brand = brand.removeEnd(','); // remove the last comma which would have been added
acct.CB_Centre__c = brand; }// assign it back to the field

 
Jayant DasJayant Das
You cannot update the same record in after update until you re-query it. So a better way to do this would be to use before update instead of after update in your trigger. Before update will necessarily first go through the logic to cleanse your data and then save the data in the record.