I created this javascript to auto populate a field, with values from other fields. It is called in the form onSave event.
function OppTopic() {
var products = "";
var parent = Xrm.Page.getAttribute("parentaccountid").getvalue();
var city = Xrm.Page.getAttribute("address1_city").getValue();
var automation = Xrm.Page.getAttribute("new_automationfeatures").getValue();
var service = Xrm.Page.getAttribute("new_service").getValue();
//Determines if a Product/Service is selected
if (automation == true) {//***AUTOMATION***
if (products != ""){
products += ",Automation";
}
else{
products = "Automation";
}
}
if (service == true) {//***SERVICE***
if (products != "")
products += ",Service";
else
products = "Service";
}
if (automation == false && service == false) {
products = "null";
}
var subject = parent + " - " + city + " - " + products;
Xrm.Page.getAttribute("name").setValue(subject);
}
But , when the form is saved this is the error that appears.I'm not really sure what the error means?
I have checked the field names and they are correct.
What could be the problem that is causing this error?
Thanks
var parent will return a javascript object that is a CRM lookup. If you are putting it together in a string with other strings, you will have to retrieve the name or whichever other attribute from the lookup you're trying to add
var parentName = "";
var parent = new Array();
parent = Xrm.Page.getAttribute("parentaccountid").getValue();
if(parent!=null){
parentName = parent[0].name;
}
Source: http://www.mscrmconsultant.com/2012/08/get-and-set-lookup-value-using.html
Related
On this question here I finally succeed to change two values from the same column - priority. I tried to do it on select list - column priority lov - but with no conclusive success. Selects don't have "default values" properties like text fields, so I tried to get it from the source.context.index properties. Here the oracle apex app, user and password test.
I'm considering to use pure Javascript, to deal with it.
The Javascript is triggered after change the select list:
var source = apex.jQuery(this.triggeringElement).find('select[name="f31"]')
console.log(source)
var lista = apex.jQuery(source.context.form).find('select[name="f31"]')
console.log(lista)
console.log('source.context.selectedIndex inicial ' +source.context.index)
var valor_default = lista[0].selectedIndex
console.log(valor_default)
var index_default = apex.jQuery(this.triggeringElement).closest('select[name="f31"]').find('option[selected]')[0].index
console.log('indice default:' + index_default)
for (var x=0;x<lista.length;x++){
if (source.context.selectedIndex == lista[x].selectedIndex && source.context != lista[x]){
console.log('selectedIndex ' + source.context.selectedIndex)
console.log('source.context')
console.log(source.context)
console.log('lista[x]')
console.log(lista[x])
lista[x].selectedIndex = index_default
index_default = source.context.selectedIndex
// lista[x].defaultValue = source.context.defaultValue
// source.context.defaultValue = source.context.value
}
}
Fellows,
A possible solution was found.
On form properties - footer text, an array is build to get the indexes of object f31 - select list:
<script >
var listaOriginal = document.getElementsByName('f31')
var valordefault = []
for (item of listaOriginal) {
valordefault.push(item.selectedIndex)
}
console.log(valordefault)
</script>
on change from select list the array is used to compare with the changes:
var source = apex.jQuery(this.triggeringElement).find('select[name="f31"]')
var lista = apex.jQuery(source.context.form).find('select[name="f31"]')
var valueDefault = lista[0].selectedIndex
var happyIndex = ''
for (var happy=0;happy<lista.length; happy++){
if (source.context === lista[happy]){
happyIndex = happy
}
}
for (var x=0;x<lista.length;x++){
if (source.context !== lista[x] && source.context.selectedIndex == lista[x].selectedIndex){
var my_table = {};
my_table.source_context_selectedIndex = source.context.selectedIndex
my_table.lista_x_selectedIndex = lista[x].selectedIndex
my_table.valueDefault = valueDefault[x]
console.table(tabela)
lista[x].selectedIndex = valueDefault[happyIndex]
valueDefault[happyIndex] = valueDefault[x]
valueDefault[x]=lista[x].selectedIndex
// source.context.defaultValue = source.context.value
}
console.log(valueDefault)
}
I created a small function that stores the book isbn, it's name and it's author. Everything is fine until I start to print out array. On every entery that completes the object into array, I want it to be printed one after another in new row, but this one is printing the objects from beginning every time when a new object is inserted. How do I fix this?
var books = [];
function blaBla(){
while(isbn != null || name != null || writer != null){
var isbn = window.prompt("Enter ISBN");
var name = window.prompt("Enter name of the book");
var writer = window.prompt("Enter name of the writer");
var patternString = /^[a-zA-Z]+$/;
var patternNum = /^[0-9]+$/;
if(isbn.match(patternNum)){
if(name.match(patternString)){
if(writer.match(patternString)){
books.push({
isbn: isbn,
name: name,
writer: writer
});
}
}
}
for (var i=0; i<books.length; i++){
document.write(books[i].isbn + " - " + books[i].name + " - " + books[i].writer + "</br>");
}
}
}
PS: How do I make it even more "cleaner", so when I hit cancel on prompt, it automatically stops with entering data into array, while, if i stop it on the "writer" prompt, it deletes previous entries for that object (last isbn and last name of the book)?
Thanks in advance.
You might want to give a little more context as to what this function is doing so we can help make your code cleaner as requested. I've separated the collection logic from the display logic here, and also used a while (true) loop with breaks on null or invalid inputs which will stop the collection of data.
Please note that prompt/alert boxes are a hideous way of collecting user input though (very awkward user experience). Consider using a table, input fields, and some jQuery instead to add rows and validate what the user has entered into input boxes.
var books = [];
function collectResponses() {
var patternString = /^[a-zA-Z]+$/;
var patternNum = /^[0-9]+$/;
while (true) {
var isbn = window.prompt("Enter ISBN");
if (!isbn || !isbn.match(patternNum)) {
break;
}
var name = window.prompt("Enter name of the book");
if (!name || !name.match(patternNum)) {
break;
}
var writer = window.prompt("Enter name of the writer");
if (!writer || !writer.match(patternNum)) {
break;
}
books.push({
isbn: isbn,
name: name,
writer: writer
});
}
}
function displayResponses() {
for (var i=0; i<books.length; i++){
document.write(books[i].isbn + " - " + books[i].name + " - " + books[i].writer + "</br>");
}
}
im so new to javascript, my situation is beginner of the beginner :)
But i have to write a code on mscrm 2011 order entity.
i have two field, pricelevelid and new_taxincl(radio button). I want to change value in pricelevelid field based on selection of new_taxincl field.
If user select ''yes'' i want to fill pricelevelid field with ''USD -Tax Included-'', ''no'' ''USD -without tax-''. This have to run onChange event.
I could not write the correct code so far.
Any help will be fine for me.
Thanks.
PS: new_vrg is a field in the pricelevel entity and has same value with new_taxincl field in the order entity.
here is my code:
function callRetrievePList() {
var field = Xrm.Page.data.entity.attributes.get("new_taxincl");
if ((field == null) || (field.getValue() == null) || (field.getValue()[0] == null) || (field.getValue()[0].id == null))
return;
var plistfield = Xrm.Page.data.entity.attributes.get("pricelevelid");
if (plistfield != null && plistfield.getValue() != null)
return;
var options = "$select=PriceLevelId,Name&$filter=new_vrg/Id eq (guid'" + (field.getValue()[0]).id + "')";
SDK.REST.retrieveMultipleRecords("PriceLevel", options, retrievePListCallBack, errorCallBack, completeCallBack);
}
function retrievePListCallBack(retrievedStock) {
var plistfield = Xrm.Page.data.entity.attributes.get("pricelevelid");
if (plistfield != null && plistfield.getValue() != null)
return;
for (var i = 0; i < retrievedStock.length; i++) {
var stock = retrievedStock[i];
var lookupReference = [];
lookupReference[0] = {};
lookupReference[0].id = stock.PriceLevelId;
lookupReference[0].entityType = "pricelevel";
lookupReference[0].name = stock.Name;
plistfield.setValue(lookupReference);
return;
}
}
function errorCallBack(errorObject) {
}
function completeCallBack() {
}
Below code may help you:
To get a radio button in CRM JavaScript:
function TaxIncludedRadioButton_OnChange()
{
var taxInclBtn= Xrm.Page.ui.controls.get("new_taxincl").getAttribute().getValue();
var lookup = new Array();
lookup[0] = new Object();
if(taxIncludeRdBtn==true)
{
lookup[0].id = yourrecorid1;
lookup[0].name = 'USD -Tax Included-';
lookup[0].entityType = entityname;
Xrm.Page.getAttribute("pricelevelid").setValue(lookup);
}
else if(taxIncludeRdBtn==false)
{
lookup[0].id = yourrecorid2;
lookup[0].name = 'USD -Without-tax';
lookup[0].entityType = entityname;
Xrm.Page.getAttribute("pricelevelid").setValue(lookup);
}
}
Register the above function OnChange event of you new_taxincl radio button
I have a few textboxes on a page whose values are getting populated from the return value of
a modal window like this and I have a javascript method that calls the event handler of a button in the following way. The values are being returned properly and the textbox is getting populated properly but i am getting an exception parameter count mismatch.
res = window.showModalDialog('frm_VisitorSearchPopUp.aspx', "", "dialogWidth:1024px;dialogHeight:600px");
getElementById('<%=AddVisitorID.ClientID %>').click();
This problem does not occur when i am using window.open() method can anybody tell me why this problem is occurring.
My Code
function openup() {
var left = screen.width / 2 - 1024 / 2;
var tops = screen.height / 2 - 600 / 2;
var d = new Date();
var res;
res = window.showModalDialog('frm_VisitorSearchPopUp.aspx', "", "dialogWidth:1024px;dialogHeight:600px");
setvalues(res.PersonName, res.Address, res.CompanyName, res.ContactNumber, res.Email);
}
function setvalues(PersonName, Address, CompanyName, ContactNumber, Email) {
var RowId = $("#<%= VisitorDetailsGrid.ClientID%>").getDataIDs();
for (i = 0; i < RowId.length; i++) {
rowData = $("#<%= VisitorDetailsGrid.ClientID%>").getRowData(RowId[i]);
if (rowData.PersonName == PersonName && rowData.ContactNumber == ContactNumber && rowData.CompanyName == CompanyName && rowData.Email == Email && rowData.Address == Address) {
alert("The visitor \"" + PersonName + "\" has already been added to the visitor's list.");
document.getElementById('<%=PersonNameID.ClientID%>').value = "";
document.getElementById('<%=AddressID.ClientID%>').value = "";
document.getElementById('<%=CompanyNameID.ClientID%>').value = "";
document.getElementById('<%=ContactNumberID.ClientID%>').value = "";
document.getElementById('<%=EmailID.ClientID%>').value = "";
return;
}
}
document.getElementById('<%=PersonNameID.ClientID%>').value = PersonName;
document.getElementById('<%=AddressID.ClientID%>').value = Address;
document.getElementById('<%=CompanyNameID.ClientID%>').value = CompanyName;
document.getElementById('<%=ContactNumberID.ClientID%>').value = ContactNumber;
document.getElementById('<%=EmailID.ClientID%>').value = Email;
elem = document.getElementById('<%=AddVisitorID.ClientID %>').click();
}
I'm trying to get the ContentTypeId of an item in sharepoint to get the full url of the item to get the binary of it and after send it to another plateform.
So here i put this code in element.xml to get the list ID and the document ids of the items i'm selecting, after this i send them to an ASPX page in a Sharepoint Dialog to define the destination of the items and after this in the postback, stream the binary and send it to the another platform. The problem is : To get the full url of my items i need ListId, ItemId and ContentTypeId.
Because i've found a code to stream the binary here :
How to Programatically Download files from sharepoint document library
And i need the full url of my items.
Any idea?
thanks
var iddocs ='';
var listId ='';
function geturl()
{
var context = SP.ClientContext.get_current();
this.web = context.get_web();
listId = SP.ListOperation.Selection.getSelectedList();
var list = this.web.get_lists().getById(listId);
var ok = false;
try
{
if ( SP.ListOperation.Selection.getSelectedItems(context) !== false)
{
var items = SP.ListOperation.Selection.getSelectedItems(context);
var url='listId:'+listId+ ' Number of selected items: ' + items.length ;
var i = 0;
if(items.length==0)
{
}else{
while( i != items.length )
{
url += ' Doc' + i + ': ' + items[i].id;
if(i>0){iddocs += '-'};
iddocs += items[i].id;
i++;
};
ok = true;
alert(url+' Id of clicked item:'+{ItemId});
};
};
}
catch(err)
{
};
return ok;
};
function OpenDialog(pidliste) {
var options = SP.UI.$create_DialogOptions();
options.width = 600;
options.height = 600;
options.title = 'Envoyer vers Nuxeo';
options.url ='/_Layouts/SPTest.CustomMenuItem/index.aspx?click={ItemId}';
if(pidliste){options.url += '&ids='+pidliste +'-'+ iddocs;};
options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
SP.UI.ModalDialog.showModalDialog(options);
}
function CloseCallback(result, target) {
if (result == SP.UI.DialogResult.OK) {
}
if (result == SP.UI.DialogResult.cancel) {
SP.UI.Notify.addNotification('Opération canceled', false, '', null);
}
}
if(geturl())
{
OpenDialog(listId);
}else{
alert('Please select an item');
};
I've found the solution. In fact, items can be reached via :
{SiteUrl}+{ItemUrl}
The download function is linked in my first Post. But it doesn't work for multiple items, with this method you can only reach the properties of the item you're selecting.
You have to note that if you want to access to a SP file, you have to set your request.credential via :
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
which will take the current credential you're using.
Hope it helps.