You need to sign in to do that
Don't have an account?
Randy 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:
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);
}
}
- Custom object created. It has 5 fields: zip code, assigned agent, line of business, and 2 fields used for duplicate checking.
- 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.
- 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.
- 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.
- The last step, the Apex code, has been started. That is where I am currently stuck.
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);
}
}
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
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
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);
}
}
}
}