Passing a variable from a child form

Dear community,

I am trying to open a dialog in order to create a new child item in a sublist (child list). All works well, I get the variable back, but for some reason the partnerContactLookup is not found in the parent list.

This is my JS for the Parent list:

fd.spRendered(function () {
    window.openPartnersKontaktyForm = () => {

        const listId = '{e48958b0-4617-4075-93a6-e8daef445ee2}';
        const siteUrl = 'https://corwin.sharepoint.com/sites/DMS';
        const newFormUrl =
`${siteUrl}/_layouts/15/listform.aspx?PageType=8&ListId=${encodeURIComponent(listId)}`;

        const partnerLookup = fd.field('Partner').value; // {LookupId, LookupValue}
        const IDPartnera = partnerLookup?.LookupId;

        Dialog.open(
            newFormUrl, {
            IDPartnera
        }, 
           
            async(saved, payload) => {
            if (saved && payload?.newItemId) { // child sends {ID: newItemId}
                console.log('New contact ID:', payload.newItemId);
                const contactId = payload.newItemId;
                await addNewContact(contactId); // ⬅️ correct invocation
            }
        }, {
            width: 1000,
            height: 900,
            title: 'Create related record'
        });
    };

    /* Helper */
    async function addNewContact(contactId) {
        const lookup = fd.field('PartnerContactLookup');
        if (!lookup) {
            console.error('Control “PartnerContactLookup” is missing or mis-named.');
            return;
        }
        await lookup.refresh();
        lookup.value = {
            Id: contactId,
            LookupId: contactId
        };
    }
});

This is the JS for child list:

fd.spRendered(() => {
  if (Dialog.getHost() !== window) {
    const args = Dialog.getArgs();
    if (args?.IDPartnera) {
      fd.field('IDPartnera').value = args.IDPartnera;
      fd.field('IDPartnera').disabled = true;
      // fd.field('IDPartnera').value = String(args.IDPartnera);
    }
  }
});

fd.spSaved(result => {

  
  const payload = {
    newItemId: result.Id,
    contactName: fd.field('Title').value
  };

 
  Dialog.close(true, payload);
});

I can see this in the console:
New contact ID: 698
Control “PartnerContactLookup” is missing or mis-named.

Any other action that would use the column fails.

Hello @Juraj.Sako,

Welcome to the Plumsail Community!

What should this code do exactly?

    async function addNewContact(contactId) {
        const lookup = fd.field('PartnerContactLookup');
        if (!lookup) {
            console.error('Control “PartnerContactLookup” is missing or mis-named.');
            return;
        }
        await lookup.refresh();
        lookup.value = {
            Id: contactId,
            LookupId: contactId
        };
    }

Is 'PartnerContactLookup' the internal name of the field? Is it present on the form? Please share the screenshot like so:

Hi Margo,

Thank you for coming back to me :slight_smile:

The code should check for the lookup field in the form and if it is there it should set its value with the variable passed on save - from the child list.

PartnerContactLookup is the name of the field and is present in the parent form:

@Juraj.Sako,

Thank you for the screenshot.

This is Lookup control, not a SharePoint field. So you need to update how you address it in the code:

fd.control('PartnerContactLookup')

@Margo,

I am sorry for such an obvious solution to my problem.

Thank you, worked perfectly.

1 Like