Accessing Other Entities Attributes in Dynamics CRM/365 Forms with javaScript - javascript

This function buttonBuzz() works inside the Forms of the Entities Account, Contacts and Leads. But not in the Opportunity form.
Mainly because there is no telephone1 attribute. There is however a Contact entity added with "Quick View" in a section with a telephonenumber inside.
I think it can be accessed with the telephone1 as well just not with Xrm.page
Any ideas how i can grab the attribute from inside the "quick view"?
I dont know if the "Quick view" window is a form of an iFrame. And if it is i have no clue how to access it with the Xrm.Page.getAttribute("telephone1").getValue();
function buttonBuzz(exObj) {
var phoneNumber;
// Here i store the "telephone1" Attribute from the current .page
phoneNumber = Xrm.Page.getAttribute("telephone1").getValue();
if (phoneNumber != null) { **Sends phonenumber** } ...

Quick Views display data from a record selected in a lookup field, in this case a Contact. You can query data from related records using the OData endpoint.
You first need to get the Guid of the record selected:
var contactId = Xrm.Page.getAttribute("parentcontactid")[0].id || null;
You would then need to send a SDK.REST request, passing parameters for the Id of the record (contactId), entityName and the columns:
var entityName = "Contact";
var columns = "Address1_Telephone1, FirstName, LastName";
SDK.REST.retrieveRecord(contactId, entityName, columns, null, function(result) {
// Success, logic goes here.
var address1_Telephone1 = result.Address1_Telephone1;
}, function(e) {
console.error(e.message);
});
As well as your JavaScript file, you would need to include the SDK.REST.js file that is included in the MS CRM SDK download within your Opportunity form libraries.

You can pull that field up from the Contact into the Opportunity by creating a Calculated Field, setting it equal to parentcontactid.telephone1
Put the field on the form, and you'll be able to .getAttribute() it like any other Opportunity field (being Calculated, it updates itself whenever the source changes).

Related

How to get Dynamics CRM Web Resource name using JavaScript

I'm working on a project. I want to get the name of web resource e.g if I want to get entity name I use this query
var entityName = parent.Xrm.Page.data.entity.getEntityName();
var id = parent.Xrm.Page.data.entity.getId();
so in the same way how I can get the web resource name at this time, I am passing the web resource as a string.
getImages(entityName, id, "WebResource_webTest");
So can you tell me how i can get the web resoucre name.
Here is the code snippet I just tried on one of my Entity and it gave me webresource name
You can add this function on load on change and pass execution context as parameter to function
Note: If you have 5 webresources on your form you will get one by one all the webresource names. you can tweak/modify code as per your need.
function onChangeOfField(executionContext) {
debugger
var formContext = executionContext.getFormContext();
formContext.ui.controls.forEach(function(control, index) {
var controlType = control.getControlType();
if (controlType === "webresource" ) {
alert(control.getName());
}
});
}

Troubleshooting SuiteScript 1.0 Line Level Field Sourcing Code (List/Record)

I am an inexperience technical developer working on my first SuiteScript using SuiteScript 1.0. I am getting an SSS_MISSING_REQD_ARGUMENT error, but I am sure there are many more in my code. The purpose of the script is to populate the department field on the expense record line item from a joined record. The end user will select a Project on the expense line, and the script should look up the department on the project record (a custom field) and add the value to the native department field. Code is copied below.
function ProjectSegment ()
{
var record = nlapiLoadRecord(nlapiGetRecordType(), nlapiGetRecordId());
var recordID = nlapiGetRecordId(record);
//internal ID of project record
var project = nlapiGetField ('custcol_nra_expense_project');
//load project record
var precord = nlapiLoadRecord('job', project);
//get department on project record (internal ID)
var pdepartment = precord.GetFieldValue('custentity_nra_dept_project');
//get project name from project record
var projectName = precord.GetFieldText('entityid');
//load existing search
var search = nlapiLoadSearch('job','customsearch161');
//add filter to include project name
search.addFilter(new nlobjSearchFilter('entityid',null,'is',projectName));
//run search
var resultSet = search.runSearch();
//get department line
var departmentResult = new nlobjSearchColumn('custentity_nra_dept_project');
//set value
nlapiSetFieldTexts('job','department',1,departmentResult)
//record.commitLineItem('department');
nlapiSubmitRecord(record, true);
}
//internal ID of project record
var project = nlapiGetFieldValue ('custcol_nra_expense_project');
Praveen Kumar's response is correct regarding the missing required argument, but there are many other issues with the script as you've surmised.
Side notes:
The getFieldValue and getFieldText methods of nlobjRecord are not capitalized.
For performance reasons, you could/should use a search to get the values you need from the job record. Loading a record just to get field values is wasteful unless you mean to change the record.
Your search filter should probably be based on the value (not text) of the entityid in the job record.
Your desired search column probably is invalid (I don't think a custentity field could be on a job record).
Getting the search result is incorrect.
You want something like this instead:
var result = resultSet.getResults(0, 1);
if (result) {
var department = result.getValue('custentity_nra_dept_project');
// etc.
}
All that said, though, from your description, I don't think you need the search anyway. Once you have pdepartment (again, using precord.getFieldValue), I think all you need is:
record.setFieldValue('department', pdepartment);
Or if you're setting the line-level department, it would be different.
What kind of script is this? I'd like to make more recommendations, but it depends on what's going on.

Populate Django Drop Down List from another Drop Down List - Javascript

I have a Django + Python website, where user can request a access to a database. So in this form the user has these options.
Environment: This is a drop down list with two values: Development and Production
Permission: This is another drop down list with two values: Read and Read/Write
What I am trying to achieve is:
If Environment == Development
AddToPermissionDropDownList: Database Owner
else
Permission
So, as you can see, I want to populate one drop down list based in the selected value of another drop down.
I know that I need to use JavaScript to achieve that, but I am stuck on how to do it.
This is what I have been able to do so far.
models.py
class NewAccessRequest(models.Model):
permission_needed = models.CharField(max_length=25,verbose_name='Permissions')
forms.py
class AccessRequestForm(ModelForm):
environment= forms.ChoiceField(choices=ENVIRONMENT_CHOICES,label="",initial='',widget=forms.Select(attrs={'id':'environment','onchange':'changePermissions()'}), required=True)
permission_needed = forms.ChoiceField(initial='',widget=forms.Select(), required=True)
access.html
<form>
{% csrf_token %}
<label for="{{ form.environment.id_for_label }}">Environment:</label>
<p>{{form.environment}}</p>
<script>
function changePermissions() {
var EnvChosen = document.getElementById("environment").value;
if (EnvChosen == "DEV") {
document.getElementById("id_business_reason").value = "This is a test"
}
}
</script>
</form>
Since it needs to be populated from the options from back end .
you can call a ajax method to get the data from back end on the change event of dropdown-1 , then you have to populate dropdown-2 with data got from the response of the ajax , also you have to pass the value from dropdown-1 with the request. you can either use a get or post request. if it is post request you also need to pass csrf token
This is what I have been able to do so far. The only problem that I am seeing is.
1) In the environment drop down list I select: DEV
1.1) DB OWNER gets added
2) Now change the environment drop down list to: PROD
2.1) DB READER gets added
3) Now change the environment drop down list to: DEV
3.1) DB OWNER gets added again.
I know that I need somehow to cleanup the values in the drop down list, I just don't know yet on where and what code to do.
<script>
function changePermissions() {
var EnvChosen = document.getElementById("environment").value;
var selectList = document.getElementById("id_permission_needed")
if (EnvChosen == "DEV") {
var option = document.createElement("OPTION");
var txt = document.createTextNode("DB OWNER");
option.appendChild(txt);
selectList.insertBefore(option, selectList.lastChild);
}
if (EnvChosen == "PROD") {
var option = document.createElement("OPTION");
var txt = document.createTextNode("DB READER");
option.appendChild(txt);
selectList.insertBefore(option, selectList.lastChild);
}
}
</script>

