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&&UserInfo.getProfileId() !=adminId1.Id
        
        if((trigger.isBefore && trigger.isInsert) && UserInfo.getProfileId() != adminId.Id&&Userinfo.getProfileId() !=adminId1.id){
            for(Invoice__c i: Trigger.new){
                Opportunity.add(i.Opportunity1__c);
            
            List<Invoice__c> lstBatAlt =[select id,Opportunity1__c,Name 
                                         from Invoice__c
                                         where Opportunity1__c IN:Opportunity and Opportunity1__r.recordType.developerName != 'Concierge' ];
                
            if(lstBatAlt.size()>1){
                i.addError('You cannot create more than two receipts for opportunity');
            }
        } 
         }
    }

Regards
Srikanth
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 
https://teachmesalesforce.wordpress.com/category/code-sample/

Test class for controller

http://amitsalesforce.blogspot.sg/2015/06/best-practice-for-test-classes-sample.html

Best Practices of Test class
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

http://www.jitendrazaa.com/blog/salesforce/apex/faq-writing-test-class-in-salesforce/

Please check the below links for more details on test classes

http://www.tutorialspoint.com/apex/apex_testing.htm