CRM 2011 Retrieving lookup

3.6k views Asked by At

I'm new in CRM development. I know a basic thing like "best practice for crm 2011" I wanna understand now how to work with lookup fields. And I think I chose the easiest way for my self.

I have an costum entity "contract" it has 5 more field, 2 of these are lookups. First lookup (agl_contractId) - it is a link by it self Second lookup (agl_ClientId) - link to Client.

What do I need?

When I choose fill First lookup (agl_contractId), script should find in this contract a Client and copy-past it to current form.

I've done script but it isn't work... (((

    function GetAccountFromContract()
{
    XrmServiceToolkit.Rest.Retrieve(Xrm.Page.getAttribute("agl_osnovnoy_dogovorid").getValue(),
       'agl_osnovnoy_dogovoridSet',
       null,null,
       function (result) {
           var Id = Xrm.Page.getAttribute("agl_osnovnoy_dogovorid").getValue();

           if (result.Id != null) {
               var LookupData = new Array();
               var LookupItem = new Object();
               var lookuptextvalue = lookupvalue[0].name;
               var lookupid = lookupvalue[0].id;
               var lokupType = lookupvalue[0].entityType;

               alert(lookupvalue);
               alert(lookupData);

               Xrm.Page.getAttribute("agl_accountid").setValue(lookupData);
           }
       },
            function (error) {
                equal(true, false, error.message);
            },
                    false
       );
  }
2

There are 2 answers

0
lazarus On

If I understand you well: When you select Contract in agl_osnovnoy_dogovorid field, you want to pull Client property from that Contract and put it in agl_accountid field?

If that is right:

First, get Id of selected Contract (from agl_osnovnoy_dogovorid field)

var selectedContract = new Array(); 
selectedContract = Xrm.Page.getAttribute("agl_osnovnoy_dogovorid").getValue();
{
     var guidSelectedContract = selectedContract[0].id; 
     //var name = selectedContract[0].name;     
     //var entType = selectedContract[0].entityType;
}

Second, retrieve Client from agl_osnovnoy_dogovorid. Your oData query will be like:

http://crmserver/org/XRMServices/2011/OrganizationData.svc/ContractSet(guid'" + guidSelectedContract + "')/CustomerId

(In example I'm using CustomerId field. For your case enter Schema Name of Client field). Now, execute query and put result into agl_accountid field:

$.getJSON(
Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/ContractSet(guid'" + guidSelectedContract + "')/CustomerId", 
function(data){
if(data.d.CustomerId != null && data.d.CustomerId.Id != null && data.d.CustomerId.Id != "undefined")
{
     //set agl_accountid field
     Xrm.Page.getAttribute("agl_accountid").setValue([{id:data.d.CustomerId.Id, name:data.d.CustomerId.Name, typename:data.d.CustomerId.LogicalName}]);
}
});
0
Adi Katz On

Your using REST to retrieve data but also using FetchXml example to setup the agl_accoutid lookup. Also some of the conditions are not clear … anyway … I’ve incorporated the change to your original post.

function GetAccountFromContract()
{
    var aodLookupValue = Xrm.Page.getAttribute("agl_osnovnoy_dogovorid").getValue();
    if (!aodLookupValue) return; 

    XrmServiceToolkit.Rest.Retrieve( aodLookupValue[0].id ,
       'agl_osnovnoy_dogovoridSet', null,null,
       function (result) {
           var customer = result.d["your attribute name"];     
           if (customer) {

               var LookupData = new Array();
               var LookupItem = new Object();
               var lookuptextvalue = customer.Name;
               var lookupid = customer.Id;
               var lokupType = customer.LogicalName;

               alert(lookupvalue);
               alert(lookupData);

               Xrm.Page.getAttribute("agl_accountid").setValue(lookupData);
           }
       },
       function (error) {
                equal(true, false, error.message);
       }, false );
}