D365 CustomJS - Update Field with AutoNumber Field OnSave - javascript

I have a Autonumber field called 'ID' that generates an ID for a record. I want to set the Primary Name field called 'ID_Name' to this. I am currently using the following JS:
function setName(executionContext) {
formContext = executionContext.getFormContext();
var name = formContext.getAttribute("id").getValue();
formContext.getAttribute("id_name").setValue(name);
}
Pretty simple. I get the value of 'ID', assign it to a var called name, then set the value of 'ID_Name' to that var. This triggers OnSave.
This works fine when editing a record. The problem is, this does not work when creating a new record. I assume because at the time the OnSave triggers, Autonumber field 'ID' hasn't generated a value which can be used yet, so ID_Name is set to blank. Of course while editing, ID has a value because the record has already been submitted, so no problems. Is there a way around this issue?

Just set the OOB autonumber option on the primary name field of your entity. No code required and it saves you from duplicating data, which should be avoided when possible.

You can do some workaround using the OnLoad event and trigger an update when there is a mismatch between the two fields (meaning the value has not been saved) but I strongly suggest to don't use this kind of approach.
Ideally this can be solved using a synchronous plugin inside a post operation, at this moment in the pipeline the id field should be filled (you didn't mention if you are using the OOB autonumber or something different to generate the content of this field) and you can trigger the update inside a plugin.

Related

Trying to grab the 'save' from a dropdown option from a clone in Bootstrap and save the input to Local Storage

trying to save the input provided from the user in an input form that is cloned, so i want it to work for all the 'save' options, keep that text in local storage, and lastly to maintain that text in the input form even when the user refreshes the page, i've tried a bunch of different ways at my wits end, ha. here is fiddle: https://jsfiddle.net/dalejohn33/24u3vxmp/13/
$save.click((f) => {
var task = $("input:selected").val();
var getTask = JSON.parse(localStorage.getItem("input"));
var setTask = JSON.stringify(localStorage.setItem("input"));
console.log("text input has been saved");
}
thanks for any help!
There are some tasks here:
Get the right input's value
var task = $(f.target).closest('.dropdown-menu').next().val();
$(f.target).closest('.dropdown-menu') gets you to the dropdown and the input is next() to it.
localStorage.setItem accepts 2 arguments: (1) The key name (2) The data (You pass only the key).
In order to set the input's value to the value stored in localStorage, you need to store each input as a different key (not just input).
You need to iterate the inputs and set its value from localStorage.
Since the value you store is string, you don't need to stringify / parse.
Another point is the inconsistency name and id. The first element (the one you clone later) has none. Also the id and name are different values because you increase it between (length++)
https://jsfiddle.net/moshfeu/kw8jfg1m/32/

Issue with record.setFieldValues() in SuiteScript Emptying Field Values

Ok, so I have a user event script attached to a custom record. One of the fields on this custom record is a select field for item records. In the user event script, it's getting the value of this field, checking the item options on the selected item. As it runs through the values, it checks to see if it's missing certain values and adds them if necessary. The problem I'm having is that it's ultimately setting the item options field to blank. I've tried both with loading the record, setting the values, then saving, and also by trying to just set a single value with nlapiSubmitField(). The outcome is the same both ways. Here's a quick rundown of the code:
var itemId = customRec.getFieldValue("custrec_item_field");
var itemRec = nlapiLoadRecord("noninventoryitem", itemId, { recordmode : "dynamic" });
var optArray = [ "CUSTCOL_OPT1" , "CUSTCOL_OPT2" , "CUSTCOL_OPT3" , "CUSTCOL_OPT4" ];
itemRec.setFieldValues("itemoptions", optArray);
nlapiSubmitRecord(itemRec, true, true);
Now, a few months back I was certain this was working correctly, and if I apply similar login to a user event BeforeSubmit function when the item record saves, everything works as intended. I'm sure I could get this to work by triggering an edit on the item record within a Suitelet called from the original user event, but that seems ridiculous. There are no errors encountered unless I pass in the item option values through in lower case. Am I missing something? Or am I just going to have to find a way to trigger this outside of this user event function?
There was a flaw somewhere else that was clearing the options out because it mistakenly thought the selected value had changed.

Ruby on Rails: Getting the value from a select form field before it's submitted

I'm trying to get the value of a select form field, before it's submitted and save the value into a variable. I know that you can't do it with Rails only. So JavaScript/Ajax will do the trick. I'm pretty new to Rails, so I hope you can help.
Using Javascript
var form_field_value = document.getElementById("pass-id").value
Using jQuery
var form_field_value = $("#pass-id").val()
Each of the above ways, will save the value in your DOM, so you can access it later. Do make sure, of the scope of the variables, when you are declaring them :)
before it's submitted and save the value into a variable
This is the realm of jquery/javascript -- anything on the client side is JS, not Rails.
Rails/Ruby is basically like PHP -- it runs on the server.
Although #Sudipta Mondal's answer will help, you have to remember to bind your code to an event (typically change):
#app/assets/javascripts/application.js
$(document).on("change", "select#form_item", function(e){
$(this).attr("data-option-value", $(this).val()); // this sets the "data-option-value" to the value
});
Perhaps this isn't the context in which you intended; it should give you the ability to dynamically assign the relative value to the field.

CakePHP omitting to send an input field and changing input value onSubmit?

I am on CakePHP 1.2 at the office and, following my last question, I would like to send the array key of the selected option in a SELECT input instead of sending its actual value. I have tried a few things with the Model::beforeSave() function, without success.
I am aware that the data posted by CakePHP does not include the whole array, but only the selected value.
Here is what the function looks like at the moment:
function beforeSave(){
$this->Post->set('category_id', = array_keys($this->data['Annonce']['category_id']);
# debug($this->data);
}
Would there be a way to store the array keys into an hidden input and changing this input value depending on the user's selected item in the SELECT input, and to also omit sending the user's input but still send the hidden value?
$categories = Set::combine($categories,'{n}.categories.id', '{n}.categories.nom');
This did it for me... CakePHP assigns the array_keys() values automatically to the value field of the input.

How to update a model field without calling the field explicitly in Javascript

In my MVC3 application, I have a view model that I Json encode so I can manipulate it in JavaScript.
So let's say I have the following code:
var model=#Html.Raw(Json.Encode(Model));
Currently, model.Name has value "Name".
What I want to do now is create another JavaScript object "obj" that has a field called "Value". When you change obj.Value, it also changes model.Name.
So I want something like:
var obj=new Object();
obj.Value=model.Name;
So right now, if I change the value of obj.Value, it doesn't also change model.Name. I want that to happen and I'm not sure how I can do it in JavaScript. How do I do implement that?
Why don't just just put the name in a hidden input field. Change the value with whatever JavaScript you choose in the usual way. Then when the form is posted back you can bind to the name value in the controller action, and set the Model name server-side.
What you are attempting is unnecessarily complicated.
If you really wanted to make it work - then on submit put the entire JSON string in a hidden input field. Then again in the controller action bind to the JSON as a string and deserialize it server-side to reconstruct your Model.

Categories