• Randy Henry
  • NEWBIE
  • 20 Points
  • Member since 2015
  • IT Executive Director
  • GlobalHealth

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
I am running into test coverage issues when trying to get my new Apex Trigger configured. The issue is with code that is strictly a SOQL query and then placement of the value in the DB. The code takes a value (a User ID from a custom object) and looks it up in the User object. It then finds the Name and places that in a field on the Lead record.

Here is the code that I can't seem to get covered (I'm currently at 72%):
//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);

I have the rest of the code working fine. Basically I create a new lead, put in all of the required fields, and submit. It bypasses this code every time. 

Here is my trigger:
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);
          }
       }
    }
}

Here is the test class:
@Istest
private class TestZipCodeAgentPopulate 
{
    static testMethod void insertLead1() 
    {
        Lead l = new Lead();
        l.FirstName = 'First';
        l.LastName = 'Last';
        l.Product_Line__c = 'MA';
        l.Sub_Product_Line__c = 'Gen';
        l.LeadSource = 'Network';
        l.Permission_to_Contact__c = 'Yes';
        l.Status = 'New';
        l.Lead_Line_of_Business__c = 'MA Sales';
        l.PostalCode = '73001';
        insert l;
    }
}
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);
          }
       }
I am running into test coverage issues when trying to get my new Apex Trigger configured. The issue is with code that is strictly a SOQL query and then placement of the value in the DB. The code takes a value (a User ID from a custom object) and looks it up in the User object. It then finds the Name and places that in a field on the Lead record.

Here is the code that I can't seem to get covered (I'm currently at 72%):
//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);

I have the rest of the code working fine. Basically I create a new lead, put in all of the required fields, and submit. It bypasses this code every time. 

Here is my trigger:
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);
          }
       }
    }
}

Here is the test class:
@Istest
private class TestZipCodeAgentPopulate 
{
    static testMethod void insertLead1() 
    {
        Lead l = new Lead();
        l.FirstName = 'First';
        l.LastName = 'Last';
        l.Product_Line__c = 'MA';
        l.Sub_Product_Line__c = 'Gen';
        l.LeadSource = 'Network';
        l.Permission_to_Contact__c = 'Yes';
        l.Status = 'New';
        l.Lead_Line_of_Business__c = 'MA Sales';
        l.PostalCode = '73001';
        insert l;
    }
}
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);
          }
       }