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
HBF SFDCHBF SFDC 

Trigger syntax for combining data from 2 text boxes

Hello Community-

 

I'm trying to build a threaded results textbox that auto posts the date and name of the person contributing. I need the results inside of an SFDC form.

 

My idea is to create 2 fields - one for adding a message and one for displaying the thread.  

Field 1 - long text - Type_Here__c

Field 2 - long text - Display_Here__c

 

Jim Smith logs in to the record and wants to comment.  He types "Hello World." in Type_Here__c

After saving the record, Display_Here__c looks like this:

 

7/1/2010 - Jim Smith - Hello World.

 

Next, Stacy Jones needs to add her info and types "Sounds good." in Type_Here__c

After saving the record, Display_Here__c should look like this:

 

7/2/2010 - Stacy Jones - Sounds good.

 

7/1/2010 - Jim Smith - Hello World.

 

For all subsequent messages, they must be typed in the "Type_Here__c" field and the "Display_Here__c" field is locked for editing.  Each new message is a prefix of the old messages with the name and date stamp.

 

I don't know code, so if as much syntactically correct code could be provided, I think I could tweak a little to make it work, but I'm terrible at inventing the new code necessary (I'm hunting for a programmer as we type).

 

Thanks in advance for any help!

 

Aaron

Best Answer chosen by Admin (Salesforce Developers) 
HBF SFDCHBF SFDC

It turns out I was able to make this work with work flow.  I have 2 long text boxes (rich text boxes bugged out with the HTML).

 

Field 1:  Type_Here__c (3 lines visible)

Field 2:  Display_Here__c (locked for editing on the page layout) (5 lines visible)

 

The workflow is one rule to test for typing in Type_Here__c.  If it's found, a workflow occurs with 2 actions.  Action 1 is a field update related to Display_Here__c with the following logic:

 

Text(Month(Today())) & "-" & Text(Day(Today())) & "-" & Text(Year(Today())) & 
" - " & LastModifiedBy.FirstName & " " & LastModifiedBy.LastName & " - " & 
Type_Here__c & 
BR() & 
BR() & 
Display_Here__c

 

Action 2 simply clears the contents of Type_Here__c.

 

I hope this helps anybody who needs this or a similar feature and thanks for everybody who contributed!

 

Thanks,

Aaron

 

PS  I'm not sure if this is a bug, but the break HTML applied by the function BR() breaks (is removed) after each update if Display_Here__c is a rich text field.

All Answers

Mikko4Mikko4

Hi,

 

From the top of my head I'd say that you can accomplish this also with a workflow rule with a field update so you don't necesserily need to have a trigger. The field to update would be Display_Here__c and formula would then be

Display_Here__c + '\n' + Type_Here__c.

 

I'm not sure either if the line break \n works on a formula. If not then you probably want to do this on a trigger.

Mikko4Mikko4

And you can add TEXT(TODAY()) + ... to beginning to get the date.

ca_peterson_oldca_peterson_old

I don't think workflow would cut it, plus trying to get this logic into a formula is going to be tricky.

 

So, on that note, how does this look?

 

trigger threading on yourObj__c (before insert, before update){

    for(yourObj__c thisObj:Trigger.new){
        if(thisObj.type_here__c != null){
            String userName = UserInfo.getName(); //current user's name
            String timestamp = DateTime.now().format(); //Current time, .format() localizes it.
           
            thisObj.display_here__c +=
                timestamp + ' - ' + userName + ' - ' + thisObj.type_here__c+'\n';
            thisObj.type_here__c = null; //blank out the type_here field
        }
    }
}

HBF SFDCHBF SFDC

It turns out I was able to make this work with work flow.  I have 2 long text boxes (rich text boxes bugged out with the HTML).

 

Field 1:  Type_Here__c (3 lines visible)

Field 2:  Display_Here__c (locked for editing on the page layout) (5 lines visible)

 

The workflow is one rule to test for typing in Type_Here__c.  If it's found, a workflow occurs with 2 actions.  Action 1 is a field update related to Display_Here__c with the following logic:

 

Text(Month(Today())) & "-" & Text(Day(Today())) & "-" & Text(Year(Today())) & 
" - " & LastModifiedBy.FirstName & " " & LastModifiedBy.LastName & " - " & 
Type_Here__c & 
BR() & 
BR() & 
Display_Here__c

 

Action 2 simply clears the contents of Type_Here__c.

 

I hope this helps anybody who needs this or a similar feature and thanks for everybody who contributed!

 

Thanks,

Aaron

 

PS  I'm not sure if this is a bug, but the break HTML applied by the function BR() breaks (is removed) after each update if Display_Here__c is a rich text field.

This was selected as the best answer