Assigning value of field from JavaScript in Oracle Apex - javascript

I am trying to implement JavaScript in header of page of Oracle APEX application.
Fields in the form by default are going to be disabled if customer data exists but it can be trigger to edit mode by clicking on button 'Unlock'. By clicking button EnableItems() is run, but form is not resubmitted it is just js function that run.
function DisableItems()
{
$x_disableItem('P31_TITLE',true);
}
function EnableItems()
{
$x_disableItem('P31_TITLE',false);
}
If form is empty then all fields should be available to edit.
In Javascript >> Execute when Page Loads
I have
PageStart();
Now I need third piece of code so page would know if I want to load Enable or Disable mode. To do that I have
function PageStart(){
if ($x('P31_TEST2').value == "") {
EnableItems();
}
else {
DisableItems();
}
}
The difficulty is in second line of this code where I need to put some flag to assign value to a field or gather value from Apex field which would trigger either first or second function however I am not quite sure what would be the best practice to go around it. Any thoughts highly appreciated.

P31_START_ENABLED: hidden item, value protected = No. Provide the starting value.
For example, here the value is either 'Y' or 'N' to indicate enabled Y/N
function PageStart(){
if($v("P31_START_ENABLED")=='Y') {
EnableItems();
} else {
DisableItems();
};
};
function DisableItems()
{
$s("P31_START_ENABLED", 'N');
$x_disableItem('P31_TITLE',true);
};
function EnableItems()
{
$s("P31_START_ENABLED", 'Y');
$x_disableItem('P31_TITLE',false);
};

Related

Dynamics 365 CRM Plugin - Unable to set two option field NULL in managed solution

I have 2 environments: DEV & TEST. I want to achieve functionality as mentioned as below:
if (fields.email.getValue() == null) {
fields.emailAllowed.setValue(null);
fields.emailAllowed.setRequiredLevel("required");
}
else {
fields.emailAllowed.setRequiredLevel("none");
}
if (fields.phone.getValue() == null) {
fields.phoneAllowed.setValue(null);
fields.phoneAllowed.setRequiredLevel("required");
}
else {
fields.phoneAllowed.setRequiredLevel("none");
}
This JavaScript code is working fine in DEV environment but due to some unknown reason it is NOT saving the NULL value in TEST environment.
To support that, I have added a new CRM plugin in which I have created two handlers:
Create Plugin ==> PreValidation
Update Plugin ==> PreValidation
In both of these plugins, I am using logic as below:
// Gets PreImage & Target Image combined
public void Execute()
{
var combinedTarget = GetCombinedTarget();
if (combinedTarget == null)
{
// log & throw error
}
MapEmailField(combinedTarget);
MapPhoneField(combinedTarget);
}
private void MapEmailField(combinedTarget)
{
// if Email is NULL and EmailAllowed IS NULL
if(combinedTarget.Metadata.Attributes.Email != null && combinedTarget.Metadata.Attributes.EmailAllowed == null)
{
//log & throw error
}
else if(combinedTarget.Metadata.Attributes.Email == null)
{
combinedTarget.Metadata.Attributes.EmailAllowed = null; // To save NULL value into CRM field
}
}
// same method for phone field mapping
// save the record
Now in CRM UI, when I update email field, it automatically sets TRUE value to Phone Allowed field too, even though it is not changed and I have externally set it NULL in CRM plugin. And there are 5 more such fields are getting affected in same way.
The same changes are working fine in DEV environment but not in TEST environment.
NOTE: I have cross verified that all changes of DEV environment are moved into managed solution and it is there in TEST environment.
UPDATE 1:
I tried today debugging the JavaScript change in TEST environment, and through that it updated the records correctly. But when I closed the debugger and test the same scenario with new record, it updated all records again!!
UPDATE 2:
As my JavaScript function is getting called on onLoad event too so that it can set NULL value, but looks like it is not able to find the control or something and that's the reason it is working while debugging but not in regular mode.
Below is the Javascript function which gets executed onLoad event:
function handleAllowedFields(dataField, allowedField) {
return function () {
if (dataField == null || allowedField == null) {
return;
}
if (dataField.getValue() == null) {
allowedField.setValue(null);
allowedField.setRequiredLevel("none");
allowedField.controls.forEach(function (c) {
c.setDisabled(true);
});
} else {
// If dataField is having a value, then contact preference fields
// can have an value as Allow OR DoNotAllow ONLY.
allowedField.controls.forEach(function (c) {
c.setDisabled(false);
});
allowedField.setRequiredLevel("required");
}
}
}
Any help that how can I make my function to wait till all controls gets loaded on the form in onLoad event?
As you aware, CRM Two options is not good enough to store NULL or treat it as a NULL-able boolean field. Either have a global or local optionset (this allows NULL) with two values - this will allow to start the record with a NULL value & no need of javascript or plugin to massage the data every time.
Anyway script will not trigger when data import happens or anything outside the platform like web api calls - so plugin is good choice.
Found the issue.
It's true that Managed solution was unable to save the NULL value for two option type field. While it was able to do that in unmanaged solution.
Alongside with that, I found that onLoad event was not executed after onSave event in JavaScript and this was the reason behind the issue.
Hence, what I did as a solution is I added an onChange event to modifiedon field on the form and it is working like butter.
P.S.: I'm not using OptionSet instead of TwoOptionSet field is so that the default fields gets utilised properly by the customer for every other built in functionality.

