Passing values to a child form from two different parent forms

Hello,

I managed to pass values from two different parent forms to a child form in dialog. The problem is both parent forms have a 'Title' field that has different data from one another that I can't change field names at this point because we already have data there. So I'm pretty much passing values from both parent forms to the same child form. For example, Hardware Request parent form's 'Title' field ('Justification' name label) actually is used for 'Purpose' for the child form (Purchase Request form) to pass a value. The other Software Request parent form's 'Title' field ('Link' name label) is used for 'AddressUrl' for same child form (Purchase Request form) to pass a value. When I open the child form in dialog from Hardware Request parent form it takes the Title field data and places it in both the Purpose and AddressUrl fields of the Purchase Request form but I only want it in the Purpose field. The AddressUrl field is supposed to fill only if Purchase Request form is opened from the Software Request form.

I tried to rewrite the code to test for if/else depending on the form ID to transfer the field data but it isn't quite working for me. Can you assist me? Code below.

fd.spRendered(function() {
    var parentForm = window.top.fd;

    if (parentForm.formId == 'bc59a2f3-db60-4938-99d9-xxxxxxxxxxxx') {

        //Set field values with the values from the parent Hardware Request on form load
        fd.field('DateNeeded').value = parentForm.field('DateNeeded').value;
        fd.field('ContactPhone').value = parentForm.field('OwningStaff').value.EntityData.Email;
        fd.field('ItemOne').value = parentForm.field('HardwareType').value[0];
        fd.field('ItemTwo').value = parentForm.field('HardwareType').value[1];
        fd.field('ItemThree').value = parentForm.field('HardwareType').value[2];
        fd.field('ItemFour').value = parentForm.field('HardwareType').value[3];
        fd.field('ItemFive').value = parentForm.field('HardwareType').value[4];
        fd.field('Purpose').value = parentForm.field('Title').value;
        fd.field('QuantityOne').value = '1';
        fd.field('Address').value = parentForm.field('MailingAddress').value;

    } else {

        //Set field values with the values from the parent Software Request on form load
        fd.field('AddressUrl').value = parentForm.field('Title').value;
        fd.field('DateNeeded').value = parentForm.field('DateNeeded').value;
        fd.field('ItemOne').value = parentForm.field('SoftwareRequested').value;
        fd.field('Purpose').value = parentForm.field('PurposeProject').value;
        fd.field('QuantityOne').value = '1';
        fd.field('ApprovalComments').value = parentForm.field('Comments').value;
    }

});

Dear @lso,
Are you sure that's the code that runs? Looking at it, it should work properly. You can add alert() to test it properly, and remove the rest of the code for now:

fd.spRendered(function() {
    var parentForm = window.top.fd;
    if (parentForm.formId == 'bc59a2f3-db60-4938-99d9-xxxxxxxxxxxx') {
        alert('Hardware Request parent');
        fd.field('Purpose').value = parentForm.field('Title').value;
    } else {
        alert('Software Request parent');
        //Set field values with the values from the parent Software Request on form load
        fd.field('AddressUrl').value = parentForm.field('Title').value;
    }
});

Hi @Nikita_Kurguzov ,
I've tested and it keeps calling the 'Software Request parent' alert from both the Hardware Request form parent and Software Request form parent. I'm not sure why as I believe I'm using the correct Hardware Request form ID: bc59a2f3-db60-4938-99d9-xxxxxxxxxxxx. The Software Request from ID is 4dda18f2-f398-40f6-86ca-xxxxxxxxxxxx. I've added the x's at the end to not show both IDs publicly of course. Maybe I'm calling the parent form ID incorrectly?

Thank you

Dear @lso,
Are you sure that the top most form is the correct parent? It will only see one form, that's opened in the top most window.

You can also try comparing the list URL of the parent form, like this:

if (fd.listUrl == '/Lists/ListName'){
  //do stuff
}

Hi @Nikita_Kurguzov ,
I've tried using these 3 calls and still showing me the else call alert for 'Sortware Request parent' even though each time I'm only using the Harware Request form:

  1. if (fd.listUrl == '/Lists/HardwareRequest')
  2. if (fd.listUrl == 'https://mysite.sharepoint.com/Lists/HardwareRequest/AllItems.aspx')
  3. if (fd.source == 'https://mysite.sharepoint.com/Lists/HardwareRequest/AllItems.aspx')

The new Purchase Card Request dialogue form is only called when a pre-created Hardware Request form is edited. So when a new Hardware Request form is created you can't see the embedded Purchase Card Request form. The Purchase Card Request form dialogue only pops up a new Purchase Card Request dialogue when clicked after I click to edit the already made Hardware Request form. Sorry, hopefully this makes sense. The URL at the edit form is https://mysite.sharepoint.com/SitePages/PlumsailForms/HardwareRequest/Item/EditForm.aspx?item=.............%2FAllItems.aspx.

Could this be the reason my code isn't working? Thank you

Dear @lso,
The code is very simple - it only grabs values from window.top - Window.top - Web APIs | MDN

Are you sure you don't have Software Request form opened somewhere in the browser? You can also just check the List URL with alert, like this:

var parentForm = window.top.fd;
alert(parentForm.listUrl);
if (parentForm.listUrl == '/Lists/ListName'){
  //do stuff
}

Hi @Nikita_Kurguzov ,
Thank you, the alert(parentForm.listUrl); line helped! It does show that the parent form is 'Lists/HardwareRequest'. The if (parentForm.listUrl == '/Lists/HardwareRequest') line still showed the alert for the Software Request parent form but when I changed '/Lists/HardwareRequest' to 'Lists/HardwareRequest' it worked to show the alert for the Hardware Request parent. So it seems removing the '/' in the code got it to work. Not sure if this is just my case but maybe this is something to note in the future for the syntax 'Lists/ListName'.

I'm still not sure why the original code if (parentForm.formId == 'bc59a2f3-db60-4938-99d9-xxxxxxxxxxxx') didn't work as Hardware Request was the parent form ID all along but glad if (parentForm.listUrl == 'Lists/HardwareRequest') did. Thank you again for all your assistance!

1 Like