0

I have hopefully created a trigger that works and I am looking to write a test class for this, I have very little experience using apex but am trying to learn, if someone could give me some advice on writing my test class that would be great. Here is my trigger.

trigger ScrumTeamSharing on agf__ADM_Scrum_Team__c(after insert) {
if (trigger.isInsert) {

    List <agf__ADM_Scrum_Team__Share> rShare = new List <agf__ADM_Scrum_Team__Share>();
    agf__ADM_Scrum_Team__Share TeamShare;

    for(agf__ADM_Scrum_Team__c ADM : Trigger.new){
        TeamShare = new agf__ADM_Scrum_Team__Share();
        String IdTeam = ADM.Id;
        String assAcc = ADM.Account_ID__c;

        List<Account> alist = [SELECT Id, Name FROM Account WHERE Id=:assAcc];

        System.debug('a' + alist);

        List<Contact> clist = [SELECT Id, Name FROM Contact WHERE Contact.AccountId IN :alist];

        System.debug('c' + clist);
        // Set ID of record being shared
        TeamShare.ParentId = ADM.Id;

        // Set ID of user or group being granted access

        // Set Access Level
        TeamShare.AccessLevel = 'Read';

        //Parse contact Id into the sharing list
        Integer i = clist.size();
        while(i > 0) {
            TeamShare.UserOrGroupId = clist[i].Id;//add to the team reports
            i--;
                }
            }

                // Insert the sharing record and capture the save result. 
          // The false parameter allows for partial processing if multiple records passed 
          // into the operation.
          Database.SaveResult sr = Database.insert(TeamShare,false);

          // Process the save results.
          if(sr.isSuccess()){
             // Indicates success
             System.debug('Save Success');
          }
          else {
             // Get first save result error.
             Database.Error err = sr.getErrors()[0];

             // Check if the error is related to trival access level.
             // Access level must be more permissive than the object's default.
             // These sharing records are not required and thus an insert exception is acceptable. 
             if(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  &&  
                      err.getMessage().contains('AccessLevel')){
                // Indicates success.
                System.debug('Success on mapping error');
             }
             else{
                // Indicates failure.
                System.debug('Failed to map error');
             }
        }   
    }
}

As it stands i have got to this sharing class but am receiving an error when running it.

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please specify the department this team belongs to.: [] 

My Test class is like so:

@isTest
private class ScrumTeamSharingTest {

@isTest static void testTeamSharing() {
    //create contact for test
    List<Contact> users = new List<Contact>();
    agf__ADM_Scrum_Team__c t = new agf__ADM_Scrum_Team__c (Name ='testTeam');
    Account a = new Account(Name='Test Account');
    insert t;
    insert a;
    System.debug('Inserted team & acct: ' + t.Name + ' ' + a.Name);
    for (Integer j=0; j<5;j++) { //j number of contacts per account
        users.add(new Contact(firstname ='Test'+j,
                              lastname  ='Test'+j,
                              AccountId =a.Id));
    }
    insert users;
    }
}
4
  • 3
    What have you tried so far. Learn to write test classes from Trailhead Apex Testing
    – Raul
    Nov 21, 2016 at 12:56
  • 3
    Advice: Two items that can help your greatly: trailhead.salesforce.com and google.com as well as our very own site here. Start at trailhead then move to google or here when you have specific questions. learn the Concepts of testing well.
    – Eric
    Nov 21, 2016 at 12:56
  • Ive added in what i have so far
    – Nebbyyy
    Nov 21, 2016 at 13:53
  • @Nebbyyy - When you read the error message, what do you think you need to do?? It seems pretty self explanatory to me so curious where the difficulty is coming from
    – Eric
    Nov 21, 2016 at 14:29

1 Answer 1

4

I think you need to make sure that there are values in all your mandatory fields. The error could be related to a validation rule that fires if you don't insert for a a field related to "department team". As Rahul said, you should put sometime in Trailhead as well.

Not the answer you're looking for? Browse other questions tagged .