Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Dynamics 365 Portals (CRM Portals) Redirect Not Working (Javascript)

Posted on by 480

Hi Everyone,

I have a button on a web form step and on click of it I have configured to run a realtime workflow in CRM (Portals OOB feature). This workflow generates an Invoice in CRM. My requirement was to get the GUID of Invoice which is created by this workflow but I did not find a direct way to do it. After getting the GUID I need to redirect the user to a payment page (I have Integrated a Payment Gateway) say https://myportal.com/PayNow/?invoiceid={{INVOICE_GUID}}.

I am able to get the GUID of Invoice created as required using a tricky way (i.e. created a dummy page with fetchquery returning the GUID) but Redirect functionality is not working in one-go. I have to click the button 2-3 times then it works. For redirect, I have added javascript and bind a .onclick event (client side) to the same workflow button (start payment process):-

Below is My Web Form Step (Portals):-

Portals-Pay.png

Below is the script I have added in custom script javascript section in Portals:-

debugger;
//bind function with click event of WORKFLOW button
$('.workflow-link').click(function() 
	{	
	document.getElementById("paymentmsg").style.display="block";	
	redirecttopay();
	
});


//function to query data from CRM and get the Guid + REDIRECT
function redirecttopay()
{
var evid=getQueryStringValue("id");
 
//this page has snippet having fetchxml to retrieve INVOICE GUID
var pageurl = "/events/event/event-query/?eventid=" +evid;
$.ajax({
  async: false, 
  url: pageurl,
  type: "get",
  success: function(response) {   
	var n = response.indexOf("invoiceid");	
	var invoiceGuid = response.substring(n+12, n+48);	 
	window.location.href="/pay-invoice/?id=" + invoiceGuid;   
	return false;	 
  },
  error: function(xhr) {
  
 }
});

}

//function to get id of event from URL
function getQueryStringValue (key) {  
  return decodeURIComponent(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));  
} 


Can someone suggest why it's not working? Is the default behaviour of workflow button (as it blocks UI) preventing the redirect from happening?


Many Thanks in Advance.


*This post is locked for comments

  • Suggested answer
    Arpit Shrivastava Profile Picture
    Arpit Shrivastava 7,518 User Group Leader on at
    RE: Dynamics 365 Portals (CRM Portals) Redirect Not Working (Javascript)

    Hi Rizwan,

    Once you click on the button (OOB action button) in order to call your real-time workflow. OOB It gives you a success message, isn't it?  Like: Workflow has been successfully initiated, Record has been deactivated etc. 

    I also had the similar requirement. What you can do is, on click of the button show the loader image/ In progress message and let your script do the magic behind. Using script wait to get the success message. As soon as it appears, retrieve the Guid of created record either using Odata or fetch xml and then redirected to the payment page.

    Hope you understood.

    -Arpit

  • Rizwan Aarif Profile Picture
    Rizwan Aarif 480 on at
    RE: Dynamics 365 Portals (CRM Portals) Redirect Not Working (Javascript)

    Hi Sandeep,

    Thanks for your suggestion. I agree It would be faster but will that resolve the issue with redirection ?

  • sandeepstw Profile Picture
    sandeepstw 4,601 on at
    RE: Dynamics 365 Portals (CRM Portals) Redirect Not Working (Javascript)

    hi,

    Instead of creating a page, I will suggest to create a web template which return invoice id. Which you can call using Ajax and it will be faster then current process. Your code looks good. I think it will take time to complete process and redirect to next page.

    also use window.location = "URL";

    Please visit this page for web template return json data.

    colinvermander.com/.../dynamics-365-portals-use-liquid-to-return-json-or-xml

  • Rizwan Aarif Profile Picture
    Rizwan Aarif 480 on at
    RE: Dynamics 365 Portals (CRM Portals) Redirect Not Working (Javascript)

    Hi Mahendra,

    I want my redirect script (js) to be executed once realtime workflow is completed. What is the best way to do so? We can add few seconds wait also, that will be fine. but it needs to redirect users to payment page as I mentioned.

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Dynamics 365 Portals (CRM Portals) Redirect Not Working (Javascript)

    Hi Rizwan,

    Real time workflow will first complete processing after that it will process other things. But once it is completed it should execute your rest of the code.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans