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
SId.New devSId.New dev 

Code coverage problem & Error

public class Contacterrorclass {

public PageReference AssignPermissionforcontact() {
        return null;
    }

public list<user> u{get;set;}
id contactid;

public Contacterrorclass()
{
 contactid=ApexPages.CurrentPage().getParameters().get('id');
 }
   public PageReference AssignPermissionforcontacts() {
       Contact theContact= new contact();
       theContact=[Select accountid,id from contact where id=:contactid];
       IF(theContact.accountid!=null){
List<AccountShare> newAccountShares = new List<AccountShare>();
AccountShare thisAccountShare = new AccountShare(); //a new empty AccountShare object
        
        thisAccountShare.userorgroupid = UserInfo.getUserId();
      thisAccountShare.accountid = theContact.accountid;
        thisAccountShare.accountaccesslevel = 'edit';
        thisAccountShare.OpportunityAccessLevel = 'None';
        thisAccountShare.CaseAccessLevel = 'None';
       // thisAccountShare.ContactAccessLevel = 'readonly';
newAccountShares.add(thisAccountShare);
insert newAccountShares;
pagereference p1=new pagereference('/'+contactid);

return p1;
}else{
apexpages.message acctmsg=new apexpages.message(apexpages.severity.info,'Account NAME MISSING');
                               
 apexpages.addmessage(acctmsg);
return null;
}
 }
}

 

TEST CLASS(75%) BUT ERROR_System.QueryException: List has no rows for assignment to SObject
Class.contactERRORTestClass.contactERRORTESTCLASS: line 17, column 1

@istest(seealldata=true)
private class contactERRORTestClass {
static testMethod void contactERRORTESTCLASS() {
id contactid;
   
List<User> users = [SELECT Id FROM User WHERE IsActive = true limit 1];
test.startTest();
account acc= new account();
acc.Name='s';

insert acc;




Contact theContact= new contact();
theContact=[Select accountid,id from contact where id=:contactid];
theContact.FirstName='s';
theContact.LastName='d';
theContact.accountid=acc.id;
insert theContact;

 ApexPages.CurrentPage().getParameters().put('id',theContact.id);

   Contacterrorclass a= new Contacterrorclass();
  
//a.AssignPermissionforcontacts();
a.AssignPermissionforcontact() ;
test.stopTest();   

    }
    }

 

Devendra@SFDCDevendra@SFDC

HI,

 

The error is in the below line, as contactId is null. This will not return any row.

 

I think, the below line is not significant as you are inserting a newcontact. Instead of doing a query you can simlply insert account and contact record.

 

theContact=[Select accountid,id from contact where id=:contactid];

 

SId.New devSId.New dev
@istest(seealldata=true)
private class contactERRORTestClass {
static testMethod void contactERRORTESTCLASS() {


   
List<User> users = [SELECT Id FROM User WHERE IsActive = true limit 1];
test.startTest();
account acc= new account();
acc.Name='s';
insert acc;
Contact theContact= new contact();
//theContact=[Select accountid,id from contact where id=:contactid];
theContact.FirstName='s';
theContact.LastName='d';
theContact.accountid=acc.id;
insert theContact;

ApexPages.CurrentPage().getParameters().put('id',theContact.id);

Contacterrorclass a= new Contacterrorclass ();
 //a.AssignPermissionforcontacts();
a.AssignPermissionforcontacts() ;
test.stopTest(); 
  
    }
    }

 

Not  yet been solved,,,,!!!! not getting covered..when i remove query the following error

System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Class.Contacterrorclass.AssignPermissionforcontacts: line 28, column 1
Class.contactERRORTestClass.contactERRORTESTCLASS: line 33, column 1