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

Javascript code to populate new record

$
0
0

I need some help in updating an existing script to some changes made to two CRM forms. The forms are NEW AMBULANCE SERVICE and NEW PER DIEM

The fields on NEW AMBULANCE SERVICE are “new_operator”, “rc_nonstaffoperator”, “new_paramedicemt”, “rc_nonstaffparamedic”, “subject” and “new_region”

“new_operator” is a look up field whose target record type is STAFF. However, if the value of “new_operator” is equal to “Non Staff”, the field “rc_nonstaffoperator” becomes visible.

“rc_nonstaffoperator” is a lookup field whose target record type is EXTERNAL STAFF

“new_paramedicemt” is a look up field whose target record type is STAFF. However, if the value of “new_paramedicemt” is equal to “Non Staff”, the field “rc_nonstaffparamedic” becomes visible.

“rc_nonstaffparamedic” is a lookup field whose target record type is EXTERNAL STAFF

“new_region” is a lookup field whose target record type is REGION

“subject” is a text field

 

The fields in NEW PER DIEM are “rc_beneficiary”, “new_staffname”, “new_nonstaffname”, “new_region” and “new_patientdetails”

“rc_beneficiary” is an option set with two values, STAFF (947,950,000) and NON STAFF (947,950,001)

If “rc_beneficiary” is set to STAFF, “new_staffname” is visible and it is a lookup field whose target record type is STAFF

If “rc_beneficiary” is set to NON STAFF, “new_nonstaffname” is visible and it is a lookup field whose target record type is EXTERNAL STAFF

“new_region” is a lookup field whose target record type is REGION

“subject” is a text field

 

The script below populates the fields in NEW PER DIEM based on information in NEW AMBULANCE SERVICE

