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
TheLearnerTheLearner 

Trigger on Notes and attachment.

Hi Experts,

Could any one write trigger on this please:
"Notes & Attachment section – Delete should not be available to any user except System Administrator for the Notes & Attachments."

Thanks in advance
Best Answer chosen by TheLearner
Shrikant BagalShrikant Bagal
To write a trigger on Attachment you have to use Eclipse. Open your Org in Eclipse then create a new trigger where you will get a picklist to select the Attachment Object. This is the only the way to write a trigger on Attachment. Below is the sample code of the trigger :

trigger TiggerName on Attachment (before delete)
            {                        
                       Id profileId=userinfo.getProfileId();
                       String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
                       for(Attachment attachment : Trigger.New)
                        if(!'System Administrator'.Equals(profileName))
                        {
                                    attachment .addError('Access denied!');
                        } 
                     }
            }

Hope it will help you.!!!!!!!!

All Answers

Vetriselvan ManoharanVetriselvan Manoharan
Hi, 

You can assign the Delete access only to system administrator or on layout show the button only for admins and for others disable it.


Thanks
Vetri
Shrikant BagalShrikant Bagal
To write a trigger on Attachment you have to use Eclipse. Open your Org in Eclipse then create a new trigger where you will get a picklist to select the Attachment Object. This is the only the way to write a trigger on Attachment. Below is the sample code of the trigger :

trigger TiggerName on Attachment (before delete)
            {                        
                       Id profileId=userinfo.getProfileId();
                       String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
                       for(Attachment attachment : Trigger.New)
                        if(!'System Administrator'.Equals(profileName))
                        {
                                    attachment .addError('Access denied!');
                        } 
                     }
            }

Hope it will help you.!!!!!!!!
This was selected as the best answer
TheLearnerTheLearner
HI Manoj,

Cant we write one trigger for the notes&attachment. if yes please help me out
ManojjenaManojjena
Hi TheLearner,
Note and Attchment are two different object in Salesforce .So we have to write two trigger.


Click on Setup>Developer Console>File >New>ApexTrigger > New >Give Trigger Name >Select Object .  

Please write two trigger any way it is not possible to write one trigger .

Thanks 
Manoj
 
TheLearnerTheLearner
HI Manoj,

could you write trigger on workflow on this please.

I need to send an automatic email to user when while filling the first time.could anyone write workflow or trigger on this please

Form 7 Cert. Issued (Email only while/after filling the first time) – Email should be sent to AE / Site Contact (Lookup Email in tRIIO Resources for that Contact) & also to a DOT BOX which are Mobs.backoffice@triio.co.uk; Admin.Mobs@morrisonus.com. 

This is the field which is there in the TRioo resource(Email__c).

Here Form 7 Cert. Issued is field for the triio work documentation


email templte content is

Hi,

Please be aware that the Form 7 Certificate has been Issued for the Temporary work (Temporary Work ID goes here).

The link to the temporary work at Isis is - https://cs8.salesforce.com/aD0L000000000e6

Please contact the temporary works team if case of any concerns.

Regards,

tRIIO Temporary Works Team

tRIIO® is a joint venture partnership between Morrison Utility Services and Skanska Construction UK.
A gas distribution strategic partner.

Think green - keep it on the screen. This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.
 
TheLearnerTheLearner
this notes and attachment for the object called "tRIIO_MOB_Detail__c", could anyone tell how to write for this
TheLearnerTheLearner
Hi Srikanth,

i need to do some amendments in my trigger, when ever Defect_on_Hold__c checkbox  is checked, i need to change this Activity_Status__c field value from    "INTrR" to "HOLD". im not getting where and how exactly i need to amendments,Could anyone help please.


