0

I have implemented rest service for getting the response from external system (Repco) to salesforce. For this, i have implemented Lighting component for sending appropriate required parameters to external system and here i placed rest service URL with "Page_URL" parameter of external system and created custom button (ex: Integration) on opportunity detail page. After user journey see the below.

  1. If User clicks the "Integration" custom lightning button on opportunity detail page then it's redirect to external system (here automatically authentication is completed because we are sending the username, password and action url of external system) there user can able to select the list of products and click on "Return Button" then external system will sending the response with our rest service URL.

  2. Rest Service will parse the response and create the opportunity line time and products records. After both Opportunity Line Item and Product records are created then it should be redirect to Opportunity detail page for seeing what are the records are created associated with Opportunity.However,it is not returning to opportunity detail page even if we use "return" or "page reference" object via helper class.

My question: How return to opportunity detail page record after DML operation is completed in rest service? Can you please let you know your thoughts.

Component:

<aura:component controller="integrationcontroller" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="vh" type="Product__c"/>
    <aura:attribute name="userInfo" type="user"/>
     <aura:attribute name="oppInfo" type="Opportunity"/>  
    <div class="spinnerLoading slds-align_absolute-center">
    <lightning:spinner alternativeText="Loading" size="large" />        
    </div>
    <body style="visibility: hidden;">        
        <form action='https://abc.externalsystem.com/test/rau/portal/login.aspx' id="formHook" name="form1" method="POST" target="action">
            <input type="text" name="NEW_ITEM-CUST_FIELD1[1]" value='{!v.userInfo.site}'/>
            <input type="text" name="Username" value='{!v.userInfo.username}'/>
            <input type="text" name="Password" value='{!v.userInfo.password}'/>
            <input type="text" name="HOOK_URL" value='https://mydev.cs765.force.com/integration/services/apexrest/test/testpag.aspx/'/>
            <input type="text" name="NEW_ITEM-CUST_FIELD2[1]" value='ABC'/>
            <input type="text" name="NEW_ITEM-CUST_FIELD3[1]" value='1'/>
            <input type="text" name="NEW_ITEM-CUST_FIELD4[1]" value='inquiry'/>
        </form>            
    </body>
</aura:component>

Controller:

({
    doInit : function(component, event, helper){
        console.log('doInit:');
        var action = component.get("c.getoppDetails"); 
        var action1 = component.get("c.fetchUser"); 
        var action2 = component.get("c.getOpportunity");

        action.setParams({
            "optyId": component.get('v.recordId')
        });

          action2.setParams({
            "ooptyId": component.get('v.recordId')
        });

        action1.setCallback(this, function(response) {
            var state = response.getState();

            if (state === "SUCCESS" && response.getReturnValue() != '') {

                 component.set("v.userInfo", response.getReturnValue());
            }
         }),
        action2.setCallback(this, function(response) {
            var state = response.getState();

            if (state === "SUCCESS" && response.getReturnValue() != '') {

                 component.set("v.oppInfo", response.getReturnValue());
            }
         }),


        action.setCallback(this, function(response) {
            var state = response.getState();
            console.log('sessionId: '+ state +'----' + JSON.stringify(response.getReturnValue()));
            if (state === "SUCCESS" && response.getReturnValue() != '') {               
                component.set("v.vh", response.getReturnValue());
                window.setTimeout(
                    $A.getCallback(function(){
                        console.log('submit form');
                        var dismissActionPanel = $A.get("e.force:closeQuickAction");
                        dismissActionPanel.fire();
                        $A.get('e.force:refreshView').fire();
                        helper.submitVehicleInquiryForm(component);
                    }),1000
                );                  
            }
            else if(state === "ERROR"){
                console.log('A problem occurred: ' + JSON.stringify(response.error));
            }
        });

      $A.enqueueAction(action);
      $A.enqueueAction(action1);
      $A.enqueueAction(action2);       
    }
})

Rest Service:

@RestResource(urlmapping='test/testpage.aspx')
global without sharing class MyInegration{

 @HttpPost
    global static string doPost()
    {
        RestRequest req=RestContext.request;
        RestResponse res=RestContext.response;
        Blob body = req.requestBody;
        Map<String,String> params = RestContext.request.params;
        String name = params.get('NEW_ITEM-CUST_FIELD1[1]');
        String productcode = params.get('NEW_ITEM-CUST_FIELD2[1]');
        String sku = params.get('NEW_ITEM-CUST_FIELD3[1]');

        //here opportunity line item record is created//

        // here we have to return this to opportunity detail page//
   }
}

User Flow:

  1. User clicks "Integration" custom lightning button on opportunity detail page then it is redirect to external system.
  2. User select the product in external system and clicks on "Return" button then external system instance URL is redirecting to salesforce rest URL on browser (it is showing empty on browser) then rest service is executing and creating the opportunity line item records based on response.

Issue: browser window is showing as empty.

Expected output: After rest api execution is complete then it's should be redirect to opportunity detail page. Please help me on this.

  • WE don't have enough information information to help you. Please edit your question to include more information. While the question may seem obvious to you, we have no idea what you're talking about. The more information you can give us, the better we can help you. Please read more about How to Ask to learn more. – sfdcfox Nov 19 '18 at 12:26
  • @sfdcfox: I have updated my question and requirement. Can you please check and let me know your thoughts. – Test userADG23 Nov 19 '18 at 12:43
  • In Lightning, you need to redirect on client-side using force:navigateToURL. – sfdcfox Nov 19 '18 at 12:49
  • We are using Lightning component and controller just for sending the username, password and action URL to external system for authentication. when authentication is success then user can select the product and click on "Return" button then system automatically send the response with our force.com site Rest URL. Can you please see the above code and let me know where i can use force.navigateToURL logic. – Test userADG23 Nov 19 '18 at 13:01
3

Your REST service executes in a separate transaction from your user's interaction with your application. You cannot reach into an active user session from a REST service to change their active page directly.

It's not totally clear from your description whether the user has navigated away from the Lightning application to interact with your external application. If the external application opens in a new window or tab while the user remains on the Lightning page, you can "signal" from your inbound REST service to your Lightning application that data has been received and a refresh should be executed by using the new <lightning:empApi> component to listen for a Platform Event. Your inbound REST service can fire such an Event when it finishes its processing.

If your user has left the Lightning record page and navigated away to your external application, your external application can wait for the completion of the Salesforce REST call and then perform a redirect to the record page you want.

Edit

Per the additional information you provided, this is your problem.

User select the product in external system and clicks on "Return" button then external system instance URL is redirecting to salesforce rest URL on browser (it is showing empty on browser) then rest service is executing and creating the opportunity line item records based on response.

Your external system should not perform a redirect to the REST URL for the Salesforce inbound rest service. Instead, your external system should perform a callout to this service synchronously - waiting for it to respond with success - and then perform a redirect to the Opportunity record page.

The Salesforce REST URL is not meant to be user-facing and the user's browser shouldn't be exposed to it in that way.

  • I see the REST service code you included, but that doesn't change the above. You must use an asynchronous communication channel (such as Platform Events) to communicate with an open user session, unless you want to have your Lightning component poll, which I really don't recommend. – David Reed Nov 19 '18 at 13:31
  • I have added user flow , issue and excepted output on above question . Can you please check and let me know your thoughts. – Test userADG23 Nov 19 '18 at 13:43
  • there is parameter called "Hook_URL" on external system. We need to add our service URL with this parameter for getting the response. External system is automatically redirecting to new browser with hook url whenever user click "return" button on external system. I hope your understand the issue. – Test userADG23 Nov 19 '18 at 13:53
  • @sfdcfox: I have updated my question and requirement. Can you please check and let me know your thoughts – Test userADG23 Nov 19 '18 at 14:06

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.