Updating a different child field from Parent form and hiding it

Hiya Again

We have a parent form (Quotes) that has 2 look up fields: Company and Contact
When a new quote is produced, the client can create a new company (using the new company link when a lookup isn't found).

When a new contact is created, it has the same Company lookup, we need to be able to automatically update the company field for the new child contact form using the contents of company field from the parent quote form (that hasn't been saved, doing it from a saved item is trivial, due to your notes).If we are able to get that information, we would then like to hide that filed from the form.

In formsdesigner you would do it with this code, but i don't know the equivalent in Plumsail forms:

var CompanyId = window.top.fd.field('Company').control('data').Id;
var Company = window.top.fd.field('Company').control('data').Title;

//Hide Company field + update it
if (CompanyId){
$('.Company').hide();
fd.field('Company').value(CompanyId + ';#' + Company);

Thanking you in advance!

Hello @Jamal_Smith-Graham!

Please follow the below steps to pass value from the parent form to child.

  • Please define fd globally in the parent form by pasting the code window.fd = fd; in Javascript Editor;
    image

  • Please paste the below code in Javascript Editor in the child Form. Make sure that the internal names of the fields equal to the ones in the code.

fd.spRendered(function() {
    //Set lookup value with the value from the parent on form load
    fd.field('Company').value = window.top.fd.field('Company').value;
    //Hide Company field on form load
    $(fd.field('Company').$parent.$el).hide();
});

Hiya @mnikitina
Unfortunately your code doesn't work, this is my code

image

If I comment out line 6, then the filtering works, if I don't comment it out, nothing is added to the company field (the internal names are Company on parent and child lists) and the filtering doesn't work.

Are you sure fd.field('Company').value = window.top.fd.field('Company').value; is correct? As it seems to break my form :frowning:

Any further help you be greatly appriciated.
Do note that I have tried to do alert(window.top.fd.field('Company').value); and nothing shows at all, usually when nothing is found it will alert a "undefined", but I get no alert at all...

Looking forward to hearing from you soon!

Hello @Jamal_Smith-Graham!

Please use the below code in the chid item.

It will check if the field value available from the top window. So if the form opens not from the parent item, the filtering will still work.

function getFieldValue() {
if (window.top.fd.field('Company').value != null) {
    fd.field('Company').value = window.top.fd.field('Company').value;
	}
}

fd.spRendered(function() {

	fd.field('Company').filter = "Status_x0020__x0028_TXT_x0029_ ne 'Inactive'";
    fd.field('Company').widget.dataSource.read();
	getFieldValue();
});
1 Like

Thanks for the updated script, but unfortuantly this window.top.fd.field('Company').value doesn't return anything :frowning:

This the script I have:

window.fd = fd;
function getFieldValue() {
if (window.top.fd.field('Company').value != null) {
fd.field('Company').value = window.top.fd.field('Company').value;
}
}

fd.spRendered(function() {
//Always hide stuff
$('.AlwaysHide').hide();
//Filter Company so no inactive companies show
fd.field('Company').filter = "Status_x0020__x0028_TXT_x0029_ ne 'Inactive'";
fd.field('Company').widget.dataSource.read();
getFieldValue();
});

fd.spBeforeSave(function(spForm) {
//Update fields before saving form
fd.field('Status_x0020__x0028_TXT_x0029_').value = fd.field('Status').value;
fd.field('Display_x0020_Name').value = fd.field('Title').value;
});

And the Field names are defo correct (Quotes is the parent and Contacts is the child form):

But nothing get returned (if I do a alert), and Company field doesn't get updated, i think the problems is that window.top.fd.field('Company').value doesn't work, any further help would be great:

To add a bit more info, here is the output from the console when I open a new contact window:

EditForm.aspx?item=5&ct=0x01009B234C6D40A40046937D7E8E63E6D97D&rf=%2Fsites%2FCOREaaS-AMPCSCRM%2FLists%2FQuotes&source=https%3A%2F%2Fredacted.sharepoint.com%2Fsites%2FCOREaaS-AMPCSCRM%2FLists%2FQuotes%2FOpen%20Quotes.aspx:1
[Report Only] Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'strict-dynamic' 'unsafe-eval' 'nonce-0c7cc5c5-fdeb-4524-a304-25b856fe8b4e' ". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

VM1597 content_script.js:1 extensions loaded options

{ enabled : true , exceptions : "" , maxlength : 15 , minlength : 7 , protocol : "webclient" , …}

VM1597 content_script.js:1
Force protocol to tel because webclienturl is empty

VM1591 spredirect.js:1
GET https://redacted.sharepoint.com/sites/COREaaS-AMPCSCRM/_api/web/getFileBySe…COREaaS-AMPCSCRM/SitePages/PlumsailForms/forms_Contacts_Item.json')/$value 404

VM1591 spredirect.js:26
Error: Error making HttpClient request in queryable: [404]
at new t (VM1336 spredirect.js:36)
at t.e.handleError (VM1336 spredirect.js:36)
at VM1336 spredirect.js:36
at new Promise (<anonymous>)
at t.e.parse (VM1336 spredirect.js:36)
at VM1336 spredirect.js:36

/sites/COREaaS-AMPCSCRM/SitePages/PlumsailForms/Contacts/Item/NewForm.aspx

VM1637 content_script.js:1 extensions loaded options

{ enabled : true , exceptions : "" , maxlength : 15 , minlength : 7 , protocol : "webclient" , …} VM1637 content_script.js:1

Force protocol to tel because webclienturl is empty VM1646 spform.js:16

TypeError: Cannot read property 'dataSource' of null at eval (eval at e._executeCustomJavaScript (VM1219 spform.js:82), <anonymous>:16:28)
at VM1219 spform.js:16
at Array.map (<anonymous>)
at Function.e.safeRun (VM1219 spform.js:16)
at VM1219 spform.js:92

@Jamal_Smith-Graham,

Please comment out the code fd.field('Company').widget.dataSource.read(); and add console log action to see the returned value from the parent item, so the code will be as below.

Please let me know if window.top.fd.field returned value from the parent item or not, and if filtering working properly.

Thank you!

function getFieldValue() {
if (window.top.fd.field('Company').value != null) {
    fd.field('Company').value = window.top.fd.field('Company').value;
	}
}

fd.spRendered(function() {

	fd.field('Company').filter = "Status_x0020__x0028_TXT_x0029_ ne 'Inactive'";
    //fd.field('Company').widget.dataSource.read();
	getFieldValue();
    console.log(window.top.fd.field('Company').value);
});

Hiya @mnikitina, thanks for getting back to me, so this is the error:

forms.plumsail.com/widget/1.0.7/spform.js:16 TypeError: Cannot read property 'field' of undefined
_ at getFieldValue (eval at e.executeCustomJavaScript (forms.plumsail.com/widget/1.0.7/spform.js:82), :5:19)
_ at eval (eval at e.executeCustomJavaScript (forms.plumsail.com/widget/1.0.7/spform.js:82), :17:2)
_ at forms.plumsail.com/widget/1.0.7/spform.js:16_
_ at Array.map ()_
_ at Function.e.safeRun (forms.plumsail.com/widget/1.0.7/spform.js:16)_
_ at forms.plumsail.com/widget/1.0.7/spform.js:92_
(anonymous) @ forms.plumsail.com/widget/1.0.7/spform.js:16

It is very strange as you can see from above, the field is definitely called Company on both parent and child forms... :sob:

Any further help will be greatly appreciated!

@Jamal_Smith-Graham,

Please send me the code from JavaScript Editor from both parent and child forms to troubleshoot the issue.

Thank you!

1 Like

Parent Code:
//refreshing the Total Cost
var myVar = setInterval(calcTotal, 2000);

function calcTotal() {
	
	var value = fd.control('SPDataTable1').widget.dataItems();
	var total = 0;
	if(value){
  		for (var i = 0; i < value.length; i++){
    		if(value[i].Total_x0020_Sale_x0020_Price_x00){
        	total += parseInt(value[i].Total_x0020_Sale_x0020_Price_x00.replace(/[^0-9.]/g, ""));}
  		}
	}
	fd.field('Total').disabled = false;
	fd.field('Total').value = total;
	fd.field('Total').disabled = true;
}

fd.spBeforeRender(function() {
fd.field('Company').addNewText = "Add new Company";
fd.field('Contact').addNewText = "Add new Contact";
});

fd.spRendered(function() {
//Always hide stuff
$('.AlwaysHide').hide();

// disable the field
fd.field('Total').disabled = true;

//Filter Companies so no inactive companies Show 
var Company = fd.field('Company').value;
fd.field('Company').filter = "Status_x0020__x0028_TXT_x0029_ ne 'Inactive'";	
	//Cascading Lookup for Contacts based on Company
    function filterLookup(v){
    // getting the selected Company (0 if nothing is selected).
    var CompanyId = 0;
    if (v) {
    CompanyId = v.LookupId;
    }

    if (CompanyId) {
        // setting filtration
		 fd.field('Contact').filter = "Status_x0020__x0028_TXT_x0029_ ne 'Inactive' and Company_x0020_ID_x0020__x0028_TX eq " + CompanyId + "";
     
    } else {
        // resetting the filtration
        fd.field('Contact').filter = null;
    }

    fd.field('Contact').widget.dataSource.read();
}

//filter Contacts when form opens
fd.field('Company').ready().then(function(field) {
filterLookup(field.value);
});

//Calculate Total when form opens
fd.field('Total').ready().then(function(field) {
calcTotal();
});
	
//filter Contacts when Company changes
fd.field('Company').$on('change', function(value){
filterLookup(value);
fd.field('Contact').value = null;
});
fd.field('Company').widget.dataSource.read();

});

fd.spBeforeSave(function(spForm) {
//Update fields before saving form
calcTotal();
clearInterval(myVar);
fd.field('Status_x0020__x0028_TXT_x0029_').value = fd.field('Status').value;
fd.field('Company_x0020_ID_x0020__x0028_TX').value = fd.field('Company').value;
fd.field('Contact_x0020_ID_x0020__x0028_TX').value = fd.field('Contact').value;
});

Child Code:
window.fd = fd;
function getFieldValue() {
if (window.top.fd.field('Company').value != null) {
fd.field('Company').value = window.top.fd.field('Company').value;
}
}

fd.spRendered(function() {
//Always hide stuff
$('.AlwaysHide').hide();
//$(fd.field('Company').$parent.$el).hide();
//Filter Company so no inactive companies show
fd.field('Company').filter = "Status_x0020__x0028_TXT_x0029_ ne 'Inactive'";
//fd.field('Company').widget.dataSource.read();
getFieldValue();
console.log(window.top.fd.field('Company').value);
});

fd.spBeforeSave(function(spForm) {
//Update fields before saving form
fd.field('Status_x0020__x0028_TXT_x0029_').value = fd.field('Status').value;
fd.field('Display_x0020_Name').value = fd.field('Title').value;
});

Looking forward to your reply.

@Jamal_Smith-Graham,

To make window.top.fd.field('Company').value work, please add window.fd = fd; in the parent item code in the very top of your code, as you've done it in the child code.

Once fd defined in the parent form, please test if the value passed to the child from parent.

1 Like

That was it!!! thanks again @mnikitina, so helpful once again! :smiley:

1 Like

Please find more detailed instruction on populating fields in a dialog in the 'Passing values from a parent form to a child opened in a dialog' article.

1 Like

Thanks again! @mnikitina

1 Like