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
larrmill59larrmill59 

How to concatenate today's date into the name field of a custom object

I'm trying to auto name a record as it's created. I want to combine the account name and the current date and use that as the name.

 

The code snip below does work but the date is too complex. How do I get just the mm/dd/yyyy to show?

 

 for(Account a:accountList) {
                  //set your field values here.
   c.Name = a.Name + ' ' + date.today();
   c.Account_Executive__c = a.owner.FirstName + ' ' + a.owner.LastName;
   c.Region__c = a.Region__c;
   c.RME_Date__c = date.today();

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

By doing this you have the date correctly formatted in the variable "formatted", but you aren't using it.

 

You need to change the code to assign this to the field:

 

 

 DateTime nowDT=System.now();
String formatted=nowDT.format('dd_MM_yyyy');
   c.Name = a.Name + ' ' + nowDT;
   c.Account_Executive__c = a.owner.FirstName + ' ' + a.owner.LastName;
   c.Region__c = a.Region__c;
   c.RME_Date__c = formatted;
   

 

 

All Answers

bob_buzzardbob_buzzard

You'll need to use System.now() to get the current date and time, then you can use the DateTime.format method to just output the information you want.

 

E.g.

 

 

DateTime nowDT=System.now();
String formatted=nowDT.format('dd_MM_yyyy');

 

 

larrmill59larrmill59

Bob:

 

Thanks for the quick reply. I think we're on the right track but that still displays the time as well as the date. I'm only looking for the date to be appended to the account name...for example...."HURON VALLEY FINANCIAL 2011-01-13 16:50:30" is what currently shows using your code. How do I get rid of the time stamp at the end?

bob_buzzardbob_buzzard

That sounds like you are using the format method without a format string - can you post your updated code?

 

larrmill59larrmill59

Here is the relevant section of code. I simply pasted your example in and referenced it in my field.

 

 for(RME__c c:Trigger.new) {
            if(c.account__c != null) {
                accountIDs.add(c.Account__c);
            }

 List<Account> accountList = new List<Account>([SELECT Owner.Firstname, Owner.LastName, Name, Region__c FROM Account WHERE id IN :accountIDs]);
        for(Account a:accountList) {
                  //set your field values here.
   
   DateTime nowDT=System.now();
String formatted=nowDT.format('dd_MM_yyyy');
   c.Name = a.Name + ' ' + nowDT;

   c.Account_Executive__c = a.owner.FirstName + ' ' + a.owner.LastName;
   c.Region__c = a.Region__c;
   c.RME_Date__c = date.today();
  

bob_buzzardbob_buzzard

By doing this you have the date correctly formatted in the variable "formatted", but you aren't using it.

 

You need to change the code to assign this to the field:

 

 

 DateTime nowDT=System.now();
String formatted=nowDT.format('dd_MM_yyyy');
   c.Name = a.Name + ' ' + nowDT;
   c.Account_Executive__c = a.owner.FirstName + ' ' + a.owner.LastName;
   c.Region__c = a.Region__c;
   c.RME_Date__c = formatted;
   

 

 

This was selected as the best answer
larrmill59larrmill59

Fantastic!

 

I had a feeling that the date wasn't being used but I couldn't figure out where the problem was. This is actually being used in a different field than your last block of code indicated but it does work now. Below is the final piece of code.

 

DateTime nowDT=System.now();
    String formatted=nowDT.format('MM/dd/yyyy');
   c.Name = a.Name + ' ' + formatted;

 

Thanks for all your help.

max4904max4904

I am trying to do a similar thing. I want to concatenate 3 object fields into a new custom field so I can have a new Account Plan Name field that equals Account Name + Planning Year + Created Date. Do I use a trigger to accomplish this or something else?  What does the whole code sample need to look like? 

 

Thanks for your help.

bob_buzzardbob_buzzard

You should be able to achieve this via a formula field rather having to involve triggers.  

larrmill59larrmill59

I had already created a trigger to create the record and to initiate an approval process, so adding a field to the trigger made sense. If I didn't already have this in place, I would have used a formula to concatenate the fields as Bob suggests.

 

Something to keep in mind when working with formulas is that they run whenever a record is displayed. This means that you can't use a function like Today() as part of your new field since this will change every time you open the record. To get around this issue, I placed my formula into a workflow that updates the field and it only runs when the record is first created, thereby locking in the value I want.

 

You may also have problems with a formula field that refers to itself or depends on values that haven't yet been committed. In other words, if "planning year" and "account name" are on the same record as the formula, it can't complete properly when the record is being created. That's another reason to place the formula into a workflow since it requires a Save action in order to fire.