• Oksana Dovbak
  • NEWBIE
  • 30 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 8
    Replies
Hello,

I am looking to create a custom field that will update after event insert to count the number of RelationId's on each event. Help please!
Hello,

Can someone please help me to write a test class for this Apex Trigger - 
 
trigger AttachmentSaver_v2 on Attachment (before delete) {
    // Get the current user's profile name
    Profile prof = [select Name from Profile where Id = :UserInfo.getProfileId() ];
    set<Id> oppSet = new set<Id>();    
    // If current user is not a System Administrator, do not allow Attachments to be deleted
    System.debug(Logginglevel.ERROR+'Inside trigger Name '+prof.Name);
    if (!'System Administrator'.equalsIgnoreCase(prof.Name)) {
        System.debug(Logginglevel.ERROR+'Inside IF Name '+prof.Name);
        for (Attachment a : Trigger.old) {            
            oppSet.add(a.PARENTID);
        }  
        
        if(oppSet != null){           
            Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([SELECT Id,Name,StageName FROM Opportunity WHERE ID IN: oppSet]);
            
            for (Attachment a : Trigger.old) {
                if(oppMap.containsKey(a.ParentId) && oppMap.get(a.ParentId).StageName == 'Closed Won')
                    a.addError('You can not delete, Opportunity Stage is '+oppMap.get(a.ParentId).StageName);
            }             
        }
    }
}

The following test I have is only giving me 30% code coverage - 
 
@isTest
private class TestAttachmentSaver {
  
  /**
  * Verify that Standard User profiles are unable to delete attachments
  */
  static testMethod void testStandardUser() {
    
    // Create a new user with the Standard User profile
    Profile standardProf = [select id from profile where name='Standard User']; 
    User su = new User(alias = 'standt', email='standarduser@testorg.com', 
      emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
      localesidkey='en_US', profileid = standardProf.Id, 
      timezonesidkey='America/Los_Angeles', username='standarduser231@testorg.com');
  }
 static testMethod void testAdminUser() {
    
    // Next make sure that a System Admin *can* delete an attachment
    Profile adminProf = [select id from profile where name='System Administrator']; 
    User au = new User(alias = 'admint', email='adminuser@testorg.com', 
      emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
      localesidkey='en_US', profileid = adminProf.Id, 
      timezonesidkey='America/Los_Angeles', username='adminuser123456789@testorg.com');
    
    // Switch current user to System Admin user
    System.runAs(au) {
      
      // Create test data (a new Account with an Attachment)
      Account acct = new Account(Name = 'Test Account');
      insert acct;
      Blob attachBody = Blob.valueOf('attachment body');
      Attachment attach = new Attachment(Name = 'TestAttachment', ParentId = acct.Id, Body = attachBody);
      insert attach;
      
      // Verify that no error is thrown when the attachment is deleted
      Boolean errorThrown = false;
      try {
        delete attach;
      } catch (Exception e) {
        System.debug(e);
        errorThrown = true;
      }
      System.assert(!errorThrown);   
        
    // Switch current user to Standard User
    }
    System.runAs(au) {    
    
      // Create test data (a new Account with an Attachment)
      Account acct = new Account(Name = 'Test Account');
      insert acct;
      Blob attachBody = Blob.valueOf('attachment body');
      Attachment attach = new Attachment(Name = 'TestAttachment', ParentId = acct.Id, Body = attachBody);
      insert attach;
    }

  
 
    
  }
}

Thank you!!!
Hello,

I am trying to modify this existing trigger that prevents someone from deleting an attachment to only fire if the associated parent opportunity stage is Closed Won - 
 
trigger AttachmentSaver on Attachment (after delete) {
  // Get the current user's profile name
  Profile prof = [select Name from Profile where Id = :UserInfo.getProfileId() ];
  
  // If current user is not a System Administrator, do not allow Attachments to be deleted
  if (!'System Administrator'.equalsIgnoreCase(prof.Name)) {
    for (Attachment a : Trigger.old) {
      a.addError('Unable to delete attachments.');
    }  
  }
}

What do I need to add in here that would only fire this error for Closed Won Opps?

Thank you,
Oksana​
Hello,

I'm trying to create an apex class to use in testing an attachment trigger I created below - 

 
//Trigger to update the Send_Attachment_Email__c custom checkbox field on Opportunity if it has an attachment.

trigger attachmentTrigger on Attachment (after insert) {

list<Opportunity> o = [select id,   Send_Attachment_Email__c, StageName from Opportunity where id =: Trigger.New[0].ParentId] ;
    if(o.size()>0 ) {
        o[0].Send_Attachment_Email__c = true;
        update o;
    }
    }



