cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
NehaSingh
New Member

How to Insert and Update Lookup fields using CDS sdk

How  we can Insert and Update Lookup fields using CDS sdk in C#.

 

var data = entity.CreateEntity();

data.SetValue("Name", "abc"); // Text field

data.SetValue("Direct", ?? ); // Lookup field

4 REPLIES 4
v-micsh-msft
Community Support
Community Support

Hi @NehaSingh,

 

Lookup fields in Common Data Service under PowerApps is defined as a record.

The formula to update this Lookup field in PowerApps would require the fields defined in the parent entity DefaultLookup group.

For example, here we have custom entity testing1 and testing2.

Testing2 has a lookup field configured with the entity testing1.

To update the lookup field in PowerApps, the formula should be:

Patch(
      Testing2,
      Gallery.selected,
       { Testing_:
          {PrimaryId:"",Text:""}
         }
     )

I am not sure how the Common Data Service defines the record in the SetValue method.

Hope staff @CWesener could share more information about this.

Or you may contact the following Email directly:

"

This feature is preview. If you are interested in participating in the preview program, contact us at cdspreviewprogs_at_microsoft.com.

"

Regards,

Michael

 

Community Support Team _ Michael Shao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
pvillads
Power Apps
Power Apps

I understand this question is about how to generally perform the operations mentioned using the C# SDK. As it happens, this is quite easy, once you understand the basics. 

 

In the SDK you treat entities like plain old C# objects (aka POCOs), or as generic entities that are not strongly defined. In the sample that was provided in the question I get the feeling that the user is using some custom entities for which he has not provided any type safe C# representation.

 

In the SDK all CRUD operations occur in batches. The code on the client tier simply queues up the work (in a socalled executor), and then sends it off to the server for execution using the asynchronous ExecuteAsync call on the executor. In you example this would look like:

 

Snippet

public static async Task GenericEntitySetUsageAsync(Client client)
{
    RelationalEntitySet genericEntitySet = client.GetRelationalEntitySet(
        new EntitySetReference("Microsoft.CommonDataService.CommonEntities",
        "ProductCategory",
        Version.Create("1.0.0")));
 
    // Create a few product categories
    var genericEntitySurface = genericEntitySet.CreateEntity();
    genericEntitySurface.SetValue<string>("Name""Surface");
    // The compiler can infer the type, so the generic argument describing
    // the type of the value is not strictly required. This is a shorthand.
    genericEntitySurface.SetValue("Description""Surface produce line");
 
    var genericEntityPhone = genericEntitySet.CreateEntity();
    genericEntityPhone.SetValue("Name""Phone");
    genericEntityPhone.SetValue("Description""Phone produce line");
 
    var insertExecutor = client.CreateRelationalBatchExecuter(
        RelationalBatchExecutionMode.Transactional);
 
    await insertExecutor
        .Insert(genericEntitySurface)
        .Insert(genericEntityPhone)
        .ExecuteAsync();
 
    // Create a query using the indexing operators
    var query = genericEntitySet.CreateQueryBuilder()
        .Where(pc => pc["Name"] == "Surface" || pc["Name"] == "Phone")
        .OrderByAscending(pc => new[] { pc["CategoryId"] })
        .Project(pc => pc.SelectField(f => f["CategoryId"])
            .SelectField(f => f["Name"])
            .SelectField(f => f["Description"]));
 
    var selectExecutor = client.CreateRelationalBatchExecuter(
        RelationalBatchExecutionMode.Transactional);
 
    OperationResult<IReadOnlyList<RelationalEntity>> queryResult = null;
    await selectExecutor
        .Query(query, out queryResult)
        .ExecuteAsync();
 
    var updateExecutor = client.CreateRelationalBatchExecuter(
        RelationalBatchExecutionMode.Transactional);
 
    // Traverse the results
    foreach (var entry in queryResult.Result)
    {
        string categoryId = "";
        object name = null;
        string description = "";
 
        // The same shorthand is applied there, where the generic type
        // parameter can be omitted. The expected type can also be 
        // passed as a parameter of type Type.
        if (entry.TryGetValue<string>("CategoryId"out categoryId)
            && entry.TryGetValue("Name"out name, typeof(string))
            && entry.TryGetValue("Description"out description))
        {
            Console.WriteLine("{0} - {1} - {2}", categoryId, name, description);
        }
    }
}

 

I hope this leaves you something to work with. You can always find more information in  https://docs.microsoft.com/en-us/common-data-service/entity-reference/ , particularly: https://docs.microsoft.com/en-us/common-data-service/entity-reference/cds-sdk-manipulate-data

 

I am trying to insert values for Lookup type field. I am able to insert and update values for other types of fields. 

But lookup relations are not getting updated.

Can you please provide any example of updating a Lookup type field using CDS sdk ?

Thank you Michael. But I am trying to update lookup fields using CDS sdk through code in C#.

Helpful resources

Announcements
Power Platform Call June 2022 768x460.png

Power Platform Community Call

Join us for the next call on August 17, 2022 at 8am PDT.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Users online (1,741)