1

I'm practicing on creating a Trigger (still many things to learn...)

I would like to know on why am I getting a blank value on Text__c, the record's email address is an email address of an exisiting User.

See code snippet below.

trigger AutoChangeOwner on Contact (before insert, before update) {

Set<String> conEmail = new set<String>();
Map<String, Id> UsrMap = new map<String, Id>();

for(Contact con : trigger.new ){
    conEmail.add(con.Email);
    system.debug('conemail' + conEmail);
}

List<User> userEmail = [SELECT Id, Name, Email FROM User WHERE Email != null];
for(User u : userEmail){
    if(conemail.contains(u.email)){
        UsrMap.put(u.Email, u.Id); 
        system.debug('useremail' + userEmail);
    }
}

for(Contact c : trigger.new){
    c.Text__c = UsrMap.get(c.Email);
}

}

Any kind of explanation or ways to enhance the code is much appreciated!

3 Answers 3

0

As the comments point out (I can't comment so I cant explain there) You're comparing a String (u.email) and a Set (conemail) these to things cannot be equal as one is a single string and one is a Set of strings. You just need to check if the String is in the Set. You can do this using contains(value) so if you replace if(u.email == conemail) with if(conemail.contains(u.email)) it should solve your problem

6
  • thank you for your response. I've update my IF statement and even tried to comment the IF statement and still the field is not updating. I also tried to generate a debug log and I'm not able to see traces of useremail Nov 4, 2019 at 0:35
  • So even without an If statement the system.debug('useremail' + useremail); line is not executing? You could add a check to see the size of your user email list before entering the loop just as a sanity check. Also try checking the emails are exactly the same eg no capitals as this may cause contains() to fail
    – KDH
    Nov 4, 2019 at 0:43
  • I'm getting null assignment to Text__c Debug log results: |VARIABLE_ASSIGNMENT|[21]|this.Text__c|null|0x2238376b Nov 4, 2019 at 0:45
  • Yes the null assignment will happen if a contact in the trigger does not have a corresponding user in the usr map collection so check how many users are in that map. The more system.debugs you have the more you'll know about how the code is running so try logging what is going on with the individual users and contact records
    – KDH
    Nov 4, 2019 at 0:47
  • this.userEmail has 35 results because I removed the IF statement. Nov 4, 2019 at 0:51
1

You should use system.debug('conemail' + conEmail); after the first loop to see if you're populating the set correctly.

Then check if the list userEmail has been populated correctly with system.debug('useremail' + userEmail);

5
  • I'm getting the correct output from conemail. In useremail I wasn't able to see any traces from the debug log. Any ideas? Oct 29, 2019 at 6:03
  • I don't think you should use In: ConEmail as a filter here, you should filter your SOQL based on an id set. You can get users with non blank email fields with SQOL and loop inside collection to fill the UsrMap instead.
    – Atlas Can
    Oct 29, 2019 at 6:08
  • See my code update, how can I compare the u.email and conemail I'm having an error: Comparison arguments must be compatible types: String, Set<String> I'm getting the logic but I don't know how to convert it to code... Oct 29, 2019 at 6:19
  • one of them is a set, one of them is a string value, you need to iterate over the set and check if the value of the member of the set equals your u.email.
    – Atlas Can
    Oct 29, 2019 at 6:21
  • Can you please provide sample on how should I do it? I really appreciate your help here! Oct 29, 2019 at 6:58
0

Your if-statement if(u.email == conemail) will always return false, so your UsrMap will always stay empty.

And doing the .get() on a map with an non-existing key will always return you null.

I would recommend changing that comparison to:

if(conemail.contains(u.email)) {
    // fill in your map here
}
2
  • thank you for your response. I've update my IF statement and even tried to comment the IF statement and still the field is not updating. I also tried to generate a debug log and I'm not able to see traces of useremail Nov 4, 2019 at 0:34
  • To add up I'm getting null assignment to Text__c |VARIABLE_ASSIGNMENT|[21]|this.Text__c|null|0x2238376b Nov 4, 2019 at 0:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .