I have some JavaScript that runs when the ContactId is changed on a Custom Entity and populates the fax number field from the Contacts entity into mh_fax field of the custom entity. This code works fine:
function contact_onchange () {
// get contactid
var vContactid = Xrm.Page.data.entity.attributes.get("mh_contactid").getValue()[0].id;
try { var Fax= getAttributeValue (vContactid , "contact", "contactid", "fax", false); } catch (e) { }
Xrm.Page.data.entity.attributes.get("mh_fax").setValue(Fax);
}
I am trying to write similar code to run when the parentcontactid field is updated on the Leads entity, but this code does not work.
function contact_onchange () {
// -----get contactid
var vContactid = Xrm.Page.data.entity.attributes.get("parentcontactid").getValue()[0].id;
alert(vContactid)
try { var Fax= getAttributeValue (vContactid , "contact", "contactid", "fax", false); } catch (e) { }
alert (Fax);
Xrm.Page.data.entity.attributes.get("telephone1").setValue(Fax);
}
The 1st alert that I have inserted in the code, shows that vContactid is being set to the correct value.
The problem seems to be with the "Fax= getAttributeValue " line, as the 2nd alert returns undefined and "telephone1 field is blanked out.
Can anyone see why this code works for a custom entity but not for the Leads entity?
Thanks
Related
Once the user clicks on the standard void button in NS, I wanna do something first before it is committed. Since there is no function triggered from client event script, I tried to use User event before submit, but I cannot see a way to identify whether the event is fired by VOID or by SAVE button. Do you have any suggestions/ideas on what work around can be done?
Usage & Explanation
Deploy as a User Event script on the Journal record
This code will find the related transaction that's being voided and before it's voided will change its memo to "This transaction has been voided!"
Note that "before it's voided" refers to when you click the "Save" button on the Voiding Journal record, not specifically when you press the "Void" button on the original transaction.
You can use this as a basis for performing other actions on the voidedRecord object.
SuiteScript 1
function beforeSubmit(type) {
if (type == 'create') {
var voidRecord = nlapiGetNewRecord();
var voidedRecordId = voidRecord.getFieldValue('createdfrom');
var voidedRecordType = voidRecord.getFieldValue('transform');
var voidedRecord = nlapiLoadRecord(voidedRecordType,voidedRecordId);
voidedRecord.setFieldValue('memo','This transaction has been voided!');
nlapiSubmitRecord(voidedRecord);
}
}
SuiteScript 2
/**
*#NApiVersion 2.x
*#NScriptType UserEventScript
*/
define(['N/record'],
function (record) {
function beforeSubmit(context) {
if (context.type == context.UserEventType.CREATE) {
var voidRecord = context.newRecord;
var voidedRecordId = voidRecord.getValue({fieldId: 'createdfrom'});
var voidedRecordType = voidRecord.getValue({fieldId: 'transform'});
var voidedRecord = record.load({
type: voidedRecordType,
id: voidedRecordId
});
voidedRecord.setValue({
fieldId: 'memo',
value: 'This transaction has been voided!'
});
voidedRecord.save();
}
}
}
);
Note this assumes that you have the option set to "Use Reversing Journals to Void Transactions". If that option is not turned-on, then there is no J/E created. The base transaction is set to posting=N, zeroed-out and saved. A voided transaction cannot be resurrected.
I have the following code. There is a button in the UI that when clicked executes the if statement. I pass in a URL from a database and compare it to the current URL the user is on. If they match I want to run the code below, else I want to open the correct tab then run the code below.
With this code below I mean everything below starting from $('#sceanrioDropdownList').change(function () {...}. The code then checks a drop down and gets the selected Id from which an AJAX call is made to my web API that uses that Id in a stored procedure to return the results. The returned data is then iterated over and stored in variables which I am using to append to specific inputs, buttons and drop downs.
This is what I have so far and I think I have developed this correctly. The issue that I am currently having is that the UI wants everything from ... to be run if the if statement is true. I have tried CTRL+C and CTRL+V to copy the code into the if statement. I have also tried putting it in a new function and referencing that function n the if statement. Both do not work and I was using console.log to inspect the returned data.
It does however when I attempt to call it from inside i statement it doesn't return any data or error. It just doesn't seem to fire.
Is there a way in which I can achieve the functionality I desire? Do you have any suggestions as to if I have done something wrong. Thanks in advance.
$('#automate').click(automateButton);
function automateButton() {
if (webpageUrl == activeTabUrl) {
// do nothing
} else {
// Window opens
window.open(webpageUrl);
}
}
$('#scenarioDropdownList').change(function() {
var scenarioId = $('#scenarioDropdownList option:selected').prop('id');
getData(scenarioId);
});
function getData(scenarioId) {
$.ajax({
type: "GET",
url: 'http://localhost:54442/api/scenariodatas/GetScenarioData',
data: {
scenarioId: scenarioId
},
dataType: 'JSON',
success: scenarioData,
error: function() {
console.log("There has been an error retrieving the data");
}
});
}
function scenarioData(response) {
$.each(response, function(key, val) {
var fieldType = val.fieldType;
var fieldName = val.fieldName;
var fieldValue = val.fieldValue;
var field = $(fieldName);
if (field != undefined) {
switch (fieldType) {
case "Input":
$(field).val(fieldValue);
break;
case "Button":
$(field).click();
break;
case "Select":
$(field).val(fieldValue);
break;
}
}
})
}
onChange don´t work well with buttons because onChange detect a change in the value of your component, because of this, it´s highly recommended to use onClick when you use a button.
$('#scenarioDropdownList').click(function() {
var scenarioId = $('#scenarioDropdownList option:selected').prop('id');
getData(scenarioId);
});
I recommend you to put alerts when you are trying to test this sort of JS
EJM:
$('#scenarioDropdownList').change(function() {
alert('button active');
var scenarioId = $('#scenarioDropdownList option:selected').prop('id');
getData(scenarioId);
});
this alert allow you to know if the code is firing or not
I am facing an unexpected behavior from radtextbox control of telerik..
Let me show you what I have tried first,
function MouseOut(sender, eventArgs) {
debugger;
var Invc = new WcfAjaxServices.IInventory();
var data = null;
if (sender.get_textBoxValue() != '') {
if (!isBarCodeInsideGrid(sender)) {
Invc.ShowbarCodes(sender.get_textBoxValue(), function (result, context, OnSuccess) {
if (result) {
debugger;
clearBarCode(sender);
alert("already exists in database!");
eventArgs.
}
}, function (error, context, OnError) {
toastify("error", "ppp", "System Error", "toast-bottom-right", true);
}, null);
} else {
clearBarCode(sender);
alert("This barcode is already present inside grid");
}
} else {
alert("Insert valid value");
}
}
Now let me show you the design of the grid here..
Now, whenever user entered some value it will check
If exist in database..
If already exist inside grid..
I am able to clear textbox's value for 2nd option i.e If value is already present inside grid,but when it comes to point 1, I am using same code to clear its value but its not working..
Let me show you what I am getting from debugger after clearing text from textbox..
function that responsible for clearing..
function clearBarCode(item) {
debugger;
item.set_value("");
//item.focus();
}
Any idea why this is happening ? Or how to get desired result..
BarCode001 is still displaying inside grid..
I have a series of buttons that execute different functions when clicked. The function checks whether the user is logged in, and if so proceeds, if not it displays an overlay with ability to log in/create account.
What I want to do is re-execute the button click after log-in, without the user having to reclick it.
I have it working at the moment, but I'm pretty sure that what I'm doing isn't best practice, so looking for advice on how I can improve...
Here's what I'm doing: setting a global variable "pending_request" that stores the function to be re-run and in the success part of the log-in ajax request calling "eval(pending_request)"
Example of one of the buttons:
jQuery('#maybe_button').click(function() {
pending_request = "jQuery('#maybe_button').click()"
var loggedin = get_login_status();
if (loggedin == true) {
rec_status("maybe");
}
});
.
success: function(data) {
if(data === "User not found"){
alert("Email or Password incorrect, please try again");
}else{
document.getElementById('loginscreen').style.display = 'none';
document.getElementById('locationover').style.display = 'none';
eval(pending_request);
pending_request = "";
}
}
Register a function to handle the click and then invoke that func directly without eval().
jQuery('#maybe_button').on('click', myFunction)
This executes myFunction when the button is clicked. Now you can "re-run" the function code every time you need it with myFunction().
And btw since you are using jQuery you can do $('#loginscreen').hide() where $ is an alias for jQuery that's auto defined.
EDIT
Please, take a look at the following code:
var pressedButton = null;
$('button1').on('click', function() {
if (!isLoggedIn()) {
pressedButton = $(this);
return;
}
// ...
});
And, in your success handler:
success: function() {
// ...
if (pressedButton) pressedButton.trigger('click');
// ...
}
I have a simple registration form. As soon as the user types his phone number and leaves the textbox (onblur), a few of the other fields get auto populated, using an Ajax call to a php script, and this piece works fine. Now, I'm trying to add another onblur() event ON A DIFFERENT TEXT FIELD that has nothing to do with the ajax call or the fields populated but is in the same form. However, when I create this function in java script, the event does not get fired. More over, the original ajax call also stops working i.e., the onblur event for the phone number field also does not get fired (I've confirmed this by putting a few alert messages in place). I'm stuck and given that I'm a novice web developer, it has been an irritable ride. So any help or a nudge in the right direction would be appreciated.
My java script code is in the same file as my HTML code, i.e., between the tags. When I create both the functions between the same script tags, none work, but when I put them in separate script tags, the first function (ajax call behind Phone number) gets fired on blur but the second one does not. The functions in point are: vldtnPhNo() and enabtnRegCmplt().
<script type="text/javascript">
function vldtnPhNo()
{
var xhr;
var dvPhNo = document.getElementById("divPhNo");
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
var FoneNumb = document.getElementById("txtPhNo").value;
xhr.open("POST", "verify.php", true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xhr.onreadystatechange=function() {
if (xhr.readyState==4 && xhr.status==200) {
document.getElementById("txtRegInv").value = xhr.responseText;
var rstOftheTxt = xhr.responseText;
document.getElementById("txtPhNo").value=rstOftheTxt;
document.getElementById("txtPhPop").value=rstOftheTxt;
}
else
{
document.getElementById("txtPhNo").value='waiting';
document.getElementById("txtPhPop").value='waiting';
}
}
}
xhr.send("txtPhNo=" + FoneNumb);
}
else
{
throw new Error("Ajax is not supported by this browser");
}
}
function enabtnRegCmplt(){
If(txtRegInv.value!="")
{
var UsrNm;
var Psswd;
var RePsw;
var PostBackInfo;
UsrNm = document.getElementById('txtUsrNm');
Psswd = document.getElementById('txtPsw');
RePsw = document.getElementById('txtrePsw');
txtRegInv = document.getElementById('txtRegInv');
If (RePsw.value!="" && Psswd.value!="" && UsrNm.value!="" && RePsw.value==Psswd.value)
{
document.getElementById('btnRegCmplt').disabled="False";
}
else if(UsrNm.value=="")
{
UsrNm.value="Please Enter UserName";
}
else if(Psswd.value=="")
{
Psswd.value="Please Enter Password";
}
else if(RePsw.value=="")
{
RePsw.value="Does not match";
}
}
}
</script>
Your code is very messy. One of the reason why it's necessary to write neat code. Your brackets are wrong, thus the whole code isn't executed (else statement is connected with function).
The If statement doesn't exist, try if instead (lowercase I).
Other things:
use onkeyup instead.
2:
For the vars you could do:
var UsrNm,
Psswd,
RePsw,
PostBackInfo
;
OR
var UsrNm = document.getElementById('txtUsrNm'),
etc.
Syntax mistake
An extra brace }
`else
{
document.getElementById("txtPhNo").value='waiting';
document.getElementById("txtPhPop").value='waiting';
}
}
}`
Remove one brace after the else condition