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
Torm HustvetTorm Hustvet 

Apex Trigger Causing String Too Long Error, unable to delete record.

Hello, 

My org is using a custom object to track contact relationships and users are encountering the following error message when attempting to add/remove/edit the contact relationshiops on several contacts:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger updateCR caused an unexpected exception, contact your administrator: updateCR: execution of BeforeDelete caused by: System.DmlException: Update failed. First exception on row 0 with id 003U000001Ow73GIAR; first error: STRING_TOO_LONG, Contact Relationships: data value too large: lli (max length=255): [Contact_Relationships__c]: Trigger.updateCR: line 26, column 1".

Apex Trigger update CR:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
trigger updateCR on Contact_Relationships__c (after insert, after update,before delete,after delete){
  
    Set<Id> parentIds = new Set<ID>();
     
    // Get all the Parent Ids (Contacts  ) in the Set
    for(Contact_Relationships__c s : trigger.isDelete ? trigger.old : trigger.new){
        parentIds.add(s.Contact__c);
    }
    // QUery Contact.
    List<Contact> contacts = new List<Contact>();
    // Use the Child relations so you get all the related CR for the Contact Parent Object
    contacts = [Select Id, Name, Contact_Relationships__c, (Select Id,Associated_Contact__r.Name__c from Contact_Relationships__r) from Contact where Id in :parentIds];

       // Iterate over each parent and child CR record
    for(Contact o: contacts){
                        o.Contact_Relationships__c = '';
            if(o.Contact_Relationships__r != null && o.Contact_Relationships__r.size()>0){
                for(Contact_Relationships__c s: o.Contact_Relationships__r){
                    if(!o.Contact_Relationships__c.containsIgnoreCase(s.Associated_Contact__r.Name__c)){ // Check if the CR record is already in the CR Field. if not add it otherwise skip
                        o.Contact_Relationships__c += s.Associated_Contact__r.Name__c+' ';
                    }
                }
            }
    }
    // Update the contact
    update contacts;
}

Let me know what additional informiation is needed to assist. I'm new to the admin role at my firm and dealing with some legacy apex/workflows so any help is greatly appreciated!

Torm
Mohan ChanderMohan Chander
Hi Torm,

This is the line thats causing the error i suppose. The Character limit on the field is 255 so your data is exceeding 255 characters. If you change the field type to text area (long)  and increase the character limit to 131,072. This should resolve your error.

o.Contact_Relationships__c += s.Associated_Contact__r.Name__c+' ';

let me know if this helps

Mohan
Arvind KumarArvind Kumar
Hi,

Solution for STRING_TOO_LONG, Contact Relationships: data value too large: lli (max length=255):

1.) For Contact_Relationships__c  field: Change Data type from Text to Text Area of the Contact_Relationships__c field. Because you are entering a large value in this. so that it error is coming when you are updating to the contact.

After that your problem has been solved. If you have any query please let me know.


Thanks,
Arvind Kumar
Torm HustvetTorm Hustvet
Thanks Everyone, 

I was able to change the field type; however, I received an error that the field is being used elsewhere (Workflow). 

 The Field Update has a current field data type of "Text Area", but it doesn't appear I can edit this. 

Would I have to create a new workflow with the correct data  type and copy the legacy formula in below ? Or is there a way to modify the field data type within the Field Update "Contact Relationships Update" record ? 


Formula Value

IF( ISBLANK( Contact__r.Contact_Relationships__c ) , Associated_Contact__r.Name__c , 
IF(AND(NOT(CONTAINS( Contact__r.Contact_Relationships__c,Associated_Contact__r.Name__c )),NOT(ISBLANK(Contact__r.Contact_Relationships__c))), Contact__r.Contact_Relationships__c &" "& Associated_Contact__r.Name__c, 



Contact__r.Contact_Relationships__c))
Arvind KumarArvind Kumar

Hi Torm,

Below is action Item, Please follow the same and hope it will works for you :) -

1. Delete exiting workflow rule & field update.

2. Make new workflow rule & field update with the current data type.( Contact_Relationships__c field : data type (Text Area) ) & use formula value for field update which you have mention above.

3. Use Text Area Field Contact_Relationships__c in trigger. For which have ideally 32,768 character length. It will solve your problem.

4. After make a change please let me know what the results.

If you have any query please let me know.

Thanks,
Arvind Kumar