SalesforceLabs / survey-force

85 stars 61 forks source link

link to specific survey taken record #85

Closed dardenadams closed 3 years ago

dardenadams commented 3 years ago

Several years ago we had an implementation of Survey Force where we linked a Survey Taken record to a custom object (Orderc) via lookup and then sent out a link to that Survey Taken record for a customer to fill in. I've gathered through notes and some Apex backups on the implementation that the way this worked was that a dummy Case record was created and linked to the Survey Taken record via lookup (Apex script). Then, the email template used the Contact + Case ID merge link and since the dummy Case record was referenced in the link, the Survey Question Response records would be linked to the Survey Taken record, which was already linked to the dummy Case and by the custom Orderc object. Specifically, the notes say that so long as the same Survey, Contact, and Case ID's are specified in the survey URL, all Survey Question Response records get recorded to the same Survey Taken record.

Now, I've been trying to recreate this implementation, but it doesn't seem to work anymore. Despite referencing a dummy Case ID and using the same Contact and Survey IDs, when I anonymously click the survey link and fill it out, a totally new Survey Taken record is generated every time. The result is that I end up with two Survey Taken records: one linked to the custom Order__c object and the dummy Case and a second (new) one only linked to the Case.

Is there a way to either reference a custom object in the URL (instead of Case, since this was just the workaround used in the past)? Or to specify the Survey Taken record ID in the URL? Or at least to prevent a new Survey Taken record from being generated and have all Survey Question Response records associated to the same Survey Taken record?

jrattanpal commented 3 years ago

@dardenadams

I am not sure how to answer this one because it will depend on how you implemented it. It maybe that you need to look into your code and add it to Trigger for Survey Taken and make sure you use the same record rather than creating your own.

Or, there is another way. As you are changing the code, you may add your own code as well for order object.

You need to change https://github.com/SalesforceLabs/survey-force/blob/master/force-app/main/default/classes/ViewSurveyController.cls

IN addition to adding a new custom field on Survey taken that "looks up" to Order object.

Take line#74 (or 51, depends on where you use it) for example, this is where you get Case ID from the URL. You could add another URL parameter of, say, "oid" (order ID). Then you can capture that here.

Then in AddSurveyTaker() function, around like 362 or 398, you can store that new value in that new field

That should do it.

Of course it will become custom code. Every time you want to get new features from Survey (AS we implement those) you need to ensure you don't simply overwrite that code.

Hope this helps.

dardenadams commented 3 years ago

Thanks for pointing me to ViewSurveyController. I'll give it a try!

dardenadams commented 3 years ago

Got it working. As you suggested, I added a URL parameter to pass through Order ID. I also added a URL parameter for Survey Taker record ID and a new function similar to AddSurveyTaker() called AppendToSurveyTaker(). Instead of adding a new survey record, this will add Survey Question Response records to an existing Survey Taken record, provided no responses have yet been recorded. It's activated by the presence of a Survey Taken record ID in the URL (otherwise AddSurveyTaker() runs as normal). Should make my users happy - what they wanted was the capability to create a survey record, add some internal notes and other information to it, then trigger an email to the customer containing the survey link. Now I can do that and get the responses recorded on the original survey record. Thank you for pointing me in the right direction.

jrattanpal commented 3 years ago

Glad to hear and thanks for letting us know.

I want to add a disclaimer. Please make sure your code is secure so make sure to run all tests as needed.

charles-thompson-salesforce-com commented 2 years ago

Steps to link a Survey to an object other than Contact and Case

  1. Add a lookup field on Survey Taker to the linked object
  2. Manually add a URL parameter to the outgoing email that links to the Survey - this parameter references the linked record
  3. Edit ViewSurveyController Apex class:
  4. In the section where URL parameters are being read, add code to read this new parameter
  5. Update addSurveyTaker() method to include the new field value that you read from the parameter