Dynamics CRM 4.0 - Saving a Form once all Required Fields are Filled in

Paul Nieuwelaar, 04 August 2010

In CRM, by default, when you create a new record, the left hand navigation area is greyed out until the form has been saved. And the only way to save the form is to fill in every required field, and then click save. Which is fine, but I wanted to make it so that once all the required fields had been filled in on the form, the form would be saved automatically, and therefore unlocking the left navigation.

In particular I wanted to do this on Quotes, Orders, and Invoices, because when you’re creating one usually the first thing you want to do is add line items.

The first step is to locate all the required fields on the form that you would need to fill in before saving the form (for example not Owner, since it prefills), note down all of their names. With Quote for example they are Name, Potential Customer, and Price List.

Next open the onChange event for any of the required fields, check Event is enabled, and paste in the following code:

if (crmForm.FormType == 1) {

    if ((crmForm.all.name.DataValue != null) && (crmForm.all.customerid.DataValue != null) && (crmForm.all.pricelevelid.DataValue != null)) {

        crmForm.Save();

    }

}

Change ‘name’, ‘customerid’, and ‘pricelevelid’ to be the names of all the required fields you would normally fill in. If there are more or less than the 3 required fields in the code above, add another ‘&& (crmForm.all.customerid.DataValue != null)‘, changing ‘customerid’ or remove one of them, so that it still looks similar to above.

Since we want this code to fire when any of the Required fields is changed, we will need to add a line of code to each of the other required fields, to fire that code when they are changed. Check Event is enabled, and paste in the following line of code to each of the other required fields:

crmForm.all.name.FireOnChange();

Change ‘name’ to be the name of the field you pasted the first lot of code into. This will fire the onChange for ‘name’ when any of the required fields is changed, so it won’t matter in what order you fill them out in.

The last step is to add the same line of code to the forms onLoad event, so if onLoad the fields are already filled in (based on field mappings from another entity) then the form will save automatically. Check Event is enabled, and paste in the following line of code to forms onLoad event:

crmForm.all.name.FireOnChange();

Publish the entity and test that it works, all the code should work unchanged for Quote, Order, and Invoice, but can also be used on any entity.