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
sreekanth reddysreekanth reddy 

test class for trigger for multiple profile

Hi All,

Please help with the following trigger. The trigger is used to prevent creation of more than 2 receipts per opportunity with the exception for one profile ( Accounts). Now if I am trying to add another profile to the trigger for excemtion, but I am not getting code coverage. Please help with possible solution.

trigger Receiptt on Invoice__c(before insert) {
    ReceiptRestiction__c ua = ReceiptRestiction__c.getInstance (userinfo.getUserId());
    if (ua.Active__c) {
        set<id> Opportunity = new set<id>();
        Profile adminId = [SELECT Id from Profile where Name='Accounts' LIMIT 1]; //First profile
        profile adminId1 =[SELECT Id from Profile where Name='System Administrator' LIMIT 1]; //Second Profile
        //&&Userinfo.getProfileId() ! !=adminId1.Id
        if((trigger.isBefore && trigger.isInsert) && UserInfo.getProfileId() != adminId.Id&&Userinfo.getProfileId() !{
            for(Invoice__c i:{
            List<Invoice__c> lstBatAlt =[select id,Opportunity1__c,Name 
                                         from Invoice__c
                                         where Opportunity1__c IN:Opportunity and Opportunity1__r.recordType.developerName != 'Concierge' ];
                i.addError('You cannot create more than two receipts for opportunity');

tarun jain 110tarun jain 110
Hi Sreekanth,

After seeing your code I think you not create proper mock for this soql as [select id,Opportunity1__c,Name  from Invoice__c  where Opportunity1__c IN:Opportunity and Opportunity1__r.recordType.developerName != 'Concierge'​]. Make sure that Opportunity1__c ​ is exist in Opportunity​, then insert your invoice. Also check ua.Active__c​ field is true or false.

Test class for Trigger:

Please check the below link

Test class for controller

Best Practices of Test class

Please check the below links for more details on test classes