Page 1 of 2

Related Documents

Posted: 21 Sep 2017
by TonyDuke
I have added a Related Documents item to a New form, following all the guides, I have it setup so I can drag and drop documents into the viewer and it uploads the document to the document library, what I can't do is get the fields to auto populate, my code is below

Code: Select all

var ContactName = fd.field('Contact_x0020_Name').value();
var CompanyName = fd.field('Company_x0020_Name').value();
var RelatedOpportunity = fd.field('Related_x0020_Opportunity').value();
var RelatedAddress = fd.field('Related_x0020_Address').value();
fd.updateDroppedDocuments($('.emailUpload'), {
	Activity_x0020_Item_x0020_ID: '{CurrentItem}',
	Contact_x0020_Name: ContactName,
	Company_x0020_Name: CompanyName,
	Related_x0020_Opportunity: RelatedOpportunity,
	Related_x0020_Address: RelatedAddress
});
The internal names in both lists are identical, the only thing that happens is the fields Activity Item ID has a -1 entered.

Thanks

Re: Related Documents

Posted: 21 Sep 2017
by TonyDuke
Sorry I have now read this guide - https://spform.com/javascript-framework ... ctionality and got the functionality working, in a fashion -

Code: Select all

fd.updateDroppedDocuments($('.emailUpload'), function(listItem) {
	//get the values from the Form fields:
	var ContactName = fd.field('Contact_x0020_Name').value();
	var CompanyName = fd.field('Company_x0020_Name').value();
	var RelatedOpportunity = fd.field('Related_x0020_Opportunity').value();
	var RelatedAddress = fd.field('Related_x0020_Address').value();
	//set the values on uploaded document:
	listItem.set_item('Company_x0020_Name', CompanyName);
	listItem.set_item('Contact_x0020_Name', ContactName);
	ListItem.set_item('Related_x0020_Opportunity', RelatedOpportunity);
	listItem.set_item('Related_x0020_Address', RelatedAddress);
	listItem.update();
});
This now sets the fields I want to set but how can add the relationship back to the parent i.e.
Parent: '{CurrentItem}',

I don't see where this would fit in the function.

Thanks

Re: Related Documents

Posted: 22 Sep 2017
by Nikita Kurguzov
Hello, Tony!
I am glad that you've found the article which helped you here. In case some of fields are mandatory for the uploaded document, don't forget to check it in in the code.

As for Parent field, you can just include additional code for it, completely separate from the rest:

Code: Select all

fd.updateDroppedDocuments($('.emailUpload'), {
  Parent: '{CurrentItem}'
});

Re: Related Documents

Posted: 25 Sep 2017
by TonyDuke
Hi Nikita,

I'm still struggling a little with this one, I have now added the following code: -

Code: Select all

// Add Parent ID to uploaded email
fd.updateDroppedDocuments($('.emailUpload'), {
  Sales_x0020_Activity_x0020_Lookup: '{CurrentItem}'
});
The Sales Activity Lookup is the field in the Document Library I wish to contain the Parent ID

I have opened the New form of the Document Library in Forms Designer and added: -

1) The Sales Activity Lookup column and set CSS to 'parent-field'

2) A HTML with CDATA set to False as follows -

Code: Select all

<div id="_fd_parent_temp">
<asp:HiddenField runat="server" ID="_fd_parent_tempField" __designer:bind="{ddwrt:DataBind('i','_fd_parent_tempField','Value','Load','ID',ddwrt:EscapeDelims(string(@ID)),'@_fd_parent_temp')}" />
</div>
 
3) The Following code into the JavaScript Editor -

Code: Select all

if (window != window.top && window.top.fd) {
  // the form is opened in a dialog from the parent form
  var parentId = parseInt(window.top.GetUrlKeyValue('ID'));
  if (parentId) {
    // the form is opened from the Edit form
    fd.field('Sales_x0020_Activity_x0020_Lookup').value(parentId);
  } else {
    // the form is opened from the New form
    $('#_fd_parent_temp > input').val(window.top.fd._tempParentId());
  }
   
  $('.parent-field').hide();
}
After all of the above the field Sales Activity Lookup in the document library remains blank when a document is uploaded using the related items viewer?