How do I get the values I submitted on a form and pass it onto my modal

I have a form in my jsp wherein I enter a value. This is then inserted into a table on my database. However, I would like to ask the user first to confirm his submission. I used this:
function askConfirmation() {
var confirmSubmission = confirm("Do you wish to add this entry?");
if(confirmSubmission) {
return true;
} else {
return false;
}
}
Although this does the task of suspending submission, it's not quite what I had in mind. I tried using a modal but I don't know how to get the values I used from my EL and place it in the modal after the button click. How do I do this?
I am not quite sure how you have your form inputs setup, but you can pass an object representing all your form inputs as an argument into askConfirmation() function and then pass that to confirm(). This will display to the user all the information in the form. Eg.
function askConfirmation(formInput) {
var confirmSubmission = confirm(formInput);
if(confirmSubmission) {
return true;
} else {
return false;
}
}
So in this way, you can access members/fields in the object and format them accordingly. You can also use jquery dialog() function to achieve this if you don't like the lnf of the window. The bottomline is, just pass the object that represents your inputs value into whatever that is used in displaying to the user for further confirmation. Hope this helps!
As Felipe T mentioned your values are already rendered you just need to get them with javascript here's an example using bootstrap modal https://jsfiddle.net/tf021so9/6/
-
However, I prefer using a confirmation dialog plugin in such cases like this one :https://myclabs.github.io/jquery.confirm/
-

CRM form edit using Java Script

I am trying to modify a CRM form for an entity called "property".
I want to basically write an IF ELSE statement to do enable/disable a field on the form when the "new property" tab is clicked by the end user on the CRM interface.
I already have written the javascript code but I am not sure of the syntax to create a new property (a new record within the entity)?
I also tried to ID a new property record by the fact the a new property record will have the name field blank.
Please see a sample of the code below:
if(crmForm.all.new_name == null)
{
//Enable field
crmForm.all.new_interestrate.Disabled = false;
}
else
{
//Enable field
crmForm.all.new_interestrate.Disabled = true;
}
I have set this script on the OnLoad section of the form but it is not running. I postulated that this might be because I am clicking create a new property rather than loading an already existing property?
Any feedback would be appreciated.
Thanks
To anyone reading this, I solved my issue, the flaw was that the field wasn't actually null. I basically rectified this problem using the initial statement
var value = crmForm.all.new_name.datavalue
if (value < 1)
{"do something";}
This worked for my case as no name value was less than 1.

Debugging script Netsuite suitescript in Chrome exception

