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
Randy HenryRandy Henry 

User Name isolated in an array and posted to Lead

Hello. I'm posting here as I used code from here as my starter for one of the standard zip code issues. In my instance, the sales and customer service teams have requested a field where the agent assigned to a particular zip code is returned. These assignments can differ based on line of business, but they are supposed to be unique.  It cannot be a straight lead assignment rule, as the rules are ... porous. Here is where I am at:
  1. Custom object created. It has 5 fields: zip code, assigned agent, line of business, and 2 fields used for duplicate checking.
  2. Workflow that populates one of fields from another formula field. The workflow target field is a text(unique), so it causes a failure as planned if the zip code and line of business combination is already found.
  3. All of my sample data was uploaded using Dataloader.io. This was necessary over the standard import tool because the Assigned Agent field is a lookup to User.
  4. Apex target field was created on Lead (Agent for Zip Code). Rather than do a standard lead assignment rule, I will need to place in the suggestion of who the lead owner should be.
  5. The last step, the Apex code, has been started. That is where I am currently stuck.
When I add a record without a zip code, it works fine as it escapes out of the code as designed. When I add a record with an invalid zip code (not assigned), same story. When I hit the positive test, I'm encountering an error. Basically it is trying to insert the entire array. I cannot seem to figure out why. I know it's just something I'm doing wrong, but I can't seem to nail it down. I have had the code through about a hundred permutations (no joke) and have dug around a lot of Salesforce support sites. No luck so far.

If I run the original code found here (https://developer.salesforce.com/forums/ForumsMain?id=906F0000000Af9nIAC), it pastes in the ID instead of the name. I need the literal name. My code (below) errors out as follows: "Zip_Code_Agent_Populate: data changed by trigger for field Agent for Zip Code: data value too large: (User:{Name="Agent Name", Id=00540000002oOO3AAM}) (max length=40)". FYI, I replaced "Agent Name" here, but it is the name I'm looking for when running the code.

Help in resolving this array woe would be greatly appreciated. The code is below.

trigger Zip_Code_Agent_Populate on Lead (before insert) {
  List<Lead> leadsToUpdate = new List<Lead>();
    for (Lead lead : Trigger.new)
    {   
      if (lead.PostalCode != NULL)
      {
          // Find the sales rep for the current zip code
          List <Zip_Code__c> zip = [select Assigned_Agent__c from Zip_Code__c
                                      where Zip_Code__c = :lead.PostalCode
                                      and Line_of_Business__c = :lead.Lead_Line_of_Business__c
                                      limit 1];    
          // if you found one
          if (zip.size() > 0)
          {  
              //find the name literal from the user ID
              String agentName = '' + [SELECT Name FROM User WHERE Id =: zip[0].Assigned_Agent__c limit 1];
              //assign the lead owner suggestion to the agent for zip code field
              lead.Agent_for_Zip_Code__c = agentName;
              //lead.Agent_for_Zip_Code__c = zip[0].Assigned_Agent__c; -- this code returns only the ID
              leadsToUpdate.add(lead);
          }
       }
Best Answer chosen by Randy Henry
Bhanu MaheshBhanu Mahesh
Hi Henry,

You need to assign only Name of the user. But in your code you are assigning entire user instance
in the below line
String agentName = '' + [SELECT Name FROM User WHERE Id =: zip[0].Assigned_Agent__c limit 1];

Change this line to 
String agentName = '' + [SELECT Name FROM User WHERE Id =: zip[0].Assigned_Agent__c limit 1].Name;

this will give you the user name and assign it to agentName

And also your trigger is not bulkified. SOQL query is inside for loop which is not a best practice to writer triggers.

As there are governor limits on number of SOQL queries per transaction. It may fail if you insert more records.

Refer below link for best practices in writing apex
https://developer.salesforce.com/page/Apex_Code_Best_Practices

Mark it as "SOLVED" if your query is Answered

Regards,
Bhanu Mahesh

All Answers

Bhanu MaheshBhanu Mahesh
Hi Henry,

You need to assign only Name of the user. But in your code you are assigning entire user instance
in the below line
String agentName = '' + [SELECT Name FROM User WHERE Id =: zip[0].Assigned_Agent__c limit 1];

Change this line to 
String agentName = '' + [SELECT Name FROM User WHERE Id =: zip[0].Assigned_Agent__c limit 1].Name;

this will give you the user name and assign it to agentName

And also your trigger is not bulkified. SOQL query is inside for loop which is not a best practice to writer triggers.

As there are governor limits on number of SOQL queries per transaction. It may fail if you insert more records.

Refer below link for best practices in writing apex
https://developer.salesforce.com/page/Apex_Code_Best_Practices

Mark it as "SOLVED" if your query is Answered

Regards,
Bhanu Mahesh
This was selected as the best answer
Randy HenryRandy Henry
Thanks @Bhanu Mahesh. I guess I need to check on the email set for replies as I didn't see this response until I came back to post my own solution. As you rightly said, I was assigning the entire list return to a string. Instead of the above solution, I created another list using the User type and then posted just the <list>.Name to the field.

Here is my solution just for reference. Thanks again and I will still mark yours as the best solution. :)

trigger Zip_Code_Agent_Populate on Lead (before insert) 
{
  List<Lead> leadsToUpdate = new List<Lead>();
    for (Lead lead : Trigger.new)
    {    
      if (lead.PostalCode != NULL)
      {
          // Find the sales rep for the current zip code
          List <Zip_Code__c> zip = [select Assigned_Agent__c from Zip_Code__c 
                                      where Zip_Code__c = :lead.PostalCode 
                                      and Line_of_Business__c = :lead.Lead_Line_of_Business__c 
                                      limit 1];
          // if you found one
          if (zip.size() > 0)
          {   
              //find the name literal from the user ID
              List<User> agent = [SELECT Name FROM User WHERE Id =: zip[0].Assigned_Agent__c limit 1];
              //assign the lead owner suggestion to the agent for zip code field
              lead.Agent_for_Zip_Code__c = agent[0].Name;
              leadsToUpdate.add(lead);
          }
       }
    }
}