Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Is it possible to set lookup value from primary key Id in crm 365

(0) ShareShare
ReportReport
Posted on by 2,665

Hi All,

I am using below code to set one lookup field 

if (formContext.getAttribute("wcl_technicalactivity") !== null)
var technicalactivity = formContext.getAttribute("wcl_technicalactivity").getValue();
if (technicalactivity != null && technicalactivity != undefined) {
// var newid = technicalactivity[0].id.slice(1, -1);
var recordId = formContext.data.entity.attributes.get("wcl_technicalactivity").getValue()[0].id;
var newID = recordId.toLowerCase();
if (newID !== null) {
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/campaignactivities?$select=activityid,_wcl_technicalactivity_value&$filter=_wcl_technicalactivity_value eq '" + newID + "'", 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 results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var activityid = results.value[i]["activityid"];
var _wcl_technicalactivity_value = results.value[i]["_wcl_technicalactivity_value"];
var _wcl_technicalactivity_value_formatted = results.value[i]["_wcl_technicalactivity_value@OData.Community.Display.V1.FormattedValue"];
var _wcl_technicalactivity_value_lookuplogicalname = results.value[i]["_wcl_technicalactivity_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
if (formContext.getAttribute("wcl_catechnicalactivityattendanceid") !== null)
formContext.getAttribute("wcl_catechnicalactivityattendanceid").setValue(activityid); How can it works as activityid is primary key(Record ID)
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}

Thanks,

Jharana

  • Suggested answer
    Jharana Baliyar Singh Profile Picture
    Jharana Baliyar Singh 2,665 on at
    RE: Is it possible to set lookup value from primary key Id in crm 365

    It is resolved now by taking name field & added used in below

    var req = new XMLHttpRequest();

                       req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/campaignactivities?$select=activityid,subject,_wcl_technicalactivity_value&$filter=_wcl_technicalactivity_value eq '" + newID + "'", 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 results = JSON.parse(this.response);

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

                                       if (results.value[i]["activityid"] !== null)

                                           var activityid = results.value[i]["activityid"];

                                       if (results.value[i]["subject"] !== null)

                                           var subject = results.value[i]["subject"];

                                       if (results.value[i]["_wcl_technicalactivity_value"] !== null) {

                                           var _wcl_technicalactivity_value = results.value[i]["_wcl_technicalactivity_value"];

                                           var _wcl_technicalactivity_value_formatted = results.value[i]["_wcl_technicalactivity_value@OData.Community.Display.V1.FormattedValue"];

                                           var _wcl_technicalactivity_value_lookuplogicalname = results.value[i]["_wcl_technicalactivity_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

                                       }

                                       var value = new Array(); //create a new object array

                                       value[0] = new Object();

                                       value[0].id = activityid; // set ID to ID

                                       value[0].name = subject; //set name to name

                                       value[0].entityType = "campaignactivity"; // entity schema name

                                       if (formContext.getAttribute("wcl_catechnicalactivityattendanceid") !== null)

                                           formContext.getAttribute("wcl_catechnicalactivityattendanceid").setValue(value);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans