You need to sign in to do that
Don't have an account?
[Lindsey Kiken]
Apex: Splitting a Text String
I have a need to allow Community Portald users to add in comma delimited values to a CC_List__c long text field on the Case object, and then have the following occur:
- Text string in the CC_List__c is split by the comma symbol
- Newly added or updated delimited values found in the CC_List__c field are then entered into a child object called CC_List_Object__c as new related records
- Values found in the related CC_List_Object__c's child list that are no longer listed in the CC_List__c are deleted
String[] strs = valstring.spilt(',');Thoughts?
Final code:
All Answers
I have proposed a solution below, which avoids DML operations inside loop. Its too hard to explain in words so I have put up a reference code (I see no reason why it will not compile). To avoid complex DML operations within loops you basically have to create two additional fields in your child object "CC_List_Object__c". One field "Case__c" which looks upto Case and another "My_ID__c" which is a text type field marked as external ID (to avoid issues related to large data later on).
Refer the sample code below:
You may separate the CaseTriggerUtil class out and make the method handleCCFieldChangeEvent static, to have a clean trigger with just one line.
Hope this helps.
Please see below for my updated code:
I ended up doing the following:
Changed the object name "CC_List_Object__c" to "CC_List__c"
Created an autonumber for CC_List__c Name versus text
Changed the field name "Case__c" to "Related_Case__c"
Added a new Email_Address__c email address field to the CC_List__c object
Added in the following additional code to line 22: The code is firing brilliantly, adding in each parsed CC record to the CC_List__c object upon save of the related case. With that said, each time I save the case, it resaves all values instead of just the new additions. Example:
If I enter in: Then the object spits out: If I update the same case to: Then the object spits out: As shown above, the abc@corp.com is listed twice within the CC list, despite only be listed once in the CC_List__c field. Each time I save the record, the CC_List__c field inserts records into the CC_List__c objects as if they were new.
Any thoughts on how to handle? One thought I has was that each time the related case record is saved, it deletes all CC_List__c object records that have it's Case listed in the Related_Case__c field. After the deletion, all records can be re-added into the object like new. If I went this route, I would update the auto text Name value on the CC_List__c object to text with a single dummy value as you have originally shown in order to avoid confusion.
Thoughts?
Final code:
So the code at line 29: Database.insert(lNewCCRecords, false); would actually insert only the new entries, and the ones already existing in database fail silently because the unique condition was not met.
Anyway, glad I could help. tc.