So I am trying to add a button to the Sales Order form in netsuite that validates certain fields based on what you entered in previous fields. I am having trouble testing and debugging this in google chrome in netsuite. First, here is my code: I am adding the button that calls this function within the client script record.
function vsoeValidate(){
var calc = nlapiGetFieldValue('custbody_cv_vsoe_calculation');
calc = nlapiGetFieldValue('custbody_cv_renewal_rev_amount') - (nlapiGetFieldValue('custbody_cv_vsoe_cola') * nlapiGetFieldValue(1-'custbody_cv_vsoe_partner_disc')) - (nlapiGetFieldValue('custbody_cv_vsoe_bts_fees') * (1-nlapiGetFieldValue('custbody_cv_vsoe_partner_disc'))) /
(nlapiGetFieldValue('custbody_cv_vsoe_software_amt') * (1- nlapiGetFieldValue('custbody_cv_vsoe_multiyear_disc')));
nlapiSetFieldValue('custbody_cv_vsoe_calculation', calc);
var display = nlapiGetFieldValue('custbody_cv_vsoe_calculation_disp');
var bucket = nlapiGetFieldValue('custbody_cv_vsoe_bucket');
if(bucket === 'X'){
return false;
}
if(calc > (nlapiGetFieldValue('custbody_cv_vsoe_bucket.custrecord_cv_vsoe_maintenance_rate') *1.15) || calc < ('custbody_cv_vsoe_bucket.custrecord_cv_vsoe_maintenance_rate'*0.85)){
display = '<div style="; background-color:red; color:white;font-weight:bold;text-align:center">Out of bounds</div>';
return true;
}
else{
display = '<div style="; background-color:green; color:white;font-weight:bold;text-align:center">In bounds</div>';
return true;
}
}
when I click the button I get the error TypeError undefined is not a function.
I am really not sure where to go from here, is it because the logic inside vsoeValidate is not right or am I using the wrong type of function? Any help would be great thank you!
Update: here is the screenshot of my script record!
Try passing the function name as string i.e.
form.addButton('custpage_validatevsoe', 'Validate VSOE', 'vsoeValidate');
You mentioned that you set vsoeValidate as a validateField function. Do you want this function to run when users click the button or when NetSuite's valdiateField event is fired (upon field change, before the value is stored)?
If you want this to run on NetSuite's validateField event, then the function must return true or false; it cannot return void. Right now in your logic, you have:
if (bucket = 'x') {
return;
}
if (bucket = 'x') is an assignment operation, not an equality check. This assignment operation will return 'x', which is a truthy value, so your code will enter that if-block. You then return void (undefined), so my guess is that NetSuite is trying to do something with the result returned by your function but cannot because it returned undefined.
The validateField function also gets passed a parameter that provides the ID of the field being validated.
You will also want to inject some console logging at various points so you can figure out where your script is failing instead of just trying to guess at reading syntax.
Can you provide us with a screenshot of your Script record setup?
Also since you are using a client side script, you don't need to use the pageInit event for adding a custom button.
There is a 'Buttons' subtab, under the 'Scripts' tab when you create the Script record in NetSuite. This subtab is next to the 'Libraries' subtab.
There are two columns here, 'Label' and 'Function'.
So in your case, you can just put 'Validate VSOE' in the Label field and vsoeValidate in the Function field.
Please note that if you do it this way, the button will only be shown when you are creating or editing a record.

Force a SaveForm in Dynamics CRM 2011 using JavaScript

In CRM 2011, I want to be able to force a save of a form. My requirement is as below.
When the user opens the form, I check if a particular field is empty or not.
If it is empty, I want to force a save, thereby triggering a plugin which will set the field.
My current code is as below. This is in the FormOnLoad function which is associated with LoadForm.
if(checkfield == null){
Namespace.Functions.Contact.FormOnSave();
}
else{
// do nothing.
}
Now, this fails with the following line
Can't execute code from a freed script.
I have no clue as to how to proceed further. As a temporary fix, what I have done is the following
if(checkfield == null){
setTimeout('Namespace.Functions.Contact.FormOnSave()',4000);
}
else{
// do nothing.
}
Any advice on why this is happening and how can I fix it without the timeout would be really helpful?
I have had a look at this question and this, but in my case the form does get loaded or has it actually not yet loaded?
Either way, how can I force a save of a CRM form?
You can save the Form by using:
parent.Xrm.Page.data.entity.save();
But It will only trigger if
(Xrm.Page.data.entity.getIsDirty() == true)
However there is a workaround to set IsDirty property true.
Run the Javascript function on PageLoad. Try to update the value of the field which you are planning to populate through plugin:
For e.g.
function OnPageLoad()
{
var yourField = Xrm.Page.getAttribute("new_yourfield").getValue();
if(yourField == null)
{
parent.Xrm.Page.getAttribute("new_yourfield").setValue("any value");
parent.Xrm.Page.getAttribute("new_yourfield").setSubmitMode("always");
parent.Xrm.Page.data.entity.save();
}
}
You could skip all of this mumble jumbo with loading your form twice with Javascript by creating a plugin registered on the read or the entity. (Or better yet and if possible, on the Create of the entity)
The Plugin would just check for the field, and if it's empty, perform whatever logic you need it to to update the field, then save it, and return the updated field (or possibly just populate the value and don't update the database until the user performs the save)

Categories