0

I created a record hyperlink and sending it over an email but when I click the link the record id in the URL is changing like this '%7Ba006F00003dLUwDQAW%7D'

enter image description here

trigger UserReqNotification on Sfdc_user_request__c (after insert) {

    Set<Id> allInsertedIds = trigger.newMap.keySet();
    System.debug(allInsertedIds);

    Sfdc_user_request__c userRes = [SELECT Id, Salesforce__C, Notified_System_Owner__c, Servicemax__C, Role__c, Profile__c, Department__c,Division__c, Title__c, Position__c,QA_Sandbox__c FROM Sfdc_User_Request__c where id = :allInsertedIds];

    String link = '<a href="https://ap1.salesforce.com/' + allInsertedIds + '">User request : ' + allInsertedIds + '</a>';
    //String link = '<a href="https://ap1.salesforce.com/'+ String.valueOf(allInsertedIds) +'">User request : '+ allInsertedIds + '</a>';

    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    List<String> sendTo = new List<String>();
    sendTo.add('[email protected]');
    for (Sfdc_user_request__c ur : trigger.new) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(sendTo);
        mail.setSubject('SFDC User request');
        mail.setSenderDisplayName('New SFDC User Request');
        //String body =  '<a href='+ ur.Id +'></a>';
        //mail.setPlainTextBody('https://tapp0.salesforce.com/'+allInsertedIds);
        mail.setHtmlBody(link);
        mails.add(mail);
    }
    Messaging.sendEmail(mails);
}

1 Answer 1

2

That is because you are trying to insert Set of Ids inside allInsertedIds.

Set inserts a { and } which when in UTF-8 converts to %7B and %7D respectively.

Thus it would be better if you are looping on the Map returned from Trigger.newMap then add a a and <br> tag separately for each ids.

First use only Map as your context is after insert

Map<Id, Sfdc_user_request__c> sObjectsByIds = Trigger.newMap;

String link = '';
for(Id sfdcUserRequestId  : sObjectsByIds.keySet()){
    sObjectsByIds.get(sfdcUserRequestId);
    link = link + '<a href="https://ap1.salesforce.com/'+ sfdcUserRequestId +'">User request : '+ sfdcUserRequestId + '</a> <br>';
    mail.setHtmlBody(link);
}
mails.add(mail);

Also I see you are sending it to only one address, so no need to creating those many emails. Just one instance with separate links for each user.

Or even this would work:

String link = 'User request : <br>';
for(Id sfdcUserRequestId  : sObjectsByIds.keySet()){
    sObjectsByIds.get(sfdcUserRequestId);
    link = link + '<a href="https://ap1.salesforce.com/'+ sfdcUserRequestId +'"> '+ sfdcUserRequestId + '</a> <br>';
    mail.setHtmlBody(link);
}
mails.add(mail);
Messaging.sendEmail(mails);

You must log in to answer this question.

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