Help with where I'm going wrong appreciated.

Thanks

Re: Related Documents

Posted: 25 Sep 2017
by Nikita Kurguzov
Hello, Tony!
Please, refer to the correct article - https://spform.com/documentation/related-documents
You seem to follow instruction for Related Items, not Related Documents. You don't need all this code, just this:

Code: Select all

fd.updateDroppedDocuments($('.related-docs'), {
  Parent: '{CurrentItem}'
});
Please, take note, that the Parent Lookup field will appear Empty on the New Form, until the item is saved! You will see message Linking Items when you click Save and when you open the item again in Edit/Display Form, you will see that the documents are binded properly.

Re: Related Documents

Posted: 26 Sep 2017
by TonyDuke
Hi Nikita,

That's, great, I have that working now, I get the message "Linking Items" and when I check the document library the Lookup Column now has the correct data. One quick question, ordinarily on Save the user would be taken back to the List however following the "linking Items" the user is taken to the Site Home, is there a way to hijack this?

Thanks again

Re: Related Documents

Posted: 26 Sep 2017
by Nikita Kurguzov
Hmm, that's strange as it shouldn't happen. There must be something causing it, but I wasn't able to replicate this behavior. Can you maybe show me some screenshots of the Form prior to saving? Also, is there any additional code on the Form?

Re: Related Documents

Posted: 28 Sep 2017
by TonyDuke
Hi Nikita,

Ok Screen shots attached, as per my other post the related items viewer looks empty as on rename the document disappears, but this occurred before I added that code.

There is some other code but nothing that should effect it and prior to adding the above code it worked ok. But as there isn't much all the code is below: -

JavaScript editor: -

Code: Select all

/*INITIAL SETUP*/


// Hides required fields and items
$('.hiddenFields').hide();
$('.emailUpload').hide();


// Set Type of Activity field to "Send an Email"
fd.field('Title').value('Email');
fd.field('Task_x0020_Type').value('Email');
fd.field('Status').value('Completed');

// Add Parent ID to uploaded email for retrieval
fd.updateDroppedDocuments($('.emailUpload'), {
  Activity: '{CurrentItem}'
});

// Update Uploaded Email for Relevant fields
fd.updateDroppedDocuments($('.emailUpload'), function(listItem) {
	//get the values from the Form fields:
	var ContactName = fd.field('Contact_x0020_Name').value();
	var CompanyName = fd.field('Company_x0020_Name').value();
	var RelatedOpportunity = fd.field('Opportunity_x0020_Name').value();
	var RelatedAddress = fd.field('Address_x0020_Name').value();
	//set the values on uploaded document:
	listItem.set_item('Company_x0020_Name', CompanyName);
	listItem.set_item('Contact_x0020_Name', ContactName);
	listItem.set_item('Opportunity_x0020_Name', RelatedOpportunity);
	listItem.set_item('Address_x0020_Name', RelatedAddress);
	listItem.set_item('Document_x0020_Type', "Customer Email");
	listItem.update();
});

fd.updateDroppedDocuments($('.emailUpload'), function(listItem) {
	//new name for the file:
   	var prefix = Date.now() / 1000 | 0;
   	var currentName = listItem.get_fieldValues().FileLeafRef;
    var newName = prefix + currentName;
    //file name with the current directory:
    var newFileName = listItem.get_item('FileDirRef') + '/' + newName;
 	//finally, renaming the file after all the code:
    listItem.get_file().moveTo(newFileName);
});


/*CALLS*/


// Calling Functions when the user changes values
fd.field('Company_x0020_Name').change(clearOnCompanyChange);
fd.field('Company_x0020_Name').change(showemailUpload);
fd.field('Contact_x0020_Name').change(showemailUpload);

