0

I have a requirment where i need to show some alert messages to the user. The alert message should be non blocking and if the user scrolls the page down, the alert should also move so that the user will always have the attention to it.

I looked at the showToast and saw that it serves the purpose what i need. However, the message which will be displaying will contain URLS. There can be 5 urls or 4 etc. Basically the URLs will be build dynamically.

I looked at the documentation and saw messageDataTemplate attribute with a label and URL but i am stuck as to how i can populate the message data template dynamically ?. It works if i manuyally hard code the URLS multiple or 3 but cannot figure out the dynamic part.

var dynamicMessage = 'Record {0} created! See it {1},{2}!'
     var toastEvent = $A.get("e.force:showToast");
     toastEvent.setParams({
                "mode": "pester",
                "type": "error",
         "duration": "20000",
         "message": "Test Error Message",
         "messageTemplate": dynamicMessage,
         "messageTemplateData": ['Salesforce', {
         "url": 'http://www.salesforce.com/',
         "label": 'hello',            
         }, 
         {
         "url": 'http://www.cnn.com/',
         "label": 'foo',            
         }
    ]
            });
            toastEvent.fire();

what i want is the messageTemplateData to be created dynamically instead of me hardcoding. There can be 3 or 4 depending on how many aere stored in DB and what the controller will send me. Any help will be greatly appreciated

Update

THis does not work.

({
init : function(component, event, helper) {
    var finalList = [];
    for(var i=0; i<2; i++) {
        console.log('Adding rows');
        var answers = [];
        answers.url = 'foo.com';
        answers.label = 'hello';
        finalList.push(answers);
    }
    console.log('final List ' + finalList.length);
    var dynamicMessage = 'Record created! See it {1},{2}!'
     var toastEvent = $A.get("e.force:showToast");
     toastEvent.setParams({
                "mode": "pester",
                "type": "error",
         "duration": "20000",
         "message": "Test Error Message",
         "messageTemplate": 'Record created! See it {0},{1}!',
         "messageTemplateData": finalList
            });
            toastEvent.fire();
}
0

Update:-

A sample example would be:-

var finalList = [];
    for(var i=0; i<2; i++) {
        finalList.push({
            url: 'www.goole.com',
            label: 'google'
        });
    }

here I am creating a dynamic array with two parameters url and label. You can assign the array directly to messageTemplateData as follows:-

"messageTemplateData" : finalList, 

Make change accordingly in the above piece of code and it will work.

5
  • I tried this but it displays NULL on the message. Do you know why ?
    – Darkdog
    Aug 7 '19 at 15:29
  • How you have tried? Can you edit your question with the update? Aug 7 '19 at 16:27
  • Yep i did try. I also updated my thread. Looks like the finalList is displaying nothing.
    – Darkdog
    Aug 7 '19 at 18:05
  • @Darkdog I have updated the answer and tested it. IT is working fine now Aug 7 '19 at 20:05
  • thank you soi much
    – Darkdog
    Aug 7 '19 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.