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
Darshit Pathak 3Darshit Pathak 3 

How to update record name appended with last modified date whenever the record is inserted or updated?

If I append lastModified date everytime the record is updated it will exceed the name field character limit that is 80.
And also to get the latest LastMpdifiedDate I need to update the record in afterUpdate by doing DML. so it may go in infinite recursive trigger call.

I am not getting how to acomplish this?
Best Answer chosen by Darshit Pathak 3
Raj VakatiRaj Vakati
This code is not bukfiled but wokring  .. bulkfy the code
public class checkRecursive {
    public static Set<Id> SetOfIDs = new Set<Id>();
}



try this code
 
trigger AccountUpdate on Account (after update) {
    List<Account> accUp = new List<Account>() ;
    for(Account a :Trigger.new){
        If(!checkRecursive.SetOfIDs.contains(a.Id)){
            List<String> parts = a.Name.split('--');
            if(parts.size()>1){
              //  a.Name = parts[0]+ '--'+String.valueOf(a.LastModifiedDate);           
            }
            checkRecursive.SetOfIDs.add(a.Id);
            Account  a1 =[Select Id , Name from Account where Id =:a.Id] ; 
            a1.Name = parts[0]+ '--'+String.valueOf(a.LastModifiedDate);
            update a1 ;
            // accUp.add(a);
            //   update accUp;
            
        }
        
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
You can do it from the trigger .. here is the sample code 
 
trigger AccountUpdate on Account (before update) {
	
	for(Account a :Trigger.new){
		a.Name =a.Name+String.valueOf(a.LastModifiedDate);
		
	}
	
}

 
Darshit Pathak 3Darshit Pathak 3
thanks for the reply but this won't work because of below reasons.
  1. before update will not hold the latest lastModifiedDate
  2. Every time the account is updated a new lastModified Date will be appended in the name and it will exceed the limit of 80 characters of name field and data also will not look as expected.LastModifiedDate should be replaced in the name with the new date.
Raj VakatiRaj Vakati
This code is not bukfiled but wokring  .. bulkfy the code
public class checkRecursive {
    public static Set<Id> SetOfIDs = new Set<Id>();
}



try this code
 
trigger AccountUpdate on Account (after update) {
    List<Account> accUp = new List<Account>() ;
    for(Account a :Trigger.new){
        If(!checkRecursive.SetOfIDs.contains(a.Id)){
            List<String> parts = a.Name.split('--');
            if(parts.size()>1){
              //  a.Name = parts[0]+ '--'+String.valueOf(a.LastModifiedDate);           
            }
            checkRecursive.SetOfIDs.add(a.Id);
            Account  a1 =[Select Id , Name from Account where Id =:a.Id] ; 
            a1.Name = parts[0]+ '--'+String.valueOf(a.LastModifiedDate);
            update a1 ;
            // accUp.add(a);
            //   update accUp;
            
        }
        
    }
    
}

 
This was selected as the best answer