Using read function of oData model in UI5

I am coding an UI5 App which consumes a given OData Service. Now I want to get the name of an account with a given account number and Display it in a table. As I can only access the account Name via /AccountInfoSet()/ShortText I tried to use a formatter function to map the account number.
Binding in View:
Formatter function in Controller:
numToNameFormatter : function(sNum){
var text = this.getView().getModel().read("/AccountInfoSet('" + sNum + "')", null, null, true,
function(oData, oResponse){
return JSON.stringify(oData);
},
function(){
alert("Read failed");
});
return text;
}
This should return the requested object as a string. The data is requested successfully, as I verified via an alert. The problem is, that I can't get the data out of the call back, as it ist asynchronous. How do I get the data.
Thanks in advance!
Not sure if your data model is set up like this, but would it be possible to expand your table set to also load the related AccountInfoSet's?
I mean, if your table holds for instance an array of Accounts, and each Account entry has a related AccountInfo, you could just fill your table with the following:
http://your.service/Accounts/?$expand=AccountInfo
You then bind the field in your table directly, without a formatter:
<TextField value="{AccountInfo/0/ShortText}">

Xrm.Utility.openEntityForm setting Look Up field

I am attempting to use the Xrm.Utility.openEntityForm() method to open a new custom entity form and programatically set an entity look up field. I am following an example on http://msdn.microsoft.com/en-us/library/gg334375.aspx very closely but getting nondescript error. Any help with actually setting the field or possibly finding the logs for the error would be appreciated.
The code example I am following.
function OpenNewContact() {
var parameters = {};
//Set the Parent Customer field value to “Contoso”.
parameters["parentcustomerid"] = "2878282E-94D6-E111-9B1D-00155D9D700B";
parameters["parentcustomeridname"] = "Contoso";
parameters["parentcustomeridtype"] = "account";
//Set the Address Type to “Primary”.
parameters["address1_addresstypecode"] = "3";
//Set text in the Description field.
parameters["description"] = "Default values for this record were set programmatically.";
//Set Do not allow E-mails to "Do Not Allow".
parameters["donotemail"] = "1";
// Open the window.
Xrm.Utility.openEntityForm("contact", null, parameters);
}
The function I have created to do the same with my custom entity is as follows :
function createNewService() {
var locationId = trimBrackets(Xrm.Page.data.entity.getId());
var primaryField = Xrm.Page.data.entity.getPrimaryAttributeValue();
var entityLogicalName = Xrm.Page.data.entity.getEntityName();
var parameters = {
cw_location: locationId,
cw_locationname: primaryField,
cw_locationtype: entityLogicalName
};
Xrm.Utility.openEntityForm("cw_service", null, parameters);
}
the name of the entity I am opening a form work = cw_service (this isn't the problem as I can open a blank form with Xrm.Utility.openEntityForm("cw_service");)
the name of the field I am trying to set is cw_location.
I'd post a picture of the error message but I don't have the reputation yet to do that.
For simple lookups you must set the value and the text to display in the lookup. Use the suffix “name” with the name of the attribute to set the value for the text.
Don’t use any other arguments for simple lookups.
For customer and owner lookups you must set the value and the name in the same way you set them for simple lookups. In addition you must use the suffix “type” to specify the type of entity. Allowable values are account, contact, systemuser, and team.
For your example, it is a simple lookup, I suppose. So, Please try using the code below:
var parameters = {
cw_location: locationId,
cw_locationname: primaryField
};
For more information, Please visit Set the value for lookup fields.

Categories

Resources