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
Shahab KhanShahab Khan 

Trigger.new not working on before update

Hello,

I have write trigger on Contact before insert and before update for avoiding duplicate entry of Email.

Here is my code
trigger DuplicateEmail on Contact (before insert, before update)
{
	Contact val_con = trigger.new[0];
    Contact prev_contact_rec = null;
    if(val_con.Email != null && val_con.Email != '')
    {
        try
        {
            if (trigger.isInsert)
            {
                prev_contact_rec = [select Email from Contact where Email=:val_con.Email limit 1];
                system.debug('----->>> Email Before Insert New: ' + val_con.Email);
            }
            else if (trigger.isUpdate)
            {
                prev_contact_rec = [select Email from Contact where Email=:val_con.Email and Id !=:val_con.Id limit 1];
                system.debug('----->>> Email Before Update New: ' + val_con.Email);
                system.debug('----->>> Id Before Update New: ' + val_con.Id);
            }
            
            if(prev_contact_rec.Email != null && prev_contact_rec.Email != '')
            {
                Trigger.new[0].adderror('Email you provided has already exists');
            }
        }
        catch (exception ex)
        {
            System.debug('Error occurd: ' + ex);
        }
        
    }
}

It is working fine when inserting new contact. But when i update the email address of existing contact then the trigger gets old value of email field instead of new value for example old value of email was "foo@yahoo.com" i have updated it to "foo123@yahoo.com" instead of assigning "foo123@yahoo.com" value to val_con object it assigns "foo@yahoo.com" to it in case of before update trigger event.
Can some body help me how to fix it.

Thanks,
Shahab
shiv@SFDCshiv@SFDC
Hi Shahab,

I just tested your code in my org and it's working fine. Can you please tell me the staps to generate same error in my org.

Thanks,
Shiv
fundooMentalfundooMental
Hi Shahab,

Can you be a little more clear with your question. Do you mean when you update the field, the new value of the email is not showing in the contact records? What exactly is the error?
Shahab KhanShahab Khan
Thanks for your quick reply.
But you didn't get my point.
Let me explain you again.
I have a contact with email "foo@yahoo.com" already added.
Now i want to change the email for that contact to "foo123@yahoo.com", in edit contact i replace the email "foo@yahoo.com" with "foo123@yahoo.com" as this is update and trigger event before update will fire and the query should be like "select Email from Contact where Email='foo123@yahoo.com' and Id !=:val_con.Id limit 1" but instead of this query becomes "select Email from Contact where Email='foo@yahoo.com' and Id !=:val_con.Id limit 1" due to which i got wrong result.
Hope you got my point.

Thanks,
Shahab
fundooMentalfundooMental

I am still not clear with your question. Can you just tell me one thing: Is the new email getting saved in the contact record or not? Where exactly you are checking that the email is not changing?

Second thing: During update the Id of the record never changes so why you have given condition and Id !=:val_con.Id ?
Id will remain same for ever

 

shiv@SFDCshiv@SFDC
Shahab I have just follow these steps and getting following result.

1. Create a new contact with last name 'XYZ' and email address "foo@yahoo.com" . Record is saved.
2. Now I went to same record and and modified the email address to "foo123@yahoo.com" and click on save. And record is saved with this new email address. I think this is only we are expecting.
Shahab KhanShahab Khan
Shiv i have the scenario like that i had duplicate email records for contact before adding this trigger.
Now i hve added this trigger and i am trying to update the email field of duplicated email records and its not allowing me it says "Email you provided has already exists"
Simply delete this trigger from your org then add two contact records with same email after that add this trigger and try to update the email field of any one of recods which you had added with same email.
Shahab KhanShahab Khan
Please somebody reply on my issue how can i fix it.

Thanks,
Shahab
fundooMentalfundooMental

Shahab,

We would definetly reply with proper answer but before that you need to clear few things:

1. When you change the email of the contact, is it getting changed or not? I mean are you able to see the new email in the contact record in the salesforce UI
2. Where exactly you are checking that the email is not getting changed?
3. Why have you given the condition "and Id !=:val_con.Id " . As Id remains same in the update and it never changes once a record is created.

Please answer the above questions clearly and clarify your question properly to get a proper answer for your query.

Thanks
 

fundooMentalfundooMental

Moreover your query

prev_contact_rec = [select Email from Contact where Email=:val_con.Email and Id !=:val_con.Id limit 1];

Would return no record (asumming you already does not have any contact record with the new email), because in the clause

where Email=:val_con.Email ,  val_con.Email would have the new value of the email (which you are going to update, as val_con is from trigger.new) and if there is no such email already in the contact record then where Email=:val_con.Email would return no records.
ALSO
Why have you added the condition and Id !=:val_con.Id. It means the record that you are trying to select should have different Id than the record that you are trying to update. I don't understand why you have given this condition? This condition is not required at all, since you are interested in checking (before updating the email) if the email is same or not then why do you want to give a conditon which says "and the record Id is not same".

 

Shahab KhanShahab Khan
Thank you very much for your reply.

I have added that condition "and Id !=:val_con.Id" because it should not compare email with its own record so that we can update other fields of contact record if i remove that condition then this will not allow to update anything for existing contact record.
fundooMentalfundooMental
You said "if i remove that condition then this will not allow to update anything for existing contact record" How?
You said you just want to update email and if email already exist then it should not allow that's it, then why you need such condition. Anyways you must have something else in your mind.

BTW coming to your query again. You trying to get the previous contact record and you are comparing where Email=:val_con.Email . I will say this again: val_con.Email is would have the new value of the email as val_con is from trigger.new. If you want to compare then use trigger.oldmap and then try the comparison.
You have still not clearly specified what exactly you want to do and want to check and where?
fundooMentalfundooMental
And ID never changes. So if your thiniking that your trigger.new would have different values for contact Id than the exisiting Ids, during the update then you are wrong. Hence the condition and Id !=:val_con.Id is not required at all (looking at your requirement)
fundooMentalfundooMental
Is your problem resolved? If yes then please mark the best answer out of the given answers if they helped you solve your query or let us know if is not solved yet