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
sunnysfdcdvpsunnysfdcdvp 

trigger that compare old value with new value

HI Everyone

 

plz check  my requirement

 

In contact two fields are there one is mobile and another is phone 

 

i need to avoid duplicate values in the both fields

 

ex :let us take in first record i enter moblie=909090 and  phone=808080

 

when i am entering second record mobile=808080 and phone=909090(here i want through an error that moile number or phone  is already exhists with another record)

 

thanks in advance

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

Try this trigger. It will work for you.

trigger trgCheckContactNumbers on Contact (before insert, before update) {
    Set<String> setNo = new Set<String>();
    Set<ID> setCurrentId = new Set<ID>();
    for(Contact con:Trigger.New){
        if(con.MobilePhone <> null){
            setNo.add(con.MobilePhone);
        }
        if(con.Phone <> null){
            setNo.add(con.Phone);
        }
        if(Trigger.isUpdate){
            setCurrentId.add(con.Id);
        }
    }
    List<Contact> lstContact = new List<Contact>();
    if(setNo.size()>0){
        if(Trigger.IsUpdate){
            lstContact = [Select Id, MobilePhone, Phone From contact Where Id not In:setCurrentId And (MobilePhone In:setNo OR Phone In:setNo)];
        }
        else{
            lstContact = [Select Id, MobilePhone, Phone From contact Where (MobilePhone In:setNo OR Phone In:setNo)];
        }
        Map<String, Contact> mapMobile = new Map<String, Contact>();
        Map<String, Contact> mapPhone = new Map<String, Contact>();
        if(lstContact.size()>0){
            for(Contact con:lstContact){
                if(con.MobilePhone <> null)
                    mapMobile.put(con.MobilePhone, con);
                if(con.Phone <> null)
                    mapPhone.put(con.Phone, con);
            }
            for(Contact con:Trigger.New){
                if((mapMobile.size()>0 && mapMobile.containsKey(con.MobilePhone)==true) || 
                    (mapPhone.size()>0 && mapPhone.containsKey(con.Phone)==true)){
                    con.addError('Mobile number or Phone number already exists.');
                }
            }
        }
    }
}

 

All Answers

Dhaval PanchalDhaval Panchal

Try this trigger. It will work for you.

trigger trgCheckContactNumbers on Contact (before insert, before update) {
    Set<String> setNo = new Set<String>();
    Set<ID> setCurrentId = new Set<ID>();
    for(Contact con:Trigger.New){
        if(con.MobilePhone <> null){
            setNo.add(con.MobilePhone);
        }
        if(con.Phone <> null){
            setNo.add(con.Phone);
        }
        if(Trigger.isUpdate){
            setCurrentId.add(con.Id);
        }
    }
    List<Contact> lstContact = new List<Contact>();
    if(setNo.size()>0){
        if(Trigger.IsUpdate){
            lstContact = [Select Id, MobilePhone, Phone From contact Where Id not In:setCurrentId And (MobilePhone In:setNo OR Phone In:setNo)];
        }
        else{
            lstContact = [Select Id, MobilePhone, Phone From contact Where (MobilePhone In:setNo OR Phone In:setNo)];
        }
        Map<String, Contact> mapMobile = new Map<String, Contact>();
        Map<String, Contact> mapPhone = new Map<String, Contact>();
        if(lstContact.size()>0){
            for(Contact con:lstContact){
                if(con.MobilePhone <> null)
                    mapMobile.put(con.MobilePhone, con);
                if(con.Phone <> null)
                    mapPhone.put(con.Phone, con);
            }
            for(Contact con:Trigger.New){
                if((mapMobile.size()>0 && mapMobile.containsKey(con.MobilePhone)==true) || 
                    (mapPhone.size()>0 && mapPhone.containsKey(con.Phone)==true)){
                    con.addError('Mobile number or Phone number already exists.');
                }
            }
        }
    }
}

 

This was selected as the best answer
Preya VaishnaviPreya Vaishnavi

Hi,

 

I think you can make both the fields as unique so that the same phone number and mobile number will not be entered again.

 

If you found this answer as a solution, Please give kudos by clicking on the star icon and mark it as a solution

 

Thanks!!!!

sunnysfdcdvpsunnysfdcdvp

U Did a great job its working 

 

Thanks master!!!!