You need to sign in to do that
Don't have an account?
Brendon Wilkinson 59
Issue with Trigger to relate a record to a custom object
I followed the instructions here: https://support.zendesk.com/hc/en-us/articles/203660056-Salesforce-Creating-custom-links-in-Salesforce-Zendesk-Ticket#creating-a-trigger
This trigger works except that row 7 assumes that at least 1 record is found. If not it throws an error 'List has no rows for assignment to SObject' if there is not a record available for assignment.
I found this answer: https://help.salesforce.com/articleView?id=000159853&type=1
And have tried to incorporate it but I keep getting errors. Can anyone give some guidance on how to rewrite this?
This trigger works except that row 7 assumes that at least 1 record is found. If not it throws an error 'List has no rows for assignment to SObject' if there is not a record available for assignment.
I found this answer: https://help.salesforce.com/articleView?id=000159853&type=1
And have tried to incorporate it but I keep getting errors. Can anyone give some guidance on how to rewrite this?
firstly we create a set of the gig names
then we get a list of gigs using those names (I hope they are unique!)
we then put those gigs into a map using their names
finally we loop over the resource requests, look at the gig name and use that to look up the gig from the map and set the Id if it's wrong
I've also added a quick null check incase we couldn't find a gig with the name listed on the resource request
All Answers
I've had to type out your code from the image, sorry if there are typos
firstly you should bulkify your trigger, you may have a specific use case at the moment but who knows what happens in the future and if the trigger has to process more than 1 record at a time things will soon crash,
hopefully this should help you out
In fact I have been trying to use the template to relate one custom object to another (utilizing the structure sent previously) however when I converted this I am getting an error>
Error: Compile Error: Illegal assignment from Map<String,Id> to Map<String,Gig__c> at line 13 column 29
(Map<String, Gig__c> gigsByName = new Map<String, Gig__C>();)
Do you know how to resolve this issue?
Thanks,
You were really close, the issue was at line 15 where you are trying to put a String and Id into a String and Gig__c map, just change that to gigsByName.put(o.Name, o); and it should work better
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger RelateResourceRequestToGig caused an unexpected exception, contact your administrator: RelateResourceRequestToGig: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.RelateResourceRequestToGig: line 23, column 1
firstly we create a set of the gig names
then we get a list of gigs using those names (I hope they are unique!)
we then put those gigs into a map using their names
finally we loop over the resource requests, look at the gig name and use that to look up the gig from the map and set the Id if it's wrong
I've also added a quick null check incase we couldn't find a gig with the name listed on the resource request