+ Start a Discussion
SK SGSSK SGS 

getting the code coverage for test class

Hi All,
i am writing the test class for getting code coverage and deploy it to sandbox for that unable to get the 75%.if any one can suggest to cover the code coverage.my apex class is

public without sharing class PC_OpportunitySearchCmpCntrl {
    
    public class OpptywrapperClass{
        
        @AuraEnabled public boolean selectOppty {get;set;}
        @AuraEnabled public Opportunity Oppty   {get;set;}
        
        public OpptywrapperClass(Opportunity Opp){
            this.Oppty=Opp;
        }
    }
    
    @AuraEnabled
    public static string PerformIntialLoad(){
        
        string jsonString;
        list<String> StageValueList=new list<String>();
        StageValueList.add('All');
        
        Schema.DescribeFieldResult StagefieldResult = Opportunity.StageName.getDescribe();
        list<Schema.PicklistEntry> StagenameValues = StagefieldResult.getPicklistValues();

        for(Schema.picklistEntry objPicklistEntry : StagenameValues) {
            
            StageValueList.add(objPicklistEntry.getLabel());
            
        }
        jsonString = JSON.serialize(StageValueList);
        return jsonString;
        
    }
    
    @AuraEnabled 
    public static list<OpptywrapperClass> PopulateAllOppty(string RecordType){
        //and recordtypeid=:SalesTypeId
        string accId;
        string accType;
        String userids=UserInfo.getUserId();
        list<Opportunity> oppList;
        accId = [select Contact.accountId from User where id = :UserInfo.getUserId()].Contact.AccountId;
        accType = [select account.type from Account where id = :accId].type;
        Id OpptyRecTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get(RecordType).getRecordTypeId();
        system.debug('##### accId accType SalesTypeId '+accId+' '+accType+' '+OpptyRecTypeId+' '+userids);
        oppList = [select Id, Name, stagename,CloseDate,ownerid, Account.name ,Account.BillingCountry, Sold_to_Partner__c, Tier2__c
                   from Opportunity 
                   where (sold_to_Partner__c = :accId or Tier2__c = :accId)
                   and Isclosed=false 
                   and Id not in (select OpportunityId from OpportunityTeamMember where userid=:userids )
                   and recordtypeid=:OpptyRecTypeId
                   order by Name limit 1000];
        system.debug('###### oppList '+oppList);
        list<OpptywrapperClass> OpptyWrpList=new list<OpptywrapperClass>();
        for(Opportunity opp:oppList){
            OpptyWrpList.add(new OpptywrapperClass(opp));
        }
        system.debug('##### OpptyWrpList '+OpptyWrpList);
        return OpptyWrpList;
    }
    
    @AuraEnabled
    public static list<OpptywrapperClass> SearchOpportunityWithfiler(string SoqlFilter, String RecordType){
        list<OpptywrapperClass> OpptyWrpList=new list<OpptywrapperClass>();
        string accId=[select Contact.accountId from User where id = :UserInfo.getUserId()].Contact.AccountId;
        Id SalesTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get(RecordType).getRecordTypeId();
        String userids=UserInfo.getUserId();
        string query='select Id, Name, stagename,CloseDate,ownerid, Account.name ,Account.BillingCountry, Sold_to_Partner__c, Tier2__c from Opportunity where (sold_to_Partner__c = '+'\''+accId +'\'' + ' or Tier2__c ='+'\''+accId +'\''+') and Isclosed=false and Id not in (select OpportunityId from OpportunityTeamMember where userid='+'\''+userids+'\''+') and recordtypeid='+'\''+SalesTypeId+'\'';
        system.debug('#### query '+query);
        if(SoqlFilter!=null){
            query+=SoqlFilter+' order by Name limit 1000';
        }else{
            query+=' order by Name limit 1000';
        }
        list<Opportunity> Opplist=Database.query(query);
        if(!Opplist.isempty()){
            for(Opportunity Opp:Opplist){
                OpptyWrpList.add(new OpptywrapperClass(opp));
            }
        }
        return OpptyWrpList;
    }
    
    @AuraEnabled
    public static boolean CheckProfileAccessForSixRec(){
        boolean  isAllowedToAddMoreThanSixOpp=false;
        system.debug('The Profile Id is '+userinfo.getProfileId());
        Profile p = [Select Name from Profile where Id =: userinfo.getProfileid()]; 
        String pname = p.name;
        system.debug('profile name is '+pname);
        isAllowedToAddMoreThanSixOpp = false; 
        system.debug('the is allowed variable is '+isAllowedToAddMoreThanSixOpp);
        List<string> allowedProfiles = new List<String>(); 
        PartnerProfileIAllowedAddAllOpp__mdt[] allowedProfileNames = [SELECT ProfileName__c FROM PartnerProfileIAllowedAddAllOpp__mdt];
        for(PartnerProfileIAllowedAddAllOpp__mdt ap :allowedProfileNames ){
            allowedProfiles.add(ap.ProfileName__c);
        }
        system.debug('the is allowed profiles are '+allowedProfiles);
        
        // loop to check if the user belongs to one of the allowed profiles. 
        for(String pn : allowedProfiles){
            if (pname == pn ){
                isAllowedToAddMoreThanSixOpp = true;
                system.debug('User belongs to the allowed profile '+pn);        
            }
        }
        
        return isAllowedToAddMoreThanSixOpp;      
     }
    
   //public List<Opportunity> oppList;
    @AuraEnabled
    public static boolean updateSelectedRecords(string selectedOpptyList){

        boolean result=false;
        system.debug('#### selectedOpptyList '+selectedOpptyList);
        String userids=UserInfo.getUserId();
        String role ='Partner';  
        String accessLevel ='Edit';
        set<Id> OpptyIdSet=new set<Id>();
        List<Visible_to_Partners__c> vpc = new  List<Visible_to_Partners__c>();
        Visible_to_Partners__c[] m_oppMembers = new List<Visible_to_Partners__c> ();
        List<OpptywrapperClass> selectedOpptyWrpList = (List<OpptywrapperClass>)JSON.deserialize(selectedOpptyList, List<OpptywrapperClass>.class);
        system.debug('### for loop starting '+selectedOpptyWrpList.isempty()+' '+selectedOpptyWrpList.size());
        
        for(OpptywrapperClass cw: selectedOpptyWrpList){
            
            OpptyIdSet.add(cw.Oppty.id);
            Visible_to_Partners__c tmSTP = new Visible_to_Partners__c(PartnerName__c=userids,Opportunity_Name__c=cw.Oppty.id,role__c=role,Opportunity_Access__c=accessLevel,ownerid=cw.Oppty.ownerid);   
            m_oppMembers.add(tmSTP);
            
        }
        
        try{
            vpc = [select id from Visible_to_Partners__c where PartnerName__c=:userids and Opportunity_Name__c in:OpptyIdSet];
            //system.debug('##### vpc '+vpc);
        }catch(Exception e){
            System.debug('***********'+e);
        }
        
        system.debug('##### vpc '+vpc);
        system.debug('###### m_oppMembers '+m_oppMembers);
        try{
            if(vpc.size()>0){
                delete vpc;
            }
            if(!m_oppMembers.isempty()){
                insert m_oppMembers;
                result=true;
            }
        }catch(DMLException e){
            System.debug('Exception in adding user to sales team: ' + e.getMessage());
        }
        system.debug('#### method end '+result);
        return result;    
        
    }
}

thanks in advance..