Pages

Showing posts with label lookup. Show all posts
Showing posts with label lookup. Show all posts

Monday, 10 March 2014

Delete a Record in CRM using OData Query

Hi Folks,

To Delete a record in CRM, we require JQuery min 1.4 &  JSON2 script file which we can get from SDK from this specific path
SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts

Now add the following code to your webresource,
Also add JSon.js and Jquery1.4min.js to the Library 
--------------------------->
 function update()
{
    var lookupObject = Xrm.Page.getAttribute("new_lookup").getValue();  //Getting id throug lookup
    var id = lookupObject[0].id;
    var set="new_entity5Set";
    updateRecord(id, set);
}
    function updateRecord(id, odataSetName) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'"+id+"')",
    beforeSend: function (XMLHttpRequest) {
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
    },
    success: function (data, textStatus, XmlHttpRequest) {
    alert("Deleted successfully");
    },
    error: function (XmlHttpRequest, textStatus, errorThrown) {
    alert("Error while deletion " + errorThrown);
  
    }
    });
    }

--------------------------->

Update a Record in CRM using OData Query

Hi Folks,

To Update a record in CRM, we require JQuery min 1.4 &  JSON2 script file which we can get from SDK from this specific path
SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts

Now add the following code to your webresource,
Also add JSon.js and Jquery1.4min.js to the Library
------------------>
function update()
{
    var lookupObject = Xrm.Page.getAttribute("new_lookup").getValue();        //getting the id through lookup
    var id = lookupObject[0].id;
    var entity = new Object();
    entity.new_name="Hello";
    var set="your entity schema nameSet";              //entity name with Set
    updateRecord(id, entity, set);
}
    function updateRecord(id, entityObject, odataSetName) {
    var jsonEntity = window.JSON.stringify(entityObject);
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    data: jsonEntity,
    url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'"+id+"')",
    beforeSend: function (XMLHttpRequest) {
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
    },
    success: function (data, textStatus, XmlHttpRequest) {
    alert("Updated successfully");
    },
    error: function (XmlHttpRequest, textStatus, errorThrown) {
    if (XmlHttpRequest && XmlHttpRequest.responseText) {
    alert("Error while updating " + odataSetName+ " ; Error – " + XmlHttpRequest.responseText);
    }
    }
    });
    }

----------------------->

Wednesday, 19 February 2014

Filtering Records in Sub-Grid Based upon the Lookup

Hi Folks,

The Below scenario explains how to filter the Subgrid using a record selected in Lookup.

In this scenario I have a lookup for selecting the courses in Post Graduation. If we select any of the record in lookup, then the corresponding subjects must be displayed in the grid view.

The below screen shot shoes the subgrid name



For creating this we require the FetchXml code, which we can get through Advanced Find in CRM.
The below snippet code can be used to filter the records,

---------------->>

function updateSubGrid() {  
    var Subjects = document.getElementById("Subjects");
    var lookupfield = new Array;
var dept;
    lookupfield = Xrm.Page.getAttribute("lookup schema name").getValue();
    if (lookupfield != null) {
        var lookupid = lookupfield[0].id;
dept=lookupfield[0].name;                              //I am storing the name of record in "dept" variable 
}
    else 
{
        return;
}
    if (Subjects ==null || Subjects.readyState != "complete")
{
        setTimeout('updateSubGrid()', 2000);
        return;
}    
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    fetchXml += "<entity name='new_subjects'>";
    fetchXml += "<attribute name='new_subjectsid' />";
    fetchXml += "<attribute name='new_name' />";
    fetchXml += "<attribute name='createdon' />";
    fetchXml += "<order attribute='new_name' descending='false' />";
    fetchXml += "<filter type='and'>";
    fetchXml += "<condition attribute='new_dept' operator='eq' uiname='"+ dept +"' uitype='new_department' value='" + lookupid + "' />";                                                               //dept specifies the name of record and lookupid represents Guid
    fetchXml += "</filter>";
    fetchXml += "</entity>";
    fetchXml += "</fetch>";
    Subjects.control.SetParameter("fetchXml", fetchXml);
    Subjects.control.refresh(); 

}   

Tuesday, 11 February 2014

On Selecting one lookup field another lookup field sets automatically

Hi Folks,
Client had a requirement in which if we select one lookup field then the another lookup field must get set automatically. for doing this we have three entities. Lets call A, B and C as three different entities. A entity has a lookup of B entity, so A entity is mapped to B entity. Now C entity has a lookup of A and lookup of B, so here the requirement is that if we select a record in A lookup then the B lookup must set automatically(Since A entity and B entity are mapped by a lookup).

