You need to sign in to do that
Don't have an account?
Susannah P
Need some help with apex class referencing custom metadata
Hi! I'm stuck on a bit of code that I hope someone might be able to help me with. Here's the problem I'm trying to solve:
I want to fill in 3 lookup fields on my custom object called "Approver 1", "Approver 2", "Approver 3". These custom fields are a lookup to the user record. I've created a custom metadata definition to store the mapping of owner of the record, to these approvers. Since custom metadata does not support entity definitions to the user record, I used a text field to store the owner Full name, Approver 1 full name, etc.
I've created a trigger and trigger handler to fill in these fields whenever a record is created. I'm stuck though in figuring out how to match up my custom metadata with the records being inserted. Essentially, for every record that is being inserted I want to see if there is a reference to the owner name in my custom metadata. If there is a match, I want the approver 1 lookup field on my custom object to be filled with the user that corresponds to the full name I put in my custom metadata. Here's what I have so far.
I've attached a screenshot, and here's the gist: https://gist.github.com/Sunnydalelow/87cd1d66e1ac1c68f8f15f3390309749#file-gistfile1-txt
Here's a sample of one of my custom metadata records:
I want to fill in 3 lookup fields on my custom object called "Approver 1", "Approver 2", "Approver 3". These custom fields are a lookup to the user record. I've created a custom metadata definition to store the mapping of owner of the record, to these approvers. Since custom metadata does not support entity definitions to the user record, I used a text field to store the owner Full name, Approver 1 full name, etc.
I've created a trigger and trigger handler to fill in these fields whenever a record is created. I'm stuck though in figuring out how to match up my custom metadata with the records being inserted. Essentially, for every record that is being inserted I want to see if there is a reference to the owner name in my custom metadata. If there is a match, I want the approver 1 lookup field on my custom object to be filled with the user that corresponds to the full name I put in my custom metadata. Here's what I have so far.
I've attached a screenshot, and here's the gist: https://gist.github.com/Sunnydalelow/87cd1d66e1ac1c68f8f15f3390309749#file-gistfile1-txt
Here's a sample of one of my custom metadata records:
As per my understanding its better to use map than putting record owners in a set. Please see below approach and let me know if this helps.
All Answers
As per my understanding its better to use map than putting record owners in a set. Please see below approach and let me know if this helps.
I had hoped to use my custom metadata definition to store the user name that I could use to lookup the ID of that user (I was trying to avoid hard coding IDs). Any help you could provide in helping me get this last step would be much appreciated!!
Here's the full code I have that is working, it's just inserting a String into my lookup field instead of an ID
I would suggest to store username instead of name in custom metadata. Because there can be user with same name in CRM but username will be always unique.
Now to get the user id we have to query the user. So let's take the usenames in a list and query user object at once.
I have added this in your code & it should be something like this-
Regards,
Debanjan