Open and prepopulate fields onto a new form using JavaScript in Resco Mobile CRM

The only way to achieve opening of a new form is to write some JavaScript using JSBridge. I would assume already that you know how to add commands and OfflineHTML within Woodford.

 

Below is an example of creating a new Customer Asset form via a Command on the Work Order Product Form. I am retrieving values on the Work Order Product form and passing them into the iFrameOptions object.

if (typeof (FS) == "undefined")
{ FS = { __namespace: true }; }

if (typeof (new_WorkOrderProduct) == "undefined")
{ new_WorkOrderProduct = { __namespace: true }; }

FS.new_WorkOrderProduct = {

    //Function to trigger when the 'Create New Asset' is pressed
    createCustomAssetButton: function (entityForm) {
        MobileCRM.UI.EntityForm.onCommand("custom_createNewAsset", 
        function (entityForm) {
                MobileCRM.UI.EntityForm.requestObject(
            function (entityForm) {
                var editedWOP = entityForm.entity;
                var productid = editedWOP.properties["msdyn_product"].id;
                var productname = editedWOP.entity.properties["msdyn_product"].primaryName;
		        var new_msdyn_product = new MobileCRM.Reference("product", productid, productname);
				
                var target = new MobileCRM.Reference(editedWOP.entityName, editedWOP.id);
                var relationShip = new MobileCRM.Relationship("msdyn_workorderproduct", target, null, null);

                MobileCRM.UI.FormManager.showNewDialog(
                    "msdyn_customerasset",
                    relationShip, {
                        "@initialize": {
                        },
                        iFrameOptions: {
                            msdyn_product: new_msdyn_product,
                            msdyn_name: "New Asset"
                        }
                    }
                );             
            },
            MobileCRM.bridge.alert
        );
            }
        , true, null);
    },
}

 

The Work Order Product HTML file:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Create New Asset</title>
    <meta http-equiv='X-UA-Compatible' content='IE=edge' />
    <meta name='viewport' content='initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no'>
    <script type="text/javascript" src="../JSBridge.js"></script>
    <script type="text/javascript" src="new_WorkOrderProduct.js"></script>
</head>
<body onload="FS.new_WorkOrderProduct.createCustomAssetButton()">
</body>
</html>

 

Next you will need to create an Offline HTML file and add the iFrame onto the entity form you are opening. In this case we are opening a Customer Asset form to retrieve the values from the Work Order Product iFrame Object we were passing in above.

Below is an example of retrieving the iFrame Options from the Work Order Product record onto the Customer Asset form.

To get the values from the previous record, you need to access the values by using using entityForm.iFrameOptions

if (typeof (FS) == "undefined")
{ FS = { __namespace: true }; }

if (typeof (hisol_CustomerAsset) == "undefined")
{ hisol_CustomerAsset = { __namespace: true }; }

FS.hisol_CustomerAsset = {

    //Function to retrieve the values from the iFrame that we got from the hisol_WorkOrderProduct functions
    AddCustomerAssetDetails: function (entityForm) {
        MobileCRM.UI.EntityForm.requestObject(
            function (entityForm) {
                    var entity = entityForm.entity;
                    var options = entityForm.iFrameOptions;
                    if (entity.isNew == true) {
                        entity.properties["msdyn_product"] = options.msdyn_product;
                        entity.properties["msdyn_name"] = options.msdyn_name;                                            
                }
        },
        function (err) {
            MobileCRM.bridge.alert("An error occurred: " + err);
        },
        null
        );
    }
}

 

The Customer Asset HTML file:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Create New Asset</title>
    <meta http-equiv='X-UA-Compatible' content='IE=edge' />
    <meta name='viewport' content='initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no'>
    <script type="text/javascript" src="../JSBridge.js"></script>
    <script type="text/javascript" src="../new_WorkOrderProduct.js"></script>
    <script type="text/javascript" src="new_CustomerAsset.js"></script>
</head>
<body onload="FS.new_CustomerAsset.AddCustomerAssetDetails()">
</body>
</html>

 

This should of opened a new Customer Asset form with the ‘Name’, ‘Work Order Product’ and ‘Product’ Lookup prepopulated.

 

Hopefully this has helped 🙂

Leave a Reply

Your email address will not be published.