You need to sign in to do that
Don't have an account?
Nishant Gupta 15
Allow a user to vote only once
I created a lightning component that allows a user to upvote or downvote an 'idea' (custom object). I want a user to be able to vote only once. I need some kind of a check to see if user has previously voted or not. What are the ways achieve this? Thanks in advance.
Quick idea would be to create another custom object - "Idea Vote" with custom fields - Voted by (Lookup - User Account), Related Idea(Lookup - Idea), and possibly - Vote Type (Picklist - Up/Down)
When someone votes, SOQL to get all related "Idea Votes", IF user present, error "already voted". ELSE create/insert a new "Idea Vote" record.
We are assuming that the user is recognised by Salesforce (logged in).
The idea of the Vote Type as a picklist would be if you wanted to allow someone to change from "Down" vote to "Up" vote, or vice-versa.
You could then use the picklist value in a rollup summary to the "Idea" object to manage your counts.
Regards
Andrew
All Answers
Quick idea would be to create another custom object - "Idea Vote" with custom fields - Voted by (Lookup - User Account), Related Idea(Lookup - Idea), and possibly - Vote Type (Picklist - Up/Down)
When someone votes, SOQL to get all related "Idea Votes", IF user present, error "already voted". ELSE create/insert a new "Idea Vote" record.
We are assuming that the user is recognised by Salesforce (logged in).
The idea of the Vote Type as a picklist would be if you wanted to allow someone to change from "Down" vote to "Up" vote, or vice-versa.
You could then use the picklist value in a rollup summary to the "Idea" object to manage your counts.
Regards
Andrew
Thank you for the answer to this, as I am attempting to do the same thing. Out of curiosity, do either of you have the Apex you used to achieve this? I'm relatively new to the Apex game, and do much better at reverse engineering code to make it do what I need.