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
tomoko_otomoko_o 

trigger to mark attachment private

Hi,

 

I need some help on creating trigger for attachment for case and workorder object.
I found the code to make all attachment as private before insert, but how can I make it work only for case and workordre object?

Which field can tell what object the attachment is being attached to?

 

Thank you,

trigger PrivateAttachment on Attachment (before insert) {
  for(Attachment record:Trigger.new)   {
      record.IsPrivate = TRUE;
   }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Try this

 

trigger PrivateAttachment on Attachment (before insert) {
string s;
for(Attachment record:Trigger.new) {
if((String.valueOf(reocrd.parentId)).startswith('500') || (String.valueOf(reocrd.parentId)).startswith('XXX')) //xxx would be your custom object prefix
record.IsPrivate = TRUE;
}
}

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

trigger PrivateAttachment on Attachment (before insert) {
for(Attachment record:Trigger.new) {

if((reocrd.parentId).startswith('500') || (reocrd.parentId).startswith('XXX')) //xxx would be your custom object prefix
record.IsPrivate = TRUE;
}
}

tomoko_otomoko_o

Thank you for your reply.

I tried the code but got error saying "Error: Compile Error: Method does not exist ro incorrect signature: [id].Startswith(String) at line 3 column27"

 

Anything else I can try?

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Try this

 

trigger PrivateAttachment on Attachment (before insert) {
string s;
for(Attachment record:Trigger.new) {
if((String.valueOf(reocrd.parentId)).startswith('500') || (String.valueOf(reocrd.parentId)).startswith('XXX')) //xxx would be your custom object prefix
record.IsPrivate = TRUE;
}
}

This was selected as the best answer
tomoko_otomoko_o

Thank you!!

That was it.