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
DeveloperSalesforceDeveloperSalesforce 

Flag all notes and attachments as “private” for notes and attachments created prior to converting user.

Hi, 

I have a requirement while converting/creating a new user linked to contact.
1. Contact-->convert Usr-->created new user
then, Flag all notes and attachments as “private” for notes and attachments created prior to converting user.
How to achive this? Pls help
Best Answer chosen by DeveloperSalesforce
pconpcon
You would need to write a trigger that does the following before insert of the User object
 
trigger PrivateAttachment on User (before insert) {
    Set<Id> contactIds = new Set<Id>();

    for (User u: Trigger.new) {
        if (u.Contact != null) {
            contactIds.add(u.Contact);
        }
    }

    if (!contactIds.isEmpty()) {
        List<Attachment> attachments = [
            select Private
            from Attachment
            where ParentId in :contactIds and
                Private = False
        ];

        if (!attachments.isEmpty()) {
            for (Attachment attachment: attachments) {
                attachment.Private = True;
            }

            update attachments;
        }
    }
}

NOTE: This code has not been tested and my contain topographical or logical errors.