Hi,
You can refer the below article for the JavaScript basics.
community.dynamics.com/.../javascript-basics
Below is the sample JS code which sets the value to undtry optioset field based on the value entered in the code.
------------
function onChangeNAICS() {
var codeToSearch = Xrm.Page.data.entity.attributes.get("new_naicscode").getValue();
if(codeToSearch != null)
{
// Initialize the value to zero to check before setting it to industry
var valuereturned = 0;
// Initialize the Code & Industry Mapping
var codeIndustrymapping = [];
codeIndustrymapping.push({ code: '100', industryCode: 100000000 });
codeIndustrymapping.push({ code: '101', industryCode: 100000001 });
codeIndustrymapping.push({ code: '102', industryCode: 100000002 });
codeIndustrymapping.push({ code: '103', industryCode: 100000003 });
codeIndustrymapping.push({ code: '104', industryCode: 100000004 });
// Loop through all the available mapping values and set the valuereturned for the maching code.
for (i = 0; i < codeIndustrymapping.length; i++) {
if (codeIndustrymapping[i].code === codeToSearch) {
valuereturned = codeIndustrymapping[i].industryCode;
}
}
}
// check if the value is set from the mapping
if (valuereturned != 0) {
// if value is set from the mapping, set the value in the industry optionset
Xrm.Page.data.entity.attributes.get("new_industry").setValue(valuereturned);
}
}
------------------------