How to programmatically set selected value for an answer in inquirer - javascript

So I'm using inquirer to ask a question from my users via terminal:
var inquirer = require('inquirer');
var question = {
name: 'name',
message: '',
validation: function(){ ... }
filter: function(){ ... }
};
but at the same time let my users pass this field via --name command line argument. (say arg.name)
Question is: How can I tell inquirer to take this arg.name value and use it as if user entered it for the answer of this question.
Note that I DO need the validation and filter to also kick-in for this value. (for example if value passed via --name is invalid, the user is presented with the error message and asked to enter another value)
I'm hoping there is a way to solve this problem without having to hack-around manually calling my validation/filter methods.

You could set up a default value to a variable from arg.name:
let argument = args.name;
const question = {
name: 'name',
message: 'Name?',
default: argument
};
I tried testing this and it works. The user just has to hit enter to use the name passed in the arguments so maybe tell them that in the prompt. Also be aware that your validation will take place AFTER your filters. You just need to make sure whatever your filters are doing will pass validation.

Related

An nlobjSearchFilter contains an invalid operator, or is not in proper syntax: internalid. Error while using record.submitFields

I have tried passing various values to id. Whenever a wrong internalid is passed I am getting a "That record does not exist." but when a valid internal id is passed the script throws "An nlobjSearchFilter contains an invalid operator, or is not in proper syntax: internalid."
var recordID = record.submitFields({
"type": record.Type.SALES_ORDER,
"id":9837,
"values": {
"memo": "MEMOTXT"
}
});
There’s nothing wrong with the code that you’ve shown. It’s likely that this error is caused by another script on the sales order record that isn’t expecting an XEDIT event, which is what record.submitFields() will trigger.
During an XEDIT event, user event scripts only have access to some basic fields about the record and the fields that were submitted. They don’t have access to every field on the record.
If the other script isn’t expecting this scenario, they may be trying to perform an action, but some expected values are null because they’re not available to the user event script.
The record.submitFields method has an optional parameter named "enablesourcing". Try running it with it as false.
var recordID = record.submitFields({
type: record.Type.SALES_ORDER,
id: 9837,
values: {
memo: 'MEMOTXT'
},
options: {
enableSourcing: false, //default is true
ignoreMandatoryFields : true //default is false
}
});
The code is fine, there is a User Event on Salesorder that you need to track down, period

Passing Puppeteer log result into form input field

Using Puppeteer I can extract data and print that data on my Terminal:
Output:
{
name: 'Deep into the Jungle',
price: '$24'
}
I also know I can print only name, using the following code:
console.log(result.name);
Output:
{
'Deep into the Jungle'
}
I'm trying to pass the result.name to my form field on my website. But unsuccessfully.
I already know the form field name and can succesfully put data manually using this:
await page.$eval('input[name=wc_name]', el => el.value = 'my-manually-written-name-goes-here');
Since I know the correct field imput name and I know what value I want to pass, logically something like this should work:
await page.$eval('input[name=wc_name]', el => el.value = result.name);
But it didn't pass anything. A blank.
I have also tried the following:
await page.type('input[name=wc_name]', result.name);
A blank as well, nothing passed.
So my question is:
What I'm missing? I'm not a coder, maybe I'm just missing '' or ""
Is it actually possible to pass the result value (as soon as you grab it) into the input field?
Maybe I need to store the values using fs into the text file first, and only then (using fs again) grab them from the file and pass to the input field on my website?
Need help.
Not sure if this is enough to fix but worth trying. result variable is declared in the Node.js context. To transfer it into a callback evaluated in the browser context, you need to add it as an argument:
await page.$eval('input[name=wc_name]', (el, name) => { el.value = name; }, result.name);

Undo form changes when an exception occurs

In Dynamics CRM 2013 is it possible to revert a field changed on a form when a business process error occurs?
For example:
1. User changes a text field on a form from 'abc' to 'xyz'
2. User clicks save
3. CRM pre-operation plugin validates field, 'xyz' not allowed, exception thrown
4. CRM displays business process error to user, 'xyz' is not allowed
5. The value 'xyz' is still shown in the form
The desired behavior we want is 'xyz' to revert to 'abc' in step 5.
You will need to cache the data first. You can do this OnLoad, e.g. by memorizing the entity's attribute values:
function GetInitialAttributeState() {
var preImage = {};
Xrm.Page.data.entity.attributes.forEach(function(field) {
// TODO: for lookup attributes you need to do extra work in order to avoid making merely a copy of an object reference.
preImage[field.getName()] = field.getValue();
});
return preImage;
}
window.preImage = GetInitialAttributeState();
Then you need to perform the save operation through the Xrm.Page.data.save method. Pass a callback function handling errors and resetting the fields, e.g.
Xrm.Page.data.save().then(
function() {
/* Handle success here. */
window.preImage = getInitialAttributeState();
},
function() {
/* Handle errors here. */
Xrm.Page.data.entity.attributes.forEach(function(field) {
if (field.getIsDirty()) {
field.setValue(preImage[field.getName()]);
}
});
});
It is not possible to reset the form's fields this way using the save event, because it kicks in before the actual save operation, never after it.
Why let the user save the record at all?
You could use a Business Ruel to validate the field, and set an error condition against the field for values you don't "like". The error condition will persist and prevent them saving the record until they change the value. The error message can give them some explanation as to why their value is not valid.
Obviously the validation you can do in a Business Rule is limited, but your example does not make it clear on what basis we match "xyz" as a "bad" value.

How to know which field is invalid in knockout validation?

I have a rather big knockout model and I want to validate all nested models in it:
self.errors = ko.validation.group(self, { deep: true });
Validator found an error:
> self.errors()
["This field is required."]
I don't know which field of my huge model is invalid. How can I find out it?
I guess you should be looking for something like this
// Getting errors
var errors = ko.validation.group(this, {
deep: true,
observable: false
});
// New method: getting extended details
var details = errors.getDetails();
for (var i = 0; i < details.length; i++) {
var d = details[i];
/*
Every element contains the following fields:
"observable" - a reference to the target observable.
"error" - the error message.
"rule" - the name of the failed validation rule.
"data" - an object that contains extension data (provided via "extend" method) for every rule. E.g. "data.required == true".
*/
}
PS: You need to add few lines in your validation file to make getDetails() work i.e which may not be there in validation script file you have .(check reference link & check code)
Reference Here and credits to volpav it helped me long back .
Just incase if someone looking for working sample check here

Passing variables between "pipes" in Gulp

I'm trying to write a gulp tasks that takes some user input via the gulp-prompt plugin. But I'm having trouble passing that input along to other eg:
gulp.task('userinput', function(){
var myVar = 'MONKEY';
gulp.src('./templates/_component.*')
.pipe(prompt.prompt([
{
type: 'input',
name: 'userInput',
message: 'Say something'
}
], function(res){
myVar = res.userInput;
}))
.pipe(prompt.confirm('You said ' + myVar));
});
Assuming I enter hello at the prompt, I was expecting the confirmation to say You said Hello, but it says You said MONKEY.
Is this possible with Gulp?
The issue here is that you are creating the second prompt ('You said ' + myVar) before the first prompt has been executed:
Set myVar to 'MONKEY'
Create streams
Create src stream, which is asynchronous
Create first prompt, and add it to the src stream
Create second prompt using current value of myVar, and add it to the first prompt stream
Only now are the streams executed processed
Load sources
Run first prompt, set the myVar
Run the second prompt using previously generated message
The only solution if you want to keep it all as a single stream is to use the variable within something that allows for a closure (function). Some plugins already accept a closure as an argument, but most don't.
One solution to wrap a stream in a closure that would work here is gulp-tap, which isn't designed for this scenario specifically, but should work. it looks like this:
var tap = require('gulp-tap');
//...
gulp.task('userinput', function(){
var myVar = 'MONKEY';
gulp.src('./templates/_component.*')
.pipe(prompt.prompt([
{
type: 'input',
name: 'userInput',
message: 'Say something'
}
], function(res){
myVar = res.userInput;
}))
.pipe(tap(function(file, t) {
// format is t.through(stream-function, [arguments...])
return t.through(prompt.confirm, ['You said ' + myVar]);
});
});
Because this is wrapped in a closure, and evaluated for each file, it will pick up the current value for the variable. However, because it works on each file, you'll see the prompt once for each file processed.
An better solution would be to separate your task into multiple, dependent tasks. That would look something like this:
var myVar = 'MONKEY';
gulp.task('userinput1', function(){
return gulp.src('./templates/_component.*', {read: false})
.pipe(prompt.prompt([
{
type: 'input',
name: 'userInput',
message: 'Say something'
}
], function(res){
myVar = res.userInput;
}));
});
gulp.task('userinput', ['userinput1'], function() {
return gulp.src('./templates/_component.*')
.pipe(prompt.confirm('You said ' + myVar));
});
Now the first task (userinput1) will run and complete before the second one is processed (userinput2), so the variable will be set correctly.
NOTE: Make sure you return the stream from your tasks, otherwise they are processed synchronously, and your variable won't get set.
Finally, it might make more sense to forgo the gulp-prompt task altogether, because it doesn't really have much to do with the stream. You'd probably be better off using straight Node JavaScript within your task to gather the user's input (preferably in a synchronous manner), then processing your files in a gulp-stream after that.

Categories