Could you please advise me on the apex class needed to make sure this satisfies the code coverage?
Hello,

I am looking to create a custom field that will update after event insert to count the number of RelationId's on each event. Help please!
Hello,

Can someone please help me to write a test class for this Apex Trigger - 
 
trigger AttachmentSaver_v2 on Attachment (before delete) {
    // Get the current user's profile name
    Profile prof = [select Name from Profile where Id = :UserInfo.getProfileId() ];
    set<Id> oppSet = new set<Id>();    
    // If current user is not a System Administrator, do not allow Attachments to be deleted
    System.debug(Logginglevel.ERROR+'Inside trigger Name '+prof.Name);
    if (!'System Administrator'.equalsIgnoreCase(prof.Name)) {
        System.debug(Logginglevel.ERROR+'Inside IF Name '+prof.Name);
        for (Attachment a : Trigger.old) {            
            oppSet.add(a.PARENTID);
        }  
        
        if(oppSet != null){           
            Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([SELECT Id,Name,StageName FROM Opportunity WHERE ID IN: oppSet]);
            
            for (Attachment a : Trigger.old) {
                if(oppMap.containsKey(a.ParentId) && oppMap.get(a.ParentId).StageName == 'Closed Won')
                    a.addError('You can not delete, Opportunity Stage is '+oppMap.get(a.ParentId).StageName);
            }             
        }
    }
}

The following test I have is only giving me 30% code coverage - 
 
@isTest
private class TestAttachmentSaver {
  
  /**
  * Verify that Standard User profiles are unable to delete attachments
  */
  static testMethod void testStandardUser() {
    
    // Create a new user with the Standard User profile
    Profile standardProf = [select id from profile where name='Standard User']; 
    User su = new User(alias = 'standt', email='standarduser@testorg.com', 
      emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
      localesidkey='en_US', profileid = standardProf.Id, 
      timezonesidkey='America/Los_Angeles', username='standarduser231@testorg.com');
  }
 static testMethod void testAdminUser() {
    
    // Next make sure that a System Admin *can* delete an attachment
    Profile adminProf = [select id from profile where name='System Administrator']; 
    User au = new User(alias = 'admint', email='adminuser@testorg.com', 
      emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
      localesidkey='en_US', profileid = adminProf.Id, 
      timezonesidkey='America/Los_Angeles', username='adminuser123456789@testorg.com');
    
    // Switch current user to System Admin user
    System.runAs(au) {
      
      // Create test data (a new Account with an Attachment)
      Account acct = new Account(Name = 'Test Account');
      insert acct;
      Blob attachBody = Blob.valueOf('attachment body');
      Attachment attach = new Attachment(Name = 'TestAttachment', ParentId = acct.Id, Body = attachBody);
      insert attach;
      
      // Verify that no error is thrown when the attachment is deleted
      Boolean errorThrown = false;
      try {
        delete attach;
      } catch (Exception e) {
        System.debug(e);
        errorThrown = true;
      }
      System.assert(!errorThrown);   
        
    // Switch current user to Standard User
    }
    System.runAs(au) {    
    
      // Create test data (a new Account with an Attachment)
      Account acct = new Account(Name = 'Test Account');
      insert acct;
      Blob attachBody = Blob.valueOf('attachment body');
      Attachment attach = new Attachment(Name = 'TestAttachment', ParentId = acct.Id, Body = attachBody);
      insert attach;
    }

  
 
    
  }
}

Thank you!!!
Hello,

I am trying to modify this existing trigger that prevents someone from deleting an attachment to only fire if the associated parent opportunity stage is Closed Won - 
 
trigger AttachmentSaver on Attachment (after delete) {
  // Get the current user's profile name
  Profile prof = [select Name from Profile where Id = :UserInfo.getProfileId() ];
  
  // If current user is not a System Administrator, do not allow Attachments to be deleted
  if (!'System Administrator'.equalsIgnoreCase(prof.Name)) {
    for (Attachment a : Trigger.old) {
      a.addError('Unable to delete attachments.');
    }  
  }
}

What do I need to add in here that would only fire this error for Closed Won Opps?

Thank you,
Oksana​
Hello,

I'm trying to create an apex class to use in testing an attachment trigger I created below - 

 
//Trigger to update the Send_Attachment_Email__c custom checkbox field on Opportunity if it has an attachment.

trigger attachmentTrigger on Attachment (after insert) {

list<Opportunity> o = [select id,   Send_Attachment_Email__c, StageName from Opportunity where id =: Trigger.New[0].ParentId] ;
    if(o.size()>0 ) {
        o[0].Send_Attachment_Email__c = true;
        update o;
    }
    }



Could you please advise me on the apex class needed to make sure this satisfies the code coverage?