0

I have the scenario, on Asset I have related list of Work Order and On Work Order having related list Work Order Accessories.

If I am on Asset, select any Work Order, and trying to add Work Order Accessories, I am not able to get Work Order ID. I am using WorkspaceAPI to get the data, and developing Lighting component for Adding Work Order Accessories.

enter image description here

sforce

enter image description here

enter image description here

Could anybody help me, how can I get it.

Anita Mehta
  • 101
  • 3
  • Where is the button located? On the related list? – Shailesh Patil Apr 13 '20 at 12:15
  • @ShaileshPatil Yes I have created button in related Object and it is in related List and tried to set data in query parameter like this /lightning/cmp/c__SD_ManageWorkOrderAccessory?workOrderId={!SD_Work_Order__c.Id}, but not setting it up. – Anita Mehta Apr 13 '20 at 12:28
  • Ok... So getting an ID using a sophisticated standard way is not possible currently(Till last time I used). I am adding a hack way to obtain the parentID from the related list. Adding it in the answer. Please check and see if it fits your requirements. – Shailesh Patil Apr 13 '20 at 12:39

1 Answers1

0

Please find the below code. I am not the owner of this code and it is a hack way.

    var value = helper.getParameterByName(component , event, 'inContextOfRef');  
/*This method fetch the parent record Id.*/
    getParameterByName: function(component, event, name) {
        name = name.replace(/[\[\]]/g, "\\$&");
        var url = window.location.href;
        var regex = new RegExp("[?&]" + name + "(=1\.([^&#]*)|&|#|$)");
        var results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }
Shailesh Patil
  • 1,860
  • 12
  • 18
  • Thanks for the reply, in Init should I do it?.. – Anita Mehta Apr 13 '20 at 13:20
  • Yes. You can do. – Shailesh Patil Apr 13 '20 at 13:33
  • Bad Luck, not working for me....is it working for you..... – Anita Mehta Apr 13 '20 at 13:37
  • Yep. Its an old implementation and working fine. Is it possible for you to post the LC code? – Shailesh Patil Apr 13 '20 at 13:38
  • Here it is my init code :doInit : function(component, event, helper, name) { debugger; var workspaceAPI = component.find("workspace"); var pageRef = component.get("v.pageReference");
    var state = pageRef.state; // state holds any query params
    var stateCont =state.ws.split('/'); var objRecId = stateCont[4];<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    – Anita Mehta Apr 13 '20 at 13:41
  • If you could help me where I can put this.. – Anita Mehta Apr 13 '20 at 13:42
  • Hi Anita... Could you try the 2nd solution listed here: https://salesforce.stackexchange.com/questions/223766/lightning-new-case-action-override/244382#244382 – Shailesh Patil Apr 13 '20 at 14:01
  • I am signing out of the system. May be tomorrow I can provide you with the solution if the above link didnt help you. – Shailesh Patil Apr 13 '20 at 14:01
  • In state response I am getting this ,{"uid":"1586792012409","ws":"/lightning/r/Asset/02i3K0000005AaeQAE/view"}, but i need which work order clicked to create new Work Order Accessories.I am facing issue there.Poated one more question .https://stackoverflow.com/questions/61191341/how-to-set-data-in-query-parameter-in-url-of-custom-button-salesforce – Anita Mehta Apr 14 '20 at 04:59
  • Hi Anita... The solution of the link is working for me. Are you clicking on the button in the related list of the Work Order or Asset? – Shailesh Patil Apr 14 '20 at 05:20
  • On Asset I have related List Work Order and if I select any work order, there I have Work Order Accessories related list, and this button is on Work Order. So, If I am creating directly from Work Order Object, it is working fine. But If I go to Asset, select any work order and then try to create the Work Order Accessory, then I am not getting the work order Id. Here I am facing the issue. I have uploaded one more image where I have selected Asset, then Work order and on that I have Work Order Accessories related List.Here it is getting failed. – Anita Mehta Apr 14 '20 at 05:54
  • Hi Anita.. You are going correct way. No need to change the code. Could you please check what you are getting in the "addressableContext.attributes.recordId" using console? It should be a Work Order ID even if you get Asset info in the State. – Shailesh Patil Apr 14 '20 at 06:15
  • Hi Shailesh, var pageRef = component.get("v.pageReference");
    var state = pageRef.state; // state holds any query params console.log('Page Reference Data'+ JSON.stringify(pageRef)); console.log('State Data'+ JSON.stringify(state)); console.log('State Data'+ JSON.stringify(pageRef.state.c__recordId)); console.log('State Data'+ JSON.stringify(pageRef.state.recordId)); tried this way record id is coming undefined. <aura:attribute name="recordId" type="String" />
    – Anita Mehta Apr 14 '20 at 06:42
  • 1
    var pageRef = component.get("v.pageReference"); var state = pageRef.state; // state holds any query params var base64Context = state.inContextOfRef; console.log("^^^^^^^^^^^^^^^State^^^^^^^^^^"+JSON.stringify(state)); if (base64Context.startsWith("1.")) { base64Context = base64Context.substring(2); } var addressableContext = JSON.parse(window.atob(base64Context)); console.log("^^^^^^^^^^^^^^^^^^^^^^^^^"+addressableContext.attributes.recordId); – Shailesh Patil Apr 14 '20 at 06:59
  • Paste above code in the init method and see what you get in ^^^ log. – Shailesh Patil Apr 14 '20 at 06:59
  • I have tried yesterday, var base64Context = state.inContextOfRef; this is coming as undefined for me....don't know what is wrong that I am doing. – Anita Mehta Apr 14 '20 at 07:31
  • Ok... Have you added lightning:isUrlAddressable interface in your LC code? <aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId,force:lightningQuickAction,lightning:actionOverride, lightning:isUrlAddressable" access="global" > – Shailesh Patil Apr 14 '20 at 07:45
  • I am able to do with passing parameter in irl with "c__", which I was missing /lightning/cmp/c__SD_ManageWorkOrderAccessory?c__workOrderId={!SD_Work_Order__c.Id}, it is working now. Thanks Shailesh for the help. – Anita Mehta Apr 14 '20 at 08:22
  • Welcome! Glad it worked! :) – Shailesh Patil Apr 14 '20 at 11:33