trigger tRIIO_Calc_Activity_Status_And_Fields on tRIIO_Activity__c (before update, before insert) {
    //To calculate Reinstatement Status
    if(!Test.isRunningTest())
    if(Trigger.isUpdate){
        Boolean isActivityWithoutOM  = false;
        Boolean isAnyOMWithoutFM = false;
        Boolean doesAnyFMHaveInterimDate = false;
        String Status = ''; 
        Integer countInterimDate = 0;
        for(tRIIO_Reinstatement_FM__c fm:[SELECT Id, Interim_Date__c FROM tRIIO_Reinstatement_FM__c WHERE Activity__c =: trigger.new[0].id]){
            if(fm.Interim_Date__c != null)
                countInterimDate++;
        }
        List<tRIIO_Reinstatement_OM__c> OM = [select id, name,FM__c from tRIIO_Reinstatement_OM__c where Activity__c =: trigger.new[0].id ];
        Boolean isNotWithOutOM = false;
        Boolean isAnyOMWOFM = false;
        Boolean doesNiFMHaveInterimDate = false;
        String NGA_Work_Status_Text = '';
        if (OM.size()==0) {
            isActivityWithoutOM = true;
        } else {   
            //Loop for all OMs and if any OM is without FM then set the flag
            for(tRIIO_Reinstatement_OM__c a : OM) {
                if (a.FM__c!=true) {
                    isAnyOMWithoutFM = true;
                }
           }
           if (countInterimDate > 0) {
               doesAnyFMHaveInterimDate = true;
           } 
        }
        //if there are no Original Measures for Notice set Status to 'Schld'
        if (isActivityWithoutOM == true) {
            Status = 'Schld';
        } else {
            //if any original measures is without a final measures set status to 'InPrg'
            if (IsAnyOMWithoutFM == true) {
                Status = 'InPrg';
            } else {
                //if any final measures have an interim date set status to 'InPrg' else set to 'IntReinst'
                if (DoesAnyFMHaveInterimDate == true) {
                    Status = 'IntrC';
                } else {
                    Status = 'PermC';
                }
            }   
         }
        trigger.new[0].Status__c = Status;
    }
    //To calculate Activity Status
    if(Trigger.isInsert || Trigger.isUpdate){
        String ActivityStatus = '';
        tRIIO_Activity__c [] ActivityNew;
        ActivityNew = trigger.new;
        for( tRIIO_Activity__c Nots : ActivityNew){
            if(Nots.Activity_Status__c != 'IntrR(S)'){
                Nots.Clk_Int_Perm__c = false;
            }
        }
        Profile p = [SELECT Id,Name FROM profile WHERE id=:Userinfo.getProfileId()];
        String currentProfile = p.Name;
        String readOnlyProfiles = 'NG Read-Only';//Add other read-only profiles here seperated by ,
        if (readOnlyProfiles.indexOf(currentProfile) > -1){
            ActivityNew[0].addError('You do not have sufficient privilages to add or modify records.');
        }
        for(tRIIO_Activity__c activity : ActivityNew){
            if ((activity.Activity_Status__c != 'IntrR(S)')&&(activity.Activity_Status__c != 'Cancelled') ) {
                if (activity.Abandoned__c == true){
                    if(activity.Hit_Int_Perm__c == true){
                        ActivityStatus = 'Awaiting Notice';
                    } else{
                        ActivityStatus = 'Abandoned';
                    }
              }
              else{
                  //If Works Close Date AND Registration Date exists AND Reinstatement Status = "Schld" then "Nil Reinstatement"
                  if ((activity.Works_Close__c != null) && (activity.Registration__c != null) && (activity.Status__c == 'Schld'))
                      ActivityStatus = 'Nil Reinstatement';
                  //If Works Close Date AND Registration Date exists AND Reinstatement Status = "PermC" then "PermR"
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c != null) && (activity.Status__c == 'PermC'))
                      ActivityStatus = 'PermR';
                  //If Works Close Date exists AND Registration Date does not exist AND Reinstatement Status = "PermC" then "PermC"     
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c == null) && (activity.Status__c == 'PermC'))
                      ActivityStatus = 'PermC';
                  //If Works Close Date AND Registration Date exists AND Reinstatement Status = "IntrC" then "IntrR" 
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c != null) && (activity.Status__c == 'IntrC'))
                      ActivityStatus = 'IntrR';
                  //If Works Close Date exists AND Registration Date does not exist AND Reinstatement Status = "IntrC" then "IntrC"  
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c == null) && (activity.Status__c == 'IntrC'))
                      ActivityStatus = 'IntrC';
                  //If Actual Start Date exists then "InPrg
                  else if ((activity.Actual_Start__c != null))
                      ActivityStatus = 'InPrg';
                  //If Works Start Date exists then "Notice Raised" 
                  else if ((activity.Works_End_Date__c != null))
                      ActivityStatus = 'Notice Raised';
                  else
                      ActivityStatus = 'Awaiting Notice';                    
               }
               activity.Activity_Status__c = ActivityStatus;
               //If this is a new record then default reinstatement status to 'Schld'
               if(Trigger.isInsert)
                   activity.Status__c = 'Schld';
            }
            //when Reinstatement status changes to a status that is not 'PermC'or 'IntrC'then deattach Activity from closure report
            //so that it can can be added to next batch
            if ((activity.Status__c != 'PermC')&&(activity.Status__c != 'IntrC')){
                activity.Closure_Report__c = null;
            }
            if(Trigger.isInsert){
                Map<Id,Double> mapCounts = tRIIO_QuotationAttachment.ValidateNotice(ActivityNew);
                for(tRIIO_Activity__c activity_Quotation: Trigger.new){                
                    if(activity_Quotation.Works_Order__c != null){
                        if(mapCounts.get(activity_Quotation.Works_Order__c) != null && mapCounts.get(activity_Quotation.Works_Order__c) != 0.0){
                            activity_Quotation.addError('Notice already generated for this Quotation'); 
                        }
                    }
                }
            }    
        }
    }