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
Revati BhavsarRevati Bhavsar 

How to create a note automatically which logs an activity?

Hi,
How to create a note automatically, if a user changes the Shipping or billing address of an Account, which logs this activity, and stores the old value of the Address?
 
SandhyaSandhya (Salesforce Developers) 
Hi Revathi,

You can write trigger on account which will create note whenever there is an update.

Please see below sample code (make changes according to your requirement)
trigger insertNote on Account (after insert, after update){

    List<Note> noteList = new List<Note>();

    if(Trigger.isAfter && Trigger.isInsert){

        for(Account a : trigger.new){
            if(a.BillingAddress != null){
                Note newNote = new Note();
                newNote.Title = 'Custom Title';
                newNote.Body = a.BillingAddress;
                newNote.ParentId = a.Id;
                noteList.add(newNote);
            }
        }
    }

    if(Trigger.isAfter && Trigger.isUpdate){
        for(Account a1 : trigger.new){
            if(trigger.oldMap.get(a1.id).notes__c != a1.BillingAddress){
                Note newNote = new Note();
                newNote.Title = 'Custom Title';
                newNote.Body = a1.BillingAddress	;
                newNote.ParentId = a1.Id;
                noteList.add(newNote);
            }
        }
    }


    if(noteList.size() > 0){
        insert noteList;
    }
}

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya