function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Elizabeth Bryant 17Elizabeth Bryant 17 

Lightning Data Service Doesn't Capture Manual Record Approval


I have been wrestling with this issue for quite some time and I'm hoping someone here will have some suggestions. Please let me know if this is better suited under a different board.

Org details: Unlimited Edition org with Service Cloud
Cart details: Launched via Lightning button on the custom object's Lightning page. Using Visualforce, Lightning Out, Aura components.

Things I've tried: Platform Events (not compatible), LMS (also not compatible), Lightning Data Service's force:recordData component with recordUpdated property and using reloadRecord() function (current).
We have a custom cart solution for service reps to use to place Part and Service Orders for our members, which passes all data between components and Apex controllers using JSON objects. This cart is used by all Member Care teams within the United States, so starting over from scratch is a huge undertaking and is not within our current bandwidth. We do have plans to eventually rewrite it all in LWC.

One specific team has a requirement that all Part Orders must have an approved Service Order associated with it before we send the JSON with order details to another system for the Parts. This requirement works fine unless the record doesn't meet criteria for auto-approval and is sent to an approval queue instead. Those manual approvals are typically done right away, within a minute or so. The team would like a callout to be delayed and a particular button in the aura component to be disabled until that Service Order is approved and they can move forward.

I have tried using recordData with the most success, but it's not able to detect the manual approval's field update to the record. I've tried various versions of the below.
<aura:attribute name="recordId" type="String" description="Service Order Id" />
    <aura:attribute name="newServiceOrder" type="Service_Order__c"/>
    <aura:attribute name="updatedServiceOrder" type="Service_Order__c" />
    <aura:attribute name="showPlaceOrderButton" type="Boolean" default="false" />

    <force:recordData aura:id="serviceOrderRecord" 
        recordId="{!v.recordId}" 
        targetRecord="{!v.newServiceOrder}" 
        fields="Id, First_Approval_Status__c, Approved_For_Dispatch__c" 
        targetFields="{!v.updatedServiceOrder}"
        recordUpdated="{!c.handleServiceUpdate}" 
    />

Component controller - handleServiceUpdate doesn't capture the new field value as Approved using v.newServiceOrder or v.updatedServiceOrder, with or without using the fields.fieldName.value format, with or without checking Approved_For_Dispatch__c (another field that is set by the approval process).
handleServiceUpdate : function(component, event, helper){
        console.log('-- POA_OrderCheckoutModalController.handleServiceUpdate hit --');
        var changeType = event.getParams().changeType;
        var updatedServiceOrder = component.get('v.updatedServiceOrder');
        console.log('-- POA_OrderCheckoutModalController.handleServiceUpdate updatedServiceOrder : ', JSON.stringify(updatedServiceOrder, null, 3));
        console.log('-- --- ---- ---- Approval Status ' + updatedServiceOrder.fields.First_Approval_Status__c.value);

        if(changeType === "CHANGED" && updatedServiceOrder.fields.First_Approval_Status__c.value === 'Approved'){
            alert('!!!!!! First_Approval_Status has changed to Approved !!!!!!');
        }
    },

I've also tried this in the helper, right before the callback for the Apex that will call the other system. I've tried do-while, and while loops and also tried two ways of checking the values in updatedServiceOrder, with or without setTimeout(). I'm getting the "not approved yet" log in all variations.
var updatedServiceOrder = component.get('v.updatedServiceOrder');
        do {
            console.log('not approved yet');
            // setTimeout(10000);
             component.find('serviceOrderRecord').reloadRecord();
        } while(updatedServiceOrder.First_Approval_Status__c != 'Approved' || updatedServiceOrder.Approved_For_Dispatch__c != true);
    //} while(updatedServiceOrder.fields.First_Approval_Status__c.value != 'Approved' || updatedServiceOrder.fields.Approved_For_Dispatch__c.value != true);



Ultimately, my results are that the "Approved" First Approval Status (and Approved For Dispatch) value is never captured by the component, so our spinner continues to spin indefinitely and the cart doesn't move forward because the condition in the while/do-while attempts is never met. 

 #Salesforce Developer #Automation  #Sales Cloud
Naveen KNNaveen KN
looks interesting!

if I understood correctly, we have to check the approval status from the backend.

can we poll apex from the lightning component and get the status back to aura?? have a look at http://www.jitendrazaa.com/blog/salesforce/calling-apex-method-at-regular-interval-from-lightning-component/

do you think this will work? 
Elizabeth Bryant 17Elizabeth Bryant 17

It's been a while, I got pulled in a few other directions. I did, however, make a bit of progress on this. 

@Naveen the suggestion you made wouldn't have worked for this particular scenario - I was trying to avoid additional querying due to the application already having a high number of SOQL statements.

 

What I did do, was decided to try to move the Aura components off of the Visualforce page and launch them into a new console tab using a Lightning Component Quick Action. I encountered some struggles with the scenario, but I've finally gotten it to render using workspaceAPI and a pageReference. The plan is to now utilize Platform Events to communicate the manual approval to the component.

I am still stuck, however.
I've found that once we add items to one of the carts the buttons to cancel or proceed to checkout disappear behind the console app's utility bar. The tab itself is already allowing scrolling of the page, but my guess is that I need to do some kind of CSS or Javascript to make sure that the utility bar does not cover the buttons. Does anyone have any suggestions?