You need to sign in to do that
Don't have an account?
DavisTM
Apex trigger to copy a value from a custom field to a standard field
Hi,
I need some help...
I have an idea, but am not sure how to accomplish it using an Apex trigger...
User Action: Create a new Lead
Step #1: Enter the lead's first name, last name, email, etc.
Step #2: Select the comapny name from a custom lookup field
Step #3: Save the new lead.
Here's the issue, Company is a standard required field when creating a new Lead. I would like two things to happen during this action. The first is to check the Company field, and if it is blank, check if there is a value in the custom lookup field. If there in a value in the custom lookup field, then copy the value to the Company field.
Can this be done using an Apex trigger? If so, can you show me how?
Thanks.
I need some help...
I have an idea, but am not sure how to accomplish it using an Apex trigger...
User Action: Create a new Lead
Step #1: Enter the lead's first name, last name, email, etc.
Step #2: Select the comapny name from a custom lookup field
Step #3: Save the new lead.
Here's the issue, Company is a standard required field when creating a new Lead. I would like two things to happen during this action. The first is to check the Company field, and if it is blank, check if there is a value in the custom lookup field. If there in a value in the custom lookup field, then copy the value to the Company field.
Can this be done using an Apex trigger? If so, can you show me how?
Thanks.
1. I re-written the code more generically so that you can replace the API values with the values from your org. Please review the code below for reference:
In Developer Console, go to File, hover over new and select Apex Class. Name the Apex Class, "LeadTriggerHelper" and click Ok. Afterwards, copy and paste this code:
After you make the API changes and save it, go to Developer Console, go to File and Hover over new and select Apex Trigger. Name it LeadTrigger and select Lead as the sObject.
Copy and paste the code below:
As a best practice, one should always put the code of a trigger within a separate Apex Class and just reference the method in the Apex Trigger. The code will be easier to manage just in case you decide to add more trigger methods in the future.
2 - I am not sure what you are referring to when you state user types. Lets say a user selects one of the look up records in the Look up field. Would you like for the standard Company field to state "Existing Company" rather than having it be replaced with the name of the custom company record? If that is what you meant, then yes that is possible. Minor updates would have to be made to the code.
3 - Only updates and insertions of a new or existing lead record will cause this trigger to execute. Thus, if the "+" is shown on lead records and a user clicks it to perform some type of change on the record, then this trigger will execute. However, if the "+" is shown only on the custom object, then it should not trigger a change to the lead records when used. To better understand, is the "+" a button? and where does it usually appear? A sample screenshot would be helpful to get a better understanding.
4 - That is correct. If a user does not have editing access, then they should not be able to utilize the "+" sign. Profiles of those you want to restrict access to should be read only. Use permission sets to give certain individuals access to it.
If you have any more questions or concerns, please feel free to reach out to me. I'm am more than happy to help!
All Answers
Also what if that lookup is also blank or that will never happen. If that never happens than you can remove the check below. Also replace the <CustomLookupField> with the actual field. This may help you some understanding.
If the user didnt select a value from the custom lookup field, then he/she has to type a value in the comapmy field. The company field is a standard, required field. This may be the reason why there is an error. Maybe a prompt telling the user they have to select a value from the lookup field.
HTH
Andrew
Very clean code... I get the following error when I attempt to create the save the new Lead record;
These required fields must be completed: Company
I made the following changes to your code, and got the same error; "These required fields must be completed: Company"
The value selected in the lookup field isn't being copied into the company field when I try to save the new lead record.
The error is caused by the Order of Execution considerations:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm
Compulsory fields are assessed prior to the firing of the Before Insert triggers. Steps 2 v 4.
Not sure how to side-step that one.
Regards
Andrew
So, this means I cant accomplish this action with an Apex Trigger, correct?
https://help.salesforce.com/articleView?id=000325733&type=1&mode=1
Where person accounts are not enabled, then an idea has been subbed to SF. May need an additional vote.
https://success.salesforce.com/ideaView?id=08730000000km09AAA
Regards
Andrew
Yes. The limitation / restriction / nature of the Order of Execution prevents you from solving this issue by use of a trigger.
By default, the Company is a required field and required to be on the layout, so you can't even remove it and create your own custom field.
You can't even set a default value. If that was the case, you would test for the default value with the trigger.
So the only way around I could think of would be ..... not a good solution ... but advised the User to put a default value (such as "new") and then test for that with the trigger. But I can see that giving rise to poor data quality, which is what I think you are trying to improve/resolve with the trigger in the first place.
Regards
Andrew
If we look at the order of execution, the trigger would fire, we could then put a Custom Validation on the Object, which test for "new" also.
But, users would figure out that they could put "n", which means the SF validation succeeds. If the custom validation is IF(Company = 'new', ......) throw error, then the user can bypass the validation and arise to poor data quality.
You could try something like the above, and then do user Training to emphasis the value of good data and how it helps them. But we are now relying on human action to give good data, not validation of by use of code/formula/system. (if you know what I mean).
Perhaps, add a validation on the Company field that it has a length greater than 3 IF(LEN(Company) <3, throw error) . I think i've rarely seen companies with less than 3 letters in the name.
There's my thoughts.
Regards
Andrew
Unfortunately, in this case, I think the only way to bypass this issue would be to create your own custom Lead object. By doing this, you'll be able to create your own Company field and not make it required. This would resolve the issue you are having after creating the trigger.
In regards to your question, the ID of the company would populate. If, however, you wanted to get another field value from that record, you'll need the following: List, SOQL, Map <ID, LookUpObject>
Below is a code example:
If you have any questions, please feel free to comment. I hope my answer helped!
The idea by Christan has merit. Creating your own Custom Lead object would circumvent the issue you are experiencing. I would just offer counsel to consider that path carefully, as if you create your own Custom Lead object, you would need to replicate all the other OOB functions related to Leads. For example, the Conversion process, which creates the Account/Contact & possibly Opportunity, would need to be rewritten. Any OOB reports related to Leads would need to be recreated.
I'm not saying don't do a custom Lead object, just keep in mind that if you go down that path, other customisation will be required later.
Christin's idea does give rise to another idea - create a custom LeadInput Object, which on save creates the Lead object in the background. There would still be customisation, but you could still leverage the OOB functions for the Lead. It would mean having an Object who's only purpose is input, as I would recommend deleting the entries after the Lead is succesfully created. Or alternatively, (and i'm happy to be corrected by others who know Visualforce & Controllers better), perhaps have the custom LeadInput with a custom controller that validates the input and then creates the Lead record without saving the LeadInput object.
You have some decisions to make.
Hope the above has helped.
And thanks to Christan for the alternate view on a solution.
Regards
Andrew
I did the trailmix. It's amazing what can be accomplished with Flow & Process Builder. The challenge her is the field is a standard and required field. This field type seems to pose some challenges. Apprently in the next release, Flows will have the capability to update these field types. I will review the module you referred to. Maybe I am forgetting something...
Andrew,
Many thanks for your recommendations and input! I learned a lot from this interaction. I don't really want to go the route of creating a new object and doing cross object relationships, etc. I think its too much effort and complexity for this scenario. Your statement;
"But I can see that giving rise to poor data quality, which is what I think you are trying to improve/resolve with the trigger in the first place." is true. This is my goal.
Christian,
I see how your solution would work if the field was type was different, or I created a custom Lead Object. I don't want to create a custom object and have to maintain the complexities which will will follow....
I have some thinking to do, and decisions to make...
I am as excited as you are!! A few questions before I give it a try...
I'll keep an eye out for your response...
1. I re-written the code more generically so that you can replace the API values with the values from your org. Please review the code below for reference:
In Developer Console, go to File, hover over new and select Apex Class. Name the Apex Class, "LeadTriggerHelper" and click Ok. Afterwards, copy and paste this code:
After you make the API changes and save it, go to Developer Console, go to File and Hover over new and select Apex Trigger. Name it LeadTrigger and select Lead as the sObject.
Copy and paste the code below:
As a best practice, one should always put the code of a trigger within a separate Apex Class and just reference the method in the Apex Trigger. The code will be easier to manage just in case you decide to add more trigger methods in the future.
2 - I am not sure what you are referring to when you state user types. Lets say a user selects one of the look up records in the Look up field. Would you like for the standard Company field to state "Existing Company" rather than having it be replaced with the name of the custom company record? If that is what you meant, then yes that is possible. Minor updates would have to be made to the code.
3 - Only updates and insertions of a new or existing lead record will cause this trigger to execute. Thus, if the "+" is shown on lead records and a user clicks it to perform some type of change on the record, then this trigger will execute. However, if the "+" is shown only on the custom object, then it should not trigger a change to the lead records when used. To better understand, is the "+" a button? and where does it usually appear? A sample screenshot would be helpful to get a better understanding.
4 - That is correct. If a user does not have editing access, then they should not be able to utilize the "+" sign. Profiles of those you want to restrict access to should be read only. Use permission sets to give certain individuals access to it.
If you have any more questions or concerns, please feel free to reach out to me. I'm am more than happy to help!
In regards to question #2: In short, disregard.
In regrads to question #3: I only need this to value from the lookup field to replace the value in the Company field before insert. As a result, should I remove before update, OR is before update required for this to work?
This is my edit of the general code you provided. I am not certain its 100% correct. Perhaps you could do a quick review to confirm?
In regards to your code, please make the following changes:
Line 07: Please < > around Account after List. (It should look like this: List <Account> lookUpObjList = [SELECT Id, Name FROM Account];)
Line 17: Replace <Id, compName__c> with <Id, Account>
Besides that, everything else looks good to go.
Regarding, #3: You can remove the "before update" phrase from the Apex. Please note though by doing this, this trigger will only execute once which will be before the record is saved to the database rather than each time that record is update.
One thing to keep in mind for future. Lets say after the lead is created, the name of the account changes at some point in the future. When the name of the Account changes, please note that lead record related to that account will still showcase the old name and not the new updated name. If you would like for this to occurr, we would have to create a separate trigger for this on the Account object to update the related lead record. If you don't think this scenario will ever occur, then you can disregard this comment. I just wanted to make sure that you were aware of this possible scenario.
The scenario you describe may occur. So, I will need to add a separate trigger on the Account object. If the Company name changes before the lead is converted, then the update will occur; I like the way you think...
I can write a test Apex class if you like. I can also write a trigger for the future scenario I mentioned as well if you think it is needed. Enjoy!
I would greatly appreciate the test class and the trigger for the future scenario. I assume the future scenario will need a test class as well... Is there a way I can direct message you?