Friday, October 30, 2015

Sample CRUD Operations Using Javascript in MSCRM

function CreateRecords()
{
    XrmSvcToolkit.createRecord({
        entityName: "Contact",
        entity: { 
   FirstName: "Rahul", 
   LastName: "Veldandi", 
   Address1_City: "Hyderabad",
   new_AccountName:{Id:"A478321B-E44C-E511-8102-C4346BADA644",LogicalName:"account",Name:"A. Datum Corporation (sample)"},//Lookup
   PreferredContactMethodCode:{Value:1},//Optionset
   AnnualIncome:{Value:"15000"},//Currency
   ExchangeRate:"50.600",//Decimal
   new_Gender:false,//Two Options
   BirthDate:new Date(1995,02,08),//DateTime
   Address1_Telephone2:"9876543210"
},
        async: false,
        successCallback: function (result) {
            var contactId = result.ContactId;
            // Do here want you want with the result
            alert("record created");
        },
        errorCallback: function (error) {
alert(error.message)
            alert("Unable to create record");
        }
    });
}

function UpdateRecords()
{
    XrmSvcToolkit.updateRecord({
        entityName: "Contact",
id:"32A1B4ED-D95D-E511-810E-C4346BAD3768",
        entity: { 
Address1_City: "Hyderabad",
Address1_Telephone2:"9876543210",
AnnualIncome:{Value:"2,20,000"},
PreferredContactMethodCode:{Value:2}
},
        async: false,
        successCallback: function (result) {
            //Do here want you want with the result
            alert("record updated");
        },
        errorCallback: function (error) {
            alert("Failed to update record");
        }
    });
}

function DeleteRecords()
{
XrmSvcToolkit.deleteRecord({
        entityName: "Contact",
id:"FD6EC9BA-5E5B-E511-8110-C4346BAD26CC",
        async: false,
        successCallback: function (result) {
            //Do here want to want with the result
            alert("record deleted");
        },
        errorCallback: function (error) {
            alert("Failed to delete record");
        }
    });
}

function Retrieve()

var lookup;
var name;
var curencyfield;
var curency;
var decimal;
var phonnmbr;
var DOB;
var id = "32A1B4ED-D95D-E511-810E-C4346BAD3768"
XrmSvcToolkit.retrieve({
        entityName: "Contact",
id:id,
/*select:["FirstName","LastName", "Address1_City", "Address1_Telephone2"],*/
        async: false,
        successCallback: function (result) {
lookup= Xrm.Page.getAttribute("new_accountname").getValue();
name= lookup[0].name;
            curency=Xrm.Page.getAttribute("annualincome").getValue();
             decimal=Xrm.Page.getAttribute("exchangerate").getValue();
           DOB=Xrm.Page.getAttribute("birthdate").getValue();
             alert("FirstName : "+result.FirstName+"\nLastName : "+result.LastName+"\ncity : "+result.Address1_City+"\nTeliphone number : "+result.Address1_Telephone2+"\nAccountname : "+name+"\nAnnual Income : "+curency+"\nExchange Rate : "+decimal+"\nDate Of Borth : "+DOB) 
        },
        errorCallback: function (error) {
            alert("Unable to retrieve record");
        }
    });
}

function multiretrieve()
{
    XrmSvcToolkit.retrieveMultiple({
        entityName: "Contact",
       odataQuery:"?$select=FirstName,LastName,Address1_City,new_AccountName,PreferredContactMethodCode,AnnualIncome,ExchangeRate,new_Gender,BirthDate,Address1_Telephone2&$filter=new_AccountName/Id eq guid'A478321B-E44C-E511-8102-C4346BADA644'",
        async: false,
        successCallback: function (results) {
alert("records retrieved");
var str = "";
            for (var i = 0;i<results.length; i++) 
            {
                str += "Name: "+results[i].FirstName+ "\n\n";
            }
            alert(str);
        },
        errorCallback: function (error) {
            alert("Unable to retrieve records..Please Try Again");
        }
    });
}

function multiretrievel()
{
    XrmSvcToolkit.retrieveMultiple({
        entityName: "Contact",
       odataQuery:"?$select=FirstName,LastName&$filter=new_AccountName/Id eq guid'A478321B-E44C-E511-8102-C4346BADA644'",
        async: false,
        successCallback: function (results) {
alert("records retrieved");
var str = "";
            for (var i = 0;i<results.length; i++) 
            {
                str += "Full Name :"+results[i].FirstName+"  "+results[i].LastName+"\n";
            }
            alert(str);
        },
        errorCallback: function (error) {
            alert("There was an error when retrieving the AccountSubgrid records\n"+error);
        }
    });
}

No comments:

Post a Comment