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
Daniel GageDaniel Gage 

Convert Result of List to String Value

I asked this once before, and I said I had it. But unforunately it wasn't properly bulkified, so I ended up with the field for every lead being populated with the same information, which shouldn't happen.
So below is my code... I'm trying to get the Value of the field
memberVerification.add(mv.TFA_Salesforce_ID__c);
so that I can update the lead with it. (it is querying the entity Member_Verification__c and matching on e-mail to retrieve what the value should be)

Any help would be appreciated. The way it is right now, it only ever grabs one value and tries to put it in every lead that is inserted during an import of leads)

List<Member_Verification__c> verified = [
        SELECT
            Id, Verification__c ,TFA_Salesforce_ID__C
        FROM
            Member_Verification__c
        WHERE
            Verification__c IN :leadVerification
    ];

Set<String> memberVerification = new Set<String>();
        for(Member_Verification__c mv:verified){
        memberVerification.add(mv.Verification__c);
        memberVerification.add(mv.TFA_Salesforce_ID__c);
            TFAMV = mv.TFA_Salesforce_ID__c;
    }

then later I reference it like this:

            {
                  
                lead.TFA_Salesforce_ID__c = TFAME ;
                lead.Verified__c = True;
                }
Best Answer chosen by Daniel Gage
Balaji BondarBalaji Bondar
Hi Daniel,

Here we go:
trigger UpdateVerifyLead on Lead (before insert, before update) {
	List<String> leadEmails = new List<String>();
	Map<String, Member_Verification__c > PrimaryEmailMemberVerificationMap = new  Map<String, Member_Verification__c >();
	
	for(Lead lead:Trigger.new){
		leadEmails.add(lead.Email);
	}
	for(Member_Verification__c member :[SELECT Id, Primary_Email__c,TFA_Salesforce_ID__C FROM Member_Verification__c WHERE Primary_Email__c != null and Primary_Email__c IN :leadEmails]){
		PrimaryEmailMemberVerificationMap.put(member.Primary_Email__c , member);	
	}
	
	for(Lead leadObj:Trigger.new){
		if(PrimaryEmailMemberVerificationMap.ContainsKey(leadObj.Email)){
			lead.TFA_Salesforce_ID__c = PrimaryEmailMemberVerificationMap.get(leadObj.Email).TFA_Salesforce_ID__C ;
			lead.Verified__c = True;
		}
	}
}

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.

All Answers

James LoghryJames Loghry
Hi Daniel,  the code you provided is fairly segmented.  Could you provide the entire Apex class and perhaps we could better assit you with your issue?
Daniel GageDaniel Gage

Right now it's a trigger.. not a class... I know it should be a class. But I'm fairly new to this and haven't quite worked out how to make this a class yet. The problem is that TFAME variable doesn't change for multiple uploads, so it's only checking it once, but I thought the way I had it would reference it for every loaded lead.

Anyway.. here it is. Any and all suggestions are very appreciated.

trigger UpdateVerifyLead on Lead (before insert, before update) {

List<String> leadEmails = new List<String>();
    for(Lead lead:Trigger.new){
        leadEmails.add(lead.Email);
    }

List<Member_Verification__c> email = [
        SELECT
            Id, Primary_Email__c,TFA_Salesforce_ID__C
        FROM
            Member_Verification__c
        WHERE
            Primary_Email__c != null and Primary_Email__c IN :leadEmails
    ];

string TFAME;

Set<String> memberEmails = new Set<String>();
        for(Member_Verification__c member:email){
        memberEmails.add(member.Primary_Email__c);
         memberEmails.add(member.TFA_Salesforce_ID__c);
        TFAME = member.TFA_Salesforce_ID__c;   
    }

      for (Lead lead:Trigger.new)
        {
            if(memberemails.contains(Lead.Email))
            {
 
                lead.TFA_Salesforce_ID__c = TFAME ;
                lead.Verified__c = True;
                }

Balaji BondarBalaji Bondar
Hi Daniel,

Here we go:
trigger UpdateVerifyLead on Lead (before insert, before update) {
	List<String> leadEmails = new List<String>();
	Map<String, Member_Verification__c > PrimaryEmailMemberVerificationMap = new  Map<String, Member_Verification__c >();
	
	for(Lead lead:Trigger.new){
		leadEmails.add(lead.Email);
	}
	for(Member_Verification__c member :[SELECT Id, Primary_Email__c,TFA_Salesforce_ID__C FROM Member_Verification__c WHERE Primary_Email__c != null and Primary_Email__c IN :leadEmails]){
		PrimaryEmailMemberVerificationMap.put(member.Primary_Email__c , member);	
	}
	
	for(Lead leadObj:Trigger.new){
		if(PrimaryEmailMemberVerificationMap.ContainsKey(leadObj.Email)){
			lead.TFA_Salesforce_ID__c = PrimaryEmailMemberVerificationMap.get(leadObj.Email).TFA_Salesforce_ID__C ;
			lead.Verified__c = True;
		}
	}
}

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
This was selected as the best answer
Daniel GageDaniel Gage
I see what you did there. Let me test it out. If it works, and I don't have any other questions I will absolutely mark it as the solution. Thank you for your time.
Daniel GageDaniel Gage
Okay, so I've had time to look at this. And I see that the Query has been put in the For Loop. I thought this was a bad habit, as anything over 100 queries and it will fail. Correct?

Or am I missing something?
Daniel GageDaniel Gage
Balaji,

First, thank you for your help. I am new to SF developing (I'm a C# guy).. and any help is greatly appreciated.

I just tested this code out, and it's doing the same thing my original code was doing. It seems that it is only grabbing the  TFA SF ID from the first lead object in the list and populating it to all in the Bulk Import process. This code works fine on manual creation. But not on imports of multiple leads.
Daniel GageDaniel Gage

Okay... So it might not be the code that isn't working. Because it never actually updated. When I finally figured out why it wasn't saving, I get this error when trying to save the above provided code.

 

Error: Compile Error: Expression cannot be assigned at line -1 column -1

Daniel GageDaniel Gage
I have narrowed it down to this part of the code:

      for(Lead leadObj:Trigger.new){
          if(PrimaryEmailMemberVerificationMap.ContainsKey(leadObj.Email)){
              lead.TFA_Salesforce_ID__c = PrimaryEmailMemberVerificationMap.get(leadObj.Email).TFA_Salesforce_ID__C ;
              lead.Verified__c = True;
          }
      }

But I don't see what could be wrong about it.
Daniel GageDaniel Gage
Was a typo in the lead.Verified__c  should have been leadobj.... but other than that. the code was perfect. Thank you.