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 for class

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 

@istest(seealldata=true)
public class contactERRORTestClass {
static testMethod void contactERRORTESTCLASS() {
//try{

   
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;



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



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

Contacterrorclass a= new Contacterrorclass ();
a.AssignPermissionforcontacts();
//a.AssignPermissionforcontact() ;
test.stopTest(); 
//} catch(DmlException e) {
 //   System.debug('The following exception has occurred: ' + e);
//}  
    }
    }

 ERROR:System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Les contacts de cette personne existent dans Salesforce. Voir le contact :<a style=color:GREEN href=/apex/contacterror?id=003K000000YmEioIAF>s d</a>: []

 

 

This error coming from trigger. which is already covered.

trigger TriggerOncontact on contact(before insert) {
 
   Set<String> nameAndEmail = new Set<String>();
   Set<String> nameAndPhone = new Set<String>();

    for (contact contact : Trigger.new) {
          nameAndEmail.add(contact.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
          nameAndPhone.add(contact.concatinate_name_and_phone__c.TOLOWERCASE().TRIM());
    }
    system.debug(nameAndEmail);
    system.debug(nameAndPhone);
    list<contact> contacts= [SELECT Id,Name,Email,Phone,concatinate_name_and_email__c,concatinate_name_and_phone__c FROM contact WHERE concatinate_name_and_email__c  IN :nameAndEmail or concatinate_name_and_phone__c in :nameAndPhone];
    system.debug(contacts);
    if(!contacts.isEmpty())
    {
        for(contact newcontact: trigger.new)
        {
        if(newcontact.Any_way_save_record__c!=true){
            for(contact l: contacts)
            {
                if(l.concatinate_name_and_email__c != null && l.concatinate_name_and_email__c == newcontact.concatinate_name_and_email__c)
                { 
                   // newLead.addError('see  the lead already exists with that email and phone:--'+'\n'+l.Name);
 newcontact.addError('Les contacts de cette personne existent dans Salesforce. Voir le contact :<a style=color:GREEN href=/apex/contacterror?id='+l.id+'>'+ l.name + '</a>');
                }
                else if(l.concatinate_name_and_phone__c != null && l.concatinate_name_and_phone__c == newcontact.concatinate_name_and_phone__c)
                 {
 newcontact.addError('Les contacts de cette personne existent dans Salesforce. Voir le contact:<a style=color:GREEN href=/apex/contacterror?id='+l.id+'>'+ l.name + '</a>');       
                 }
            }
           }
        }
     }
 }

 please let me know , how to achieve 100%

 

Kudos in Advance

 

Rajesh SriramuluRajesh Sriramulu

HI

 

Insert concatinate_name_and_email__c 

 

 contact field as null in test class.

 

Regards

Rajesh..

SId.New devSId.New dev

Thanq for your concern dear,,!!!

 

Are you sure?Bcoz that is Formula field,

Rajesh SriramuluRajesh Sriramulu

Hi

 

U not need to satisfy the below condition, if u satisfy means the error will come which is in trigger.

 

l.concatinate_name_and_phone__c != null && l.concatinate_name_and_phone__c == newcontact.concatinate_name_and_phone__c

 

 

Regards,

Rajesh.