Vote Article as Guest/Unauthenticated User in Salesforce Community

INTRODUCTION

How can We enable article voting for the guest users without prompting them to login. Salesforce lets guest users vote their own article without logging in.

SOLUTION

To allow guest users to like or dislike articles in Salesforce, you can follow these steps:

  1. Create a custom object to track article votes. This object should have fields to store the article ID, vote type (like or dislike), and any other relevant information.
  2. Enable public access to the custom object so that guest users can interact with it.
  3. Add a button or link on the article page layout to capture the guest user’s vote. This button/link should trigger an Apex controller or a Lightning component.
  4. In the Apex controller or Lightning component, write code to handle the vote submission. This code should validate if the guest user has already voted for the article to prevent multiple votes. If the user has not voted, create a new record in the custom object to store the vote information.

Here’s an example of how the code:

LWC:

<template>
    <div>
        <button onclick={handleLike} disabled={isLiked}>Like</button>
        <button onclick={handleDislike} disabled={isDisliked}>Dislike</button>
    </div>
</template>

LWC JS Controller

import { LightningElement, api } from 'lwc';
import submitVote from '@salesforce/apex/ArticleVoteController.submitVote';

export default class ArticleVote extends LightningElement {
    @api articleId;
    isLiked = false;
    isDisliked = false;

    handleLike() {
        submitVote({ articleId: this.articleId, voteType: 'Like' })
            .then(() => {
                this.isLiked = true;
            })
            .catch(error => {
                console.error('Error submitting vote:', error);
            });
    }

    handleDislike() {
        submitVote({ articleId: this.articleId, voteType: 'Dislike' })
            .then(() => {
                this.isDisliked = true;
            })
            .catch(error => {
                console.error('Error submitting vote:', error);
            });
    }
}

Apex Controller:

public class ArticleVoteController {
    public static void submitVote(String articleId, String voteType) {
        // Check if guest user has already voted for the article
        // Query the custom object to determine if a vote record exists for the guest user and article ID

        // If no vote record exists, create a new record to store the vote
        if (/* Check if no vote record exists */) {
            ArticleVote__c newVote = new ArticleVote__c();
            newVote.ArticleId__c = articleId;
            newVote.VoteType__c = voteType;
            // Set any additional fields as needed

            // Insert the new vote record
            insert newVote;
        }
    }
}

In your Lightning component or Visualforce page, call the submitVote method from the Apex controller when the guest user clicks the like or dislike button. Pass the article ID and vote type as parameters to the method.

Remember to handle any necessary error checking and provide appropriate feedback to the guest user after submitting the vote.

Note: This is a basic example, and you may need to customize it further based on your specific requirements and Salesforce implementation.