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
Pallavi singhPallavi singh 

Trigger Test Class with an "Invalid Parameter value"

Hi,

This is the trigger

trigger No_Contacts_Not_Related_to_Account on Task (before insert, before update) {
    List<Task> tList = trigger.new;
    List<Id> idList = new List<Id>();
    for(Task t : tList) {
        if(t.whoId != null) idList.add(t.whoId);
    }
    Map<Id, Contact> cMap = new Map<Id, Contact>([select id, accountId from contact where Id in: idList]);

    List<Task> tList_failed = new List<Task>();
    for(Task t : tList) {
        if(t.whoId != null && t.whatId != null && t.whatId != cMap.get(t.whoId).accountId) tList_failed.add(t);
    }

  if(tList_failed.size() > 0) tList[0].addError('Achtung: Ansprechpartner ist nicht beim ausgewählten Debitor hinterlegt ');
}


And here is the Test class but i get this error message "invalid Parameter value" dont know why.

@isTest
Private class No_Contacts_Not_Related_to_Account_Test {

    static testMethod void testTrigger() {
        // Create test Account
        Account acc = new Account(Name = 'Test Account');
        insert acc;

        // Create test Contact related to Account
        Contact con = new Contact(FirstName = 'Test', LastName = 'Contact', AccountId = acc.Id);
        insert con;

        // Create test Contact not related to Account
        Contact con2 = new Contact(FirstName = 'Test2', LastName = 'Contact2');
        insert con2;

        // Create test Tasks with related and not related Contacts
        Task t1 = new Task(Subject = 'Test Task 1', WhoId = con.Id, WhatId = acc.Id);
        Task t2 = new Task(Subject = 'Test Task 2', WhoId = con2.Id, WhatId = acc.Id);

        Test.startTest();
        // Insert Tasks and verify that the trigger throws an error on t2
        try {
            insert new List<Task>{t1, t2};
            System.assert(false, 'Expected error was not thrown');
        } catch (DmlException e) {
            System.assertEquals('Achtung: Ansprechpartner ist nicht beim ausgewählten Debitor hinterlegt ', e.getMessage());
        }
        Test.stopTest();
    }
}

Need help with this test class

Thank you

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi pallavi,

Can you confirm where excatly you are getting error? Is it while running the test class?

Thanks,
 
Pallavi singhPallavi singh
Hi Praveen,

I m unable to save the trigger 

User-added image

Thanks,
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pallavi,

I dont think this error is related to this trigger and test class. Because I was able to save with out any errors. Also the Class name itself is different in the error.
Let me know if you face any issues.


Thanks,
Pallavi singhPallavi singh
Hi Praveen,

But its not coverage is still 0% 

Do you know why?

Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pallavi,

Can you try below test clas.
 
@isTest
Private class No_Contacts_Not_Related_to_Account_Test {

    static testMethod void testTrigger() {
        // Create test Account
        Account acc = new Account(Name = 'Test Account');
        insert acc;

      
        Contact con2 = new Contact(FirstName = 'Test2', LastName = 'Contact2');
        insert con2;

       Task t2 = new Task(Subject = 'Test Task 2', WhoId = con2.Id, WhatId = acc.Id);

       Test.startTest();
     Database.SaveResult result = Database.insert(t2, false);
        System.assertEquals('Achtung: Ansprechpartner ist nicht beim ausgewählten Debitor hinterlegt ',result.getErrors()[0].getMessage());
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pallavi,

Were you able to get coverage for trigger.

Thanks
Pallavi singhPallavi singh
Hi Praveen,

It's not working i get this error message.

User-added image
Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shruthi,

Did you try the same test class which I shared because I dont see any method called testtrigger in my test class.

Thanks,