Parsing out muiltipicker in javascript - javascript

I have created a form that pulls a person's (peoplepicker) office location automatically from AD via SharePoint's UPS and populates it in the entry field. What I'm having trouble with is pulling multiple individual's office locations and having that parse each individual's respective office location to place that in the field as well. Any ideas or help would be greatly appreciated.
Here is the form, and as you can see it can populate one person's office, but not the other individuals:

After further research, I want to give a thanks to the author of this useful JavaScript SharePoint library: http://spjsfiles.com/index.php?dir=SharePoint%20JavaScripts/spjs-utility/
This library gets a user's information from a SharePoint Master list. You get this with the following function: function fillFieldDemo(){
setFieldValue('Date', '1/1/2017');
setFieldValue('MM','Boston;London');
var userInfo = getUserInfo_v2(_spPageContextInfo.userId);
setFieldValue('Person', userInfo.Name);
setTimeout(function(){
var title = getFieldValue('Person');
setFieldValue('Title', title);
}, 2000);
}
_spBodyOnLoadFunctionNames.push("fillFieldDemo");
Now you can find the users' name, office, etc.
In order to parse through multiple names in an input text box and pull their offices respectively, you can alter this function, like so:
function fillFieldDemo() {
$('.button').on('click', function() {
var subjects = $('.ms-entity-resolved');
var offices = [];
for (var i = 0; i < subjects.length; i++) {
var s = $(subjects[i]).prop("title");
var print = $('.name').val(s);
var userInfo = **getUserInfo_v2**(s);
for (var key in userInfo) {
var value = userInfo.**Office**;
}
offices.push(value);
}
var input = $('.parse').val(offices);
});
}
_spBodyOnLoadFunctionNames.push("fillFieldDemo");
This will allow a user to input multiple names in an input text box and it will parse the names and grab the offices of each of those individuals.

Related

Why won't the makeCopy portion of my script execute properly in Google Apps Script?

I am a very novice coder and am trying to accomplish the following using a Google Form:
Rename file uploaded by user based on name defined by combination of form fields
Create a copy of the uploaded file to a specific folder in GDrive, based on answer to particular form question
So far, I have managed to get Part 1 working, but Part 2 doesn't seem to function properly (no error message, just no action). Anyone able to guide me where I'm going wrong?
function fileRename() {
var form = FormApp.getActiveForm()
// returns the total number of form submissions
var length=form.getResponses().length;
//retrieve fileID of document uploaded by user in Question 6 of the form (i.e. Index 5)
var id=form.getResponses()[length-1].getItemResponses()[5].getResponse();
//getResponses()[length-1] retrieves the last form response, accounting for the fact that the first index is zero and hte last length-1
//gets the form answers used to concatenate the file name
var fileUploadEntity=form.getResponses()[length-1].getItemResponses()[0].getResponse();
var fileUploadDate=form.getResponses()[length-1].getItemResponses()[3].getResponse();
var fileUploadType=form.getResponses()[length-1].getItemResponses()[1].getResponse();
//accesses the uploaded file
var file=DriveApp.getFileById(id);
var name = file.getName();
//changes the file name
var name = fileUploadEntity+'_'+fileUploadDate+'_'+fileUploadType
file.setName(name);
//creates a copy and saves it to the relevant regional shared drive depending on which array the entity belongs to, using its four-letter identifier
var APAC = ["WRAU", "WRNZ", "WRSG", "WRMY", "WRHK"];
var NORAM = ["WRCA", "WRCC", "WRCW", "WRUS"];
var MEA = ["WRKE", "WRUG", "WRSO", "WRSA", "WRRW", "WRTZ", "WRZW"];
var LATAM = ["WRMX"];
var EEA = ["WRBE", "WRUK"];
var folderAPAC = DriveApp.getFolderById('1IKIDSEEGHf802WaF4l4ntN9uiUO5jJpa');
var folderNORAM = DriveApp.getFolderById('1BitldN3Uw7453wxnnI1X5PUmbmTiQn5O');
var folderMEA = DriveApp.getFolderById('18tWR1C-mdO7moAtktOHJsvXjx_V0kdg0');
var folderLATAM = DriveApp.getFolderById('1cG0iPocn3KyXK8XgaxnZNWVU-HKJ97dX');
var folderEEA = DriveApp.getFolderById('1N8tB8AjMkR7gRarcwd4NYmry_wh0WVkY');
if (fileUploadEntity.indexOf(APAC)>-1) {
file.makeCopy(name, folderAPAC);
}
else if (fileUploadEntity.indexOf(NORAM)>-1) {
file.makeCopy(name, folderNORAM);
}
else if (fileUploadEntity.indexOf(LATAM)>-1) {
file.makeCopy(name, folderLATAM);
}
else if (fileUploadEntity.indexOf(MEA)>-1) {
file.makeCopy(name, folderMEA);
}
else if (fileUploadEntity.indexOf(EEA)>-1) {
file.makeCopy(name, folderEEA);
}
}
You code is using indexOf the wrong way.
Instead of
fileUploadEntity.indexOf(APAC)
try
APAC.indexOf(fileUploadEntity)
Do the same or the other places where indexOf is used
Reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

How to automatically add questions in a Google Form based on columns on Google Sheets? [duplicate]

