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
zhagrizhagri 

Text Box automatically Archive to Notes and Attachments

How do you archive text from a Rich Text Box automatically transfer to Notes and Attachments (related list).  

 

See problem:

 

Description Field: Account Update:

1.

2.

3.

4.

5

.

.

.

25.

 

After line 25, text automatically, on weekly basis, to be transferred to "notes and attachments".

 

Any suggestions?

 

sfdcfoxsfdcfox

Use a trigger to auto-archive in real-time, or use a Batch Apex scheduler to have it done periodically. Here's the realtime solution:

 

trigger archiveDescription on Account (after update) {
    List<Note> notes = new List<Note>();
    for(Account a:Trigger.old)
        if(a.Description != null && a.Description != '' && a.Description != Trigger.newMap.get(a.id).Description)
            notes.add(new Note(Title=a.Name,Body=a.Description,ParentId=a.id));
    insert notes;
}

 

zhagrizhagri

 

Can you please explain where to insert the code?

 

Error comes up when inserting under: App Setup>Accounts>Fields>Account Update>New Validation Rules

 

Thanks!

 

sfdcfoxsfdcfox

A "Validation Rule" can't perform this type of action. You need to create what is called a "trigger", which is synonomous to a database trigger in SQL (such as MySQL). You will need a "Sandbox" in order to create this code, so visit Setup > Data Management > Sandboxes and create a Sandbox. Next, log in to your new Sandbox, and place the code in Setup > Customize > Accounts > Triggers. You'll also need a "test method." You will enter your test method under Setup > Develop > Apex Classes.

 

You will need the following code:

 

@isTest
private class testAccountTrigger {
    private static testMethod void test() {
        Account a = new Account(Name='Test Account',Description='Note 1');
        insert a;
        a.Description = 'Note 2';
        update a;
        a = [select id,name,description,(select id,title,body from notes order by id asc) from account where id = :a.id];
        system.assertEquals(a.notes.size(),1);
        system.assertEquals('Note 1',a.notes[0].body);
        a.Description = null;
        update a;
        a = [select id,name,description,(select id,title,body from notes order by id asc) from account where id = :a.id];
        system.assertEquals(a.notes.size(),2);
        system.assertEquals('Note 2',a.notes[1].body);
        a.Description = 'Note 3';
        update a;
        a = [select id,name,description,(select id,title,body from notes order by id asc) from account where id = :a.id];
        system.assertEquals(a.notes.size(),2);
    }
}

This code achieves 100% coverage, and demonstrates how code should be tested in a normal test method.

 

Once you have created the trigger and class in your Sandbox, you need to deploy it to production. To do this, just click on Setup > Deploy > Outbound Change Sets, create a new set, add the trigger and class to the set, and then use the Upload button to push this code into production. Alternatively, you could go to Setup > Create > Packages, create a new package, add the components, upload, then use the "install link" when the files are done uploading. Either way, this will allow you to get your code into your production organization.

zhagrizhagri

thanks!

 

trigger needs to create a note whenever the size of description in Account Update - text box, is more than 25 lines  then account update text is archived.

 

A trigger  needs to check whether the size of description is equal to or greater than 25 lines or not, then a note should be created which contain all the description after line 25.

 

I need to make some modifications in your trigger, they are:

 

1.Check for the no. of lines of description.

2.If greater than 25 then create a note with remaining description.

 

Can you help write the code for this?