function getCallDetails() {

 

    var ambulance_service = Xrm.Page.data.entity.attributes.get("new_ambulanceservice").getValue();

 

if(ambulance_service!=null){

    ambulance_service = Xrm.Page.data.entity.attributes.get("new_ambulanceservice").getValue()[0].id;

    var cleanambulanceserviceid=ambulance_service.slice(1,-1);

//alert(cleanambulanceserviceid);

 

var req = new XMLHttpRequest();

req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_ambulanceservices("+cleanambulanceserviceid+")?$select=_new_operator_value,_new_paramedicemt_value,_new_region_value,subject", true);

req.setRequestHeader("OData-MaxVersion", "4.0");

req.setRequestHeader("OData-Version", "4.0");

req.setRequestHeader("Accept", "application/json");

req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

req.onreadystatechange = function() {

    if (this.readyState === 4) {

        req.onreadystatechange = null;

        if (this.status === 200) {

            var result = JSON.parse(this.response);

            var _new_operator_value = result["_new_operator_value"];

            var _new_operator_value_formatted = result["_new_operator_value@OData.Community.Display.V1.FormattedValue"];

            var _new_operator_value_lookuplogicalname = result["_new_operator_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

            var _new_paramedicemt_value = result["_new_paramedicemt_value"];

            var _new_paramedicemt_value_formatted = result["_new_paramedicemt_value@OData.Community.Display.V1.FormattedValue"];

            var _new_paramedicemt_value_lookuplogicalname = result["_new_paramedicemt_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

            var subject = result["subject"];

            var _new_region_value = result["_new_region_value"];

            var _new_region_value_formatted = result["_new_region_value@OData.Community.Display.V1.FormattedValue"];

            var _new_region_value_lookuplogicalname = result["_new_region_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

       

        //alert(_new_operator_value+_new_operator_value_formatted);

        //alert(subject+_new_region_value+_new_region_value_formatted);

        //set subject value

        Xrm.Page.getAttribute("new_patientdetails").setValue(subject);

       

        if(_new_region_value){

        var regionLookup=new Array();

                    regionLookup[0]=new Object();

                    regionLookup[0].id="{"+_new_region_value+"}";

                    regionLookup[0].name=_new_region_value_formatted;

                    regionLookup[0].entityType=_new_region_value_lookuplogicalname;

                    Xrm.Page.getAttribute("new_region").setValue(regionLookup);

           

           

        }

       

        //set operator value to staff

        var staffLookup=new Array();

                    staffLookup[0]=new Object();

                    staffLookup[0].id="{"+_new_operator_value+"}";

                    staffLookup[0].name=_new_operator_value_formatted;

                    staffLookup[0].entityType=_new_operator_value_lookuplogicalname;

                    staffLookup[0].entityType="new_staff";

                    Xrm.Page.getAttribute("new_staffname").setValue(staffLookup);

                   

                   

                   

                   

                   

                   

                    //end of set operator value

                   

                    //check if staff operator already set

                   

                    var ambulance_service = Xrm.Page.data.entity.attributes.get("new_ambulanceservice").getValue()[0].id;

var staff_id= Xrm.Page.data.entity.attributes.get("new_staffname").getValue()[0].id;

var cleanambulanceserviceid=ambulance_service.slice(1,-1);

var cleanedstaff_id=staff_id.slice(1,-1);

var fetchXml1 = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\">" +

 

                  "<entity name=\"new_perdiem\">" +

 

                    "<attribute name=\"new_staffname\" />" +

 

                    "<attribute name=\"new_ambulanceservice\" />" +

 

                    "<attribute name=\"new_patientdetails\" />" +

 

                    "<order attribute=\"new_patientdetails\" descending=\"false\" />" +

 

                    "<filter type=\"and\">" +

 

                      "<condition attribute=\"new_ambulanceservice\" operator=\"eq\" value=\""+cleanambulanceserviceid+"\" />" +

                      "<condition attribute=\"new_staffname\" operator=\"eq\" value=\""+cleanedstaff_id+"\" />" +

 

                    "</filter>" +

 

                  "</entity>" +

 

                "</fetch>";

 

 

 

var encodedFetchXml1 = encodeURI(fetchXml1);

 

var queryPath1 = "/api/data/v8.0/new_perdiems?fetchXml=" + encodedFetchXml1;

 

var requestPath1 = Xrm.Page.context.getClientUrl() + queryPath1;

 

var req1 = new XMLHttpRequest();

 

req1.open("GET", requestPath1, true);

 

req1.setRequestHeader("Accept", "application/json");

 

req1.setRequestHeader("Content-Type", "application/json; charset=utf-8");

 

req1.onreadystatechange = function ()

 

{

 

    if (this.readyState === 4)

 

    {

 

        this.onreadystatechange = null;

 

        if (this.status === 200)

 

        {

 

            var returned = JSON.parse(this.responseText);

 

            var results = returned.value;

 

 

 

            for (var i = 0; i < results.length; i++)

 

            {

 

                var new_staffname = results[i]["new_staff"];

                var resultvalue=results.length;

               

                if(resultvalue>=1){

                   

                    //alert("More Results Found");

                    //set operator value to staff

        var staffLookup=new Array();

                    staffLookup[0]=new Object();

                    staffLookup[0].id="{"+_new_paramedicemt_value+"}";

                    staffLookup[0].name=_new_paramedicemt_value_formatted;

                    staffLookup[0].entityType=_new_paramedicemt_value_lookuplogicalname;

                    staffLookup[0].entityType="new_staff";

                    Xrm.Page.getAttribute("new_staffname").setValue(staffLookup);

                   

                    //end of set operator value

                   

                }else{

                    return null;

                    //alert("No Result Found");

                }

 

 

               

                //alert(results.length);

               

 

            }

            //alert(accountName)

 

        }

 

        else

 

        {

 

            alert(this.statusText);

 

        }

 

    }

 

};

 

req1.send();

 

                   

                   

                   

                   

                   

                   

                    //end of check if staff operator is set

                   

                   

       

       

        } else {

            Xrm.Utility.alertDialog(this.statusText);

        }

    }

};

req.send();


   

}

 

}

 

 

Please assit in modifying the script to do the following;

If “new_operator” is equal not equal to “Non Staff”, set the field “rc_beneficiary” to STAFF and add the value in “new_operator” to “new_staffname”

If “new_operator” is equal to “Non Staff”, set the field “rc_beneficiary” to NON STAFF and leave the and add the value in “rc_nonstaffoperator” to “new_nonstaffname”

For the second NEW PER DIEM record, if “new_paramedicemt” is equal not equal to “Non Staff”, set the field “rc_beneficiary” to STAFF and add the value in “new_paramedicemt” to “new_staffname”

If “new_paramedicemt” is equal to “Non Staff”, set the field “rc_beneficiary” to NON STAFF and leave the and add the value in “rc_nonstaffparamedic” to “new_nonstaffname”


Viewing all articles
Browse latest Browse all 82002

Latest Images

Trending Articles



Latest Images