/*FUNCTIONS*/


// Function to Clear Contact Name, Related Opportunity to and Related Address on Company Name Change
function clearOnCompanyChange () {
	fd.field('Contact_x0020_Name').value('');
	fd.field('Opportunity_x0020_Name').value('');
	fd.field('Address_x0020_Name').value('');
}

// Function to show Email Upload
function showemailUpload () {
	if (fd.field('Company_x0020_Name').value() != '') {
		if (fd.field('Contact_x0020_Name').value() != '') {
			$('.emailUpload').show();
		} else {
			$('.emailUpload').hide();
		}
	}
}
And OnClick of the Save button

Code: Select all

fd.onsubmit(function() {
	var companyName = fd.field('Company_x0020_Name').value();
	var contactName = fd.field('Contact_x0020_Name').value();
	var subject = fd.field('Title').value();
	
	// Company Name Validation
	if (companyName == '') {
		alert("A Company Name is required");
	return false;
	}
	
	// Contact Name Validation
	if (contactName == '') {
		alert("A Contact Name is required");
	return false;
	}
	return true;
});
Again help appreciated.

Thanks

Re: Related Documents

Posted: 28 Sep 2017
by Nikita Kurguzov
Can't see anything bad, except for the Save button OnClick code. I do not think it would cause this issue, though it might, so I recommend moving fd.onsubmit block inside JavaScipt editor. It will work just fine from there and won't cause any issues for sure.

fd.onsubmit is only activated when you click save button, so it is a bit redundant to call it from the save button itself, it will work better from JavaScript editor. Tell me if there are any differences after you move the code.

P.S. I've also noticed two similar functions for dropped documents, they can probably be combined into one. Also, it's probably not critical, but it's better to have all operations together. First, change all the fields that you need, update the document and then rename it, it can be just one function.

Alternatively, you can update the document last, you won't see the changes straight away then, but it shouldn't mess up the filtering on the New Form.

Re: Related Documents

Posted: 12 Oct 2017
by TonyDuke
Hi Nikita,

Been a while but I have finally got around to implementing and testing this, I have 3 issues: -

1) Moving the fd.onsubmit block into the JavaScript editor has not fixed the issue of the redirect to site home, however I can live with this.

2) joining the functions as you suggest prevents the updating of the form fields as I require, my code is below, have I done something wrong, I am assuming I have joined the correct 2 functions?

Code: Select all

// Update Uploaded Email for Relevant fields
fd.updateDroppedDocuments($('.emailUpload'), function(listItem) {
	//get the values from the Form fields:
	var ContactName = fd.field('Contact_x0020_Name').value();
	var CompanyName = fd.field('Company_x0020_Name').value();
	var RelatedOpportunity = fd.field('Opportunity_x0020_Name').value();
	var RelatedAddress = fd.field('Address_x0020_Name').value();
	//new name for the file:
   	var prefix = Date.now() / 1000 | 0;
   	var currentName = listItem.get_fieldValues().FileLeafRef;
    var newName = prefix + currentName;
    //file name with the current directory:
    var newFileName = listItem.get_item('FileDirRef') + '/' + newName;
	//set the values on uploaded document:
	listItem.set_item('Company_x0020_Name', CompanyName);
	listItem.set_item('Contact_x0020_Name', ContactName);
	listItem.set_item('Opportunity_x0020_Name', RelatedOpportunity);
	listItem.set_item('Address_x0020_Name', RelatedAddress);
	listItem.set_item('Document_x0020_Type', "Email");
	//finally, renaming the file after all the code:
    listItem.get_file().moveTo(newFileName);
	listItem.update();
});
For now I have kept the 2 functions separate as this way the uploaded document gets both the metadata fields set and is then renamed. Will this cause any major issue do you think?

3) This is the major issue - In normal use this form will almost always be opened in a dialog from another Display form, when opened in a dialog the Parent ID isn't passed through and the "Linking Items" doesn't happen, is there a way around this?

Thanks again