0

I need to send a survey link to a list of contacts, from a lightning component. It does send the link, with the right template. But with the wrong data. We use survey force. So i have:

apex controller:

// this is the method that the component uses to submit the Survey__c information
@AuraEnabled
public static String submitSurvey(String surveyId, String contacsIdsListJSON, String Lang, String oppId){
        objectSubmitReturn obj = new objectSubmitReturn();

        List<String> contactIdsList = (List<String>) System.JSON.deserialize(contacsIdsListJSON, List<String>.class);

        Set<String> contactIdSet = new Set<String>(contactIdsList);

        List<Contact> contactList = [SELECT Id, LastName, OppId__c, FirstName, Name, AccountId, Email FROM Contact where id in : contactIdSet];

        Survey__c surveys = [SELECT Id, Name FROM Survey__c where id = :surveyId];

        String langConcat = '%_' + Lang;
        EmailTemplate template = [
            SELECT Id, Name, DeveloperName
            FROM EmailTemplate where DeveloperName like :langConcat  limit 1];

        List<Messaging.SingleEmailMessage> emailsMsgs = new List<Messaging.SingleEmailMessage>();

        Map<String, SurveyInvitation> mapInvitations = new Map<String, SurveyInvitation>();

        for(Contact cont: contactList){
            Messaging.SingleEmailMessage singleEmail = new Messaging.SingleEmailMessage();

            List<String> emailList = new List<String>();

            emailList.add(cont.Email);

            singleEmail.setToAddresses(emailList);
            singleEmail.setWhatId(surveyId);
            singleEmail.setSenderDisplayName('Salesforce Ideas!');
            singleEmail.setTargetObjectId(cont.Id);
            singleEmail.setReplyTo('[email protected]');
            singleEmail.setTemplateId(template.Id);

            emailsMsgs.add(singleEmail);

            Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {singleEmail};

            Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

            if (results[0].success){
                obj.status = true;
                System.debug('The email was sent successfully.');
            } else {
                obj.status = false;
                obj.errorMsgReturn = 'The email failed to send: ' +  results[0].errors[0].message;
                System.debug('The email failed to send: ' +  results[0].errors[0].message);
            }
        }

        // custom field in contact, should have the current Survey__c id
        for(Contact cont: contactList){
            cont.OppId__c = oppId;
        }
        
        update contactList;

        // verifying Contact information
        List<Contact> verify = [SELECT Id, LastName, OppId__c, Email FROM Contact where id in : contactIdSet];

        System.debug(verify);

        return JSON.serialize(obj);
    }

the template in use, has this code

https://quadrantesac--sac--c.cs110.visual.force.com/apex/myVFpage?id={!Survey__c.Id}&cId={!Contact.Id}&oppid={!Contact.OppId__c}&caId=none

OppId__c 123: {!Contact.OppId__c}
Email: {!Contact.Email}
LastName. {!Contact.LastName}
Contact: {!Contact.Id}

Then i get this in my email: enter image description here:

The thing i'm passing an id to Contact.OppId__c, which is lookup field, it gets filled in the contact object, and i can even click in the interface, taking me to the right opportunity. But in the template, {!Contact.OppId__c} becomes the opprtunity 's name, which of course isnt a valid id.

finally i get this in my logs enter image description here

That is, i can't understand why oppid, in the link sent to the email, is "SE ISANG 400kV / SE MORUPULE B 400kV / LINHA 218kM 400kV" and not "0060C000004i4EXQAY"

And as you can see the id of OppId__c is valid. Any hints?

1 Answer 1

0

I found a solution, even if unsatisfying. Because it's lookupfield, the template engine replaces the id with the name. So i create a formula in Contact that outputs the Opportunity id, and instead if using the field directly, i use that formula. I was surprised, given it's very different behavior to Apex. Worst, i believe.

Thanks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .