You need to sign in to do that
Don't have an account?
Matthew 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
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
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
Thanks,
Jayant
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.
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.
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
Account.Brands__c. Let me know if this works and helps.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?