• SId.New dev
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 3
    Replies
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

 

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();   

    }
    }

 

Hi all.

concatinate_name_and_phone__c,concatinate_name_and_email__c are formula fields which holds (fullname+email)&(fullname+phone)
i need is, i need to show error when ever (fullname+email) or (fullname+phone) exists.Im not getting where iam missing.

Is formula fields works before insert????

This trigger shows an error message when email+name or name+phone already exists and give a old record link to open that record.
 This trigger shows an error message when email already exists and give a old record link to open that record. 

trigger TriggerOnLead on Lead (before insert) {
 
    Map<String, Lead> mapLead1 = new Map<String, Lead>();
    SYSTEM.DEBUG('!!!!!!!!!!mapLead1!!!!!!!!!!!'+mapLead1);
    Map<String, Lead> mapLead2 = new Map<String, Lead>();
    SYSTEM.DEBUG('+++++++++++mapLead2 +++++++++++++++'+mapLead2 );

    for (Lead lead : System.Trigger.new) {
          mapLead1.put(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM(), lead);
          mapLead2.put(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM(), lead);
    }
     list<lead> lead1= new list<lead>([SELECT Id,Name,Email,Phone,concatinate_name_and_email__c FROM Lead WHERE concatinate_name_and_email__c  IN :mapLead1.KeySet()]);
   
    list<lead> lead2=[SELECT Id,Name,Email,Phone,concatinate_name_and_phone__c FROM Lead WHERE concatinate_name_and_phone__c IN :mapLead2.KeySet()];
    for (Lead lead4 : System.Trigger.new) {
      if(lead4.Any_way_save_record__c!=true)
      {
  
     if (lead.concatinate_name_and_email__c != null) 
    {
     for (Lead lead :lead1){
      Lead newLead = mapLead1.get(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
       SYSTEM.DEBUG('*************mapLead1************************'+mapLead1);
         SYSTEM.DEBUG('@@@@@@@@@newLead@@@@@@@@@@@@@@@'+newLead);
        newLead.addError('see lead<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');
  
    }}else  if (lead.concatinate_name_and_phone__c != null) 
    for (Lead lead :lead2 ) {
      Lead newLead1 = mapLead2.get(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM());
      SYSTEM.DEBUG('%%%%%%%%%%%%newLead1%%%%%%%%%%%%%%%%%%'+newLead1);
      
        
       newLead1.addError('see lead<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');

  
    }
    
   }
}
}

 

 it is saving with out giving any error(when phone&Name  is given same

 

 

trigger TriggerOnLead on Lead (before insert) {
 
    Map<String, Lead> mapLead1 = new Map<String, Lead>();
    SYSTEM.DEBUG('!!!!!!!!!!mapLead1!!!!!!!!!!!'+mapLead1);
    Map<String, Lead> mapLead2 = new Map<String, Lead>();
    SYSTEM.DEBUG('+++++++++++mapLead2 +++++++++++++++'+mapLead2 );

    for (Lead lead : System.Trigger.new) {
          mapLead1.put(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM(), lead);
          mapLead2.put(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM(), lead);
    }
     list<lead> lead1=[SELECT Id,Name,Email,Phone,concatinate_name_and_email__c FROM Lead WHERE concatinate_name_and_email__c  IN :mapLead1.KeySet()];
   
    list<lead> lead2=[SELECT Id,Name,Email,Phone,concatinate_name_and_phone__c FROM Lead WHERE concatinate_name_and_phone__c IN :mapLead2.KeySet()];
    for (Lead lead4 : System.Trigger.new) {
      if(lead4.Any_way_save_record__c!=true)
      {
  
    if(lead1.size()>0)               /*at this line error is coming. */
    {
     for (Lead lead :lead1){
      Lead newLead = mapLead1.get(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
       SYSTEM.DEBUG('*************mapLead1************************'+mapLead1);
         SYSTEM.DEBUG('@@@@@@@@@newLead@@@@@@@@@@@@@@@'+newLead);
        newLead.addError('already exists<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');
    }}else if(lead2.size()>0){
    for (Lead lead :lead2 ) {
      Lead newLead1 = mapLead2.get(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM());
      SYSTEM.DEBUG('%%%%%%%%%%%%newLead1%%%%%%%%%%%%%%%%%%'+newLead1);
      
        
       newLead1.addError('already existse<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');

  
    }
    
   }
}
}
}

 TR: Sandbox: Developer script exception : TriggerOnLead : TriggerOnLead: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.TriggerOnLead: line 22, column 1

Hi all.Please let me know if any one worked on this.

 

OWD is private for account and contact. so no user connot see records now, now i want to show contact records to users when they entered existing email. i prevoiusly worked on lead which is woking. but help me now to do the same on contacts.

 

public class contacterror {

    public PageReference AssignPermissionforcontact1() {
        return null;
    }

public list<user> u{get;set;}
id contactid;
public contacterror ()
{
 contactid=ApexPages.CurrentPage().getParameters().get('id');
}

Public pagereference AssignPermissionforcontact(){

system.debug('GGGG'+UserInfo.getUserId());

ContactShare cs = new ContactShare();
     //   cs.ContactAccessLevel = 'Edit';     ( here getting errror as "=")
     //   cs.ContactId = contactId;
      //  cs.UserOrGroupId =  UserInfo.getUserId();

//Database.SaveResult contactInsertResults = Database.insert(cs, false);


pagereference p=new pagereference('/'+contactid);

return p;

}
}


VF page:------------------


vf page:------

<apex:page controller="Contacterrorclass" action="{!AssignPermissionforcontact}">
<apex:form >

</apex:form>
</apex:page>
-----------------------------------------------------------
Trigger:-(checks the dups, and gives the link to open the existing record)


trigger TriggerOncontact on contact (before insert) {
 
    Map<String, contact> mapcontact1 = new Map<String, contact>();
    Map<String, contact> mapcontact2 = new Map<String, contact>();

    for (contact contact : System.Trigger.new) {
          mapcontact1.put(contact.concatinate_name_and_email__c.TOLOWERCASE().TRIM(), contact);
          mapcontact2.put(contact.concatinate_name_and_phone__c.TOLOWERCASE().TRIM(), contact);
    }
     list<contact>contact1=[SELECT Id,Name,Email,Phone,concatinate_name_and_email__c FROM contact WHERE concatinate_name_and_email__c  IN :mapcontact1.KeySet()];
     list<contact> contact2=[SELECT Id,Name,Email,Phone,concatinate_name_and_phone__c FROM contact WHERE concatinate_name_and_phone__c IN :mapcontact2.KeySet()];
    for (contact con4 : System.Trigger.new) {
      if(con4.Any_way_save_record__c!=true)
      {
  
    if(contact1.size()>0)
    {
     for (contact contact :contact1){
     contact newcontact = mapcontact1.get(contact.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
      
         
     newcontact.addError('Les contacts de cette personne existent dans Salesforce. Voir le contact :<a style=color:GREEN href=/apex/leaderror?id='+contact.ID+'>'+  contact.name + '</a>');
    }}else if(contact2.size()>0){
    for (contact Contact :contact2 ) {
      contact newcontact1 = mapcontact2.get(contact.concatinate_name_and_phone__c.TOLOWERCASE().TRIM());
      newcontact1.addError('contact already exists see this:<a style=color:GREEN href=/apex/leaderror?id='+contact.ID+'>'+  contact.name + '</a>');

  
    }
    
   }
}
}
}

 I want to show the contact when he clicks on link , (if it is not possible i need to show his related account).

KUDOS in Advance

 

Thanks

Sid.

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

 

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();   

    }
    }

 

 

trigger TriggerOnLead on Lead (before insert) {
 
    Map<String, Lead> mapLead1 = new Map<String, Lead>();
    SYSTEM.DEBUG('!!!!!!!!!!mapLead1!!!!!!!!!!!'+mapLead1);
    Map<String, Lead> mapLead2 = new Map<String, Lead>();
    SYSTEM.DEBUG('+++++++++++mapLead2 +++++++++++++++'+mapLead2 );

    for (Lead lead : System.Trigger.new) {
          mapLead1.put(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM(), lead);
          mapLead2.put(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM(), lead);
    }
     list<lead> lead1=[SELECT Id,Name,Email,Phone,concatinate_name_and_email__c FROM Lead WHERE concatinate_name_and_email__c  IN :mapLead1.KeySet()];
   
    list<lead> lead2=[SELECT Id,Name,Email,Phone,concatinate_name_and_phone__c FROM Lead WHERE concatinate_name_and_phone__c IN :mapLead2.KeySet()];
    for (Lead lead4 : System.Trigger.new) {
      if(lead4.Any_way_save_record__c!=true)
      {
  
    if(lead1.size()>0)               /*at this line error is coming. */
    {
     for (Lead lead :lead1){
      Lead newLead = mapLead1.get(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
       SYSTEM.DEBUG('*************mapLead1************************'+mapLead1);
         SYSTEM.DEBUG('@@@@@@@@@newLead@@@@@@@@@@@@@@@'+newLead);
        newLead.addError('already exists<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');
    }}else if(lead2.size()>0){
    for (Lead lead :lead2 ) {
      Lead newLead1 = mapLead2.get(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM());
      SYSTEM.DEBUG('%%%%%%%%%%%%newLead1%%%%%%%%%%%%%%%%%%'+newLead1);
      
        
       newLead1.addError('already existse<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');

  
    }
    
   }
}
}
}

 TR: Sandbox: Developer script exception : TriggerOnLead : TriggerOnLead: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.TriggerOnLead: line 22, column 1