For doing these we can use OData Query. You can download the Odata Query Designer from above link

http://crm2011odatatool.codeplex.com/

The above snippet code can be used for doing these operations
------------------->
function customentity_onchange() {

debugger;
    var lookupObject = Xrm.Page.getAttribute("A_type lookup fied schema name").getValue();
    var z=lookupObject[0].id; // Guid of lookup
    if (z != null) {
        var entityA="A_entitynameSet";            //Entity name of A and "Set" is used in Odataquery           
        var field = "B-type lookup schema name in A entity";        //Schema name of a lookup name in A entity
        var a = GetAdata(z,entityA,field);               //calling function to get the details
        if(a!=null)
        {
        alert(a[0]["B-type lookup schema name in A entity].Name);
        var lookupData = new Array();
        var lookupItem = new Object();
        lookupItem.id=a[0]["B-type lookup schema name in A entity"].Id;
        alert(lookupItem.id);
        lookupItem.name = a[0]["B-type lookup schema name in A entity"].Name;
        alert(lookupItem.name);
        lookupItem.entityType ="B-Entity Schema name";
        alert(lookupItem.entityType);
        lookupData[0] = lookupItem;
        Xrm.Page.getAttribute("B type lookup field schema name in C entity").setValue(lookupData);
             }
        }
    }
function GetAdata(z,entityA,field) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var oDataUri = serverUrl + "/XRMServices/2011/OrganizationData.svc/"+entityA+"(guid'" + z + "')?$select="+field;                  //retrieving B type lookup field details of a particular record using GUID
    var jSonArray = new Array();
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataUri,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.           
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
            //alert(hai);
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data && data.d != null) {
                jSonArray.push(data.d);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
        alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
            //alert("Error :  has occured during retrieval of the A details");
    }
    });

    return jSonArray;
}
--------------->

Sunday, 26 January 2014

Set Multi-Select Lookup in CRM 2011

Basically MSD CRM 2011 does not contains Multi-Select Lookup . So to set Multi-Select Lookup in CRM we need to go for coding. The below Snippet is essential for Multi-Select Lookup. If we place the below code in "On-Load" event of  a form then the lookup will be a Multi-Select Lookup.

function  load()
{
document.getElementById("lookup Schema Name").setAttribute("lookupstyle", "multi");
}

The above syntax makes the Lookup Feild as Multi-Select. suppose if we replace "multi" by "single" in the above code, then the lookup will be normal lookup.

The above is not enough, it is just use to select mutiple records in lookup. but we want to retrive those selected record details. I am taking a scenario in which I am Slelcting multiple records from a lookup and Placing those selected record details in one multiline-textfeild and again retrieving those field in the same lookup.
So this snippet consists of two functions for On-Load and On-Save.



Code for On-Load
------->>
function  load()
{
document.getElementById("lookup schema name").setAttribute("lookupstyle", "multi");
if(Xrm.Page.ui.getFormType()!=1)
{
var data=new Array();
// I created a one multiline text box to save the lookup values.
// set the multiline textbox visible equal to false.
var store=Xrm.Page.getAttribute("schema name of multiple text feild").getValue();
if(store!=null)
data=store.split(";");
// To get the "Target record type" Go to your form Customization ---> double click on your lookup--->Details--->Edit--->Here you can see "Target Record Type"
var typename = "Target Record Type";   //its mandatory to mention type name
 var arr= new Array();
 var  i=0;
var   j=0;
for(i=0;i<((data.length-1)/2);i++)
{
arr[i] = new Object();
arr[i].name = data[j];
arr[i].id= data[j+1];
arr[i].typename= typename ;
j++;
j=j+1;
}
crmForm.all["lookup schema name"].DataValue = arr;
}
}




Code for On-Save
On Saving, the selected records from lookup are stored in a MultiLine text-box with GUID.. so that it can be used while retrieving the record details.
-------->>
function save() {

var value = crmForm.all["lookup schema name"].DataValue;
var s;
var temp = "";

for (s = 0; s < value.length; s++) {
var temp2 = "";
temp2 = value[s].name+";"+value[s].id+";";
temp = temp+""+temp2;
}
Xrm.Page.getAttribute("schema name of multiple text feild").setValue(temp);
Xrm.Page.getAttribute("lookup schema name").setValue(null);
document.getElementById("lookup schema name").setAttribute("lookupstyle", "single");
}