function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nishant Gupta 15Nishant 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.
Best Answer chosen by Nishant Gupta 15
Andrew GAndrew G
Hi Nishant

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

Andrew GAndrew G
Hi Nishant

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

 
This was selected as the best answer
Nishant Gupta 15Nishant Gupta 15
Thanks Andrew, I will try this.
Dan ShenbergerDan Shenberger
Hello Andrew and Nishant

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.