Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Set value for fields using JavaScript

$
0
0

Hello guys,

This is actually can consider related to my previous thread, about Web resources for Open a new form then Save -> https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/430042/open-create-form-and-save-instantly

But it may not necessary also.

Issue is about specifying a default value for my fields, while having this kind of script ->

function OpenAndSaveForm(executionContext) {
//Get the form context
var formContext = executionContext.getFormContext();
// Get the form type
var formType = formContext.ui.getFormType();
if(formType == 1) { //create form
    try {

             formContext.data.entity.save();
        }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
        }
   }
}

So because I want to have a default value in my 2 fields (xyz_description and xyz_date), I was adding these to statements :

function OpenAndSaveForm(executionContext) {
//Get the form context
var formContext = executionContext.getFormContext();
// Get the form type
var formType = formContext.ui.getFormType();
if(formType == 1) { //create form
    try {

        formContext.getAttribute("xyz_description").setValue("Orders"); 

        formContext.getAttribute("xyz_date").setValue(new Date());  


        formContext.data.entity.save();
        }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
        }
   }
}

The lines in red are my addition, but currently produces an error saying "Cannot read property 'setValue' of null"

What would be the cause of it ? Is it correct that inside those "formContext.getAttribute" is my field name ? or should be other which makes it error. ?

Kindly advice.

Thanks


Viewing all articles
Browse latest Browse all 46379

Trending Articles