• Ben Allington
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Firstly I am relatively inexperienced with apex but im trying to learn.
Basic background I have created a class so that:
Whenever scrum team is created 'agf_ADM_scrum_team__c'
look up via account, gather the account ids, query salesforce to get all contact ids that are related to the account id, and share the team record. I have written a basic after insert trigger.

ScrumTeamSharing
 
trigger ScrumTeamSharing on agf__ADM_Scrum_Team__c(after insert) {
    if (trigger.isInsert) {
//teamid=a6PA0000000J1AdMAK
//agf__ADM_Scrum_Team__c.Account_ID__c = associated account;
//agf__ADM_Scrum_Team__c.Id = team id;
        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');
                 }
            }   
        }
    }
This could probably use improvement so if you can suggest anything I would appreciated it greatly. 
As for the test class it runs and passes but has 0 code coverage.
its here below

SharingTest
@isTest 
private class ScrumTeamSharingTest {
	
	@isTest static void testTeamSharing() {
		//create contact for test
		agf__ADM_Scrum_Team__c t = new agf__ADM_Scrum_Team__c();
		t.Name='testTeam';
		t.agf__Cloud__c='IT';
		insert t;
		List<Contact> users = new List<Contact>();
		Account a = new Account();
		a.Name = 'TestAcc';
		insert a;
		t.Account_ID__c=a.Id;
		System.Assert(a.Id != null, 'The Test Account did not insert properly, please check validation rules and other mechanisms');
		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;
		upsert t;
		Id User1Id = users[0].Id;
        Id User2Id = users[1].Id;



		//System.Assert(users.Id != null, 'The users did not insert properly, please check validation rules and other mechanisms');
		System.Assert(t.Id != null, 'The team did not insert properly, please check validation rules and other mechanisms');
		System.debug('Inserted team & acct: ' + t.Name + ' ' + a.Name);
		}
}

This is where im a little confused if someone could show me what it should look like that would be fantastic.

Thanks again
 
I am very new to apex development but I have created a class for whenever scrum team is created,
the trigger looks up via account, gathers the account id, queries salesforce to get all contact id related to account id, and shares team record. The class is like so
ScrumTeamSharing
trigger ScrumTeamSharing on agf__ADM_Scrum_Team__c(before insert) {
	if (trigger.isInsert) {
//teamid=a6PA0000000J1AdMAK
//agf__ADM_Scrum_Team__c.Account_ID__c = associated account;
//agf__ADM_Scrum_Team__c.Id = team id;
		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');
		         }
		    }	
		}
	}
My test class is as follows
ScrumTeamSharingTest
@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();
		Account a = new Account(Name='Test Account');
		t.Name='testTeam';
		t.Account_ID__c=a.Id;
		insert a;
		insert t;
		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;
		}
}
And when it runs the error which I am recieving is like so
Error
ScrumTeamSharingTest: (0/1) Passed

 METHOD RESULT 
testTeamSharing : Fail

 STACK TRACE 
Class.ScrumTeamSharingTest.testTeamSharing: line 11, column 1

 MESSAGE 
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, DedupeReminder: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Trigger.DedupeReminder: line 7, column 1: []
I am very new to apex development but I have created a class for whenever scrum team is created,
the trigger looks up via account, gathers the account id, queries salesforce to get all contact id related to account id, and shares team record. The class is like so
ScrumTeamSharing
trigger ScrumTeamSharing on agf__ADM_Scrum_Team__c(before insert) {
	if (trigger.isInsert) {
//teamid=a6PA0000000J1AdMAK
//agf__ADM_Scrum_Team__c.Account_ID__c = associated account;
//agf__ADM_Scrum_Team__c.Id = team id;
		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');
		         }
		    }	
		}
	}
My test class is as follows
ScrumTeamSharingTest
@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();
		Account a = new Account(Name='Test Account');
		t.Name='testTeam';
		t.Account_ID__c=a.Id;
		insert a;
		insert t;
		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;
		}
}
And when it runs the error which I am recieving is like so
Error
ScrumTeamSharingTest: (0/1) Passed

 METHOD RESULT 
testTeamSharing : Fail

 STACK TRACE 
Class.ScrumTeamSharingTest.testTeamSharing: line 11, column 1

 MESSAGE 
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, DedupeReminder: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Trigger.DedupeReminder: line 7, column 1: []
hi
i am new to salesforce platform.
Install a simple Apex class, write unit tests that achieve 100% code coverage for the class, and run your Apex tests.The Apex class to test is called 'VerifyDate', and the code is available here. Copy and paste this class into your Developer Edition via the Developer Console.
'VerifyDate' is a class which tests if a date is within a proper range, and if not will return a date that occurs at the end of the month within the range.
The unit tests must be in a separate test class called 'TestVerifyDate'.
The unit tests must cover scenarios for all lines of code included in the Apex class, resulting in 100% code coverage.
Run your test class at least once (via the Developer Console) before attempting to verify this challenge.

In the above challege i am not able to write a 'TestVerifyDate' class. I dont unsertstand how to write it for a date.
plz help..