I'm new with Google scripts and now I have to make a form with a list of choices. These choices should be picked up from the Google sheet.
So the first question is how to chose only unique values from some range of my spreadsheet?
The second is how to pass this list so that they will be the items in the list?
The code I've tried is:
function getMembranesList() {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/......");
var itemList = ss.getSheetByName('Answers').getRange("Q1:Q").getValues();
var form = FormApp.getActiveForm();
var item = form.addListItem()
item.setTitle('test question');
item.createChoice(itemList);
}
Looking at the methods available to populate the ListItem, you have to choose one and set your data up so it matches the expected input. For my example, I chose the setChoiceValues method, which looks for an array. So I have to manipulate the items into an array.
One thing the getRange.getValues() method does NOT get you is how many non-blank items are returned in the list. I used this quick way to get a count of those items, so I have a maximum bound for my loops. Then, I formed the itemArray and added only the non-blank items to it.
After that, it's just a matter of creating the ListItem and adding the values:
function getMembranesList() {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/...");
var itemList = ss.getSheetByName('Answers').getRange("Q1:Q").getValues();
var itemCount = itemList.filter(String).length;
var itemArray = [];
for (var i = 0; i < itemCount; i++) {
itemArray[i] = itemList[i];
}
var form = FormApp.getActiveForm();
var item = form.addListItem();
item.setTitle('test question');
item.setChoiceValues(itemArray);
}

CRM Dynamics 2013 Javascript make a field not updatable

Hi I have a scenario where in CRM form onload, I have a function that auto-populates a random generated number. I then open a URL using a Ribbon Button, after that I save the record, My issue when I attempt to save the record the form loads again therefore overrides my random number, which causes data to be corrupt. I have attempted to lock the fiels to readonly after the page loads, but however when the page saves it still ovverrides the value, please see my code below:
function AutonumberGenerator()
{
var uniqueRandoms = [];
var numRandoms = 500000;
if (!uniqueRandoms.length) {
for (var i = 0; i < numRandoms; i++) {
uniqueRandoms.push(i);
}
}
var index = Math.floor(Math.random() * uniqueRandoms.length);
var val = uniqueRandoms[index];
// now remove that value from the array
uniqueRandoms.splice(index, 1);
Xrm.Page.getAttribute("new_opguploadidentifier").setValue(val);
Xrm.Page.getAttribute("new_opguploadidentifier").controls.get(0).setDisabled(true);
}
Please note I am using CRM Dynamics 2013 ,Please help
You could simply check the value. If it's null, fill it. If it isn't, don't.
Someting like this in your onload, where you now fill the field:
if (Xrm.Page.getAttribute("your_random_number_field").getValue() == null) {
AutonumberGenerator();
}

Mozilla Add-on development: Accessing LAST USED or LAST CHANGED fields in password manger

My add-on needs to access stored credentials. In this I want to get the credentials that was changed or used recently. I see that I have LAST USED and LAST CHANGED fields but how do I access those fields in my javascript?
I searched and also tried ( https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/passwords ) but I couldn't get anything.
This is how to get a list of all the currently stored login informations:
var lm = Cc["#mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
var arrOfLogins = lm.getAllLogins();
var numberOfLogins = arrOfLogins.length;
console.info('arrOfLogins:', arrOfLogins);
var myObjs = [];
for (var i=0; i<numberOfLogins; i++) {
var pushObj = {};
pushObj.username = arrOfLogins[i].username;
pushObj.password = arrOfLogins[i].password;
pushObj.hostname = arrOfLogins[i].hostname;
arrOfLogins[i].QueryInterface(Ci.nsILoginMetaInfo);
pushObj.lastUsed = new Date(arrOfLogins[i].timeLastUsed);
pushObj.lastChanged = new Date(arrOfLogins[i].timePasswordChanged);
myObjs.push(pushObj);
}
console.info('myObjs:', myObjs);
If the user has a master password, you have to prompt them to fill it out, otherwise the above iwll fail im pretty sure.

Dynamically construct array based on user input - Javascript/jQuery

I have a list of US regions set up as checkboxes. When a user submits the form, I need an array of associated states. This array is used for comparing to another array in my SQL tables.
Using jQuery or javascript, what's the best way to go about this?
I've got the general idea I believe:
//populate region to state arrays here
$("input[name='region']").each(function () {
if ($(this).is(':checked')) {
// ADD TO SUPER ARRAY OF ALL REGIONS
// (this is where I need help)
}
});
Thanks in advance!!
PS. I tried using the input values var regionValue = $(this).attr('value') to identify each array, but I'm having issues with dynamically named arrays since all my functions are in static classes.
Edited to reflect the new information and incorporating GenericTypeTea's comment:
Assuming the ID of each checkbox is set to the region name, how about:
var regionStates = {};
regionStates['northeast'] = ['AB', 'CD', 'EF'];
regionStates['mountains'] = ['GH', 'IJ', 'KL'];
// etc...
var selectedStates = [];
$("input[name='region']:checked").each(function () {
var region = regionStates[this.id];
for (var i = 0; i < region.length; ++i) {
selectedStates[selectedStates.length] = region[i];
}
});
Using this, selectedStates will contain all the states of all regions that are selected.
Apologies for not knowing any real US state abbreviations.

Categories