Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to get entity attribute value

(0) ShareShare
ReportReport
Posted on by 435

Hi guys , 

I have written a batch job to get value of entity attribute while looping through entity .

I m using on line crm 2015.

foreach (Entity r in ec.Entities)
{
if (r.Attributes.Contains("xxx1") && r.Attributes.Contains("xxx2"))
{
if (Convert.ToDateTime(r.Attributes["xxx3"]) <= DateTime.Now)
{
r.Attributes["xxx1"] = null;
try
{
crmService.Update(r);
msg = "entity updated successfully";
CRMHelperClass.writeToTextFile(msg);
}
catch (Exception)
{
msg = "Error while updating" + Environment.NewLine;
CRMHelperClass.writeToTextFile(msg);
string k1 =r["xxx1"].ToString(); //giving error
string k2 = r.Attributes["xxx3"].ToString();  // giving error


msg  = k1 +  ","  +   k2 ;
CRMHelperClass.writeToTextFile(msg);
msg = "**************************************************************************************************************************************************";
CRMHelperClass.writeToTextFile(msg);

}
}
}
}

*This post is locked for comments

  • windyMill Profile Picture
    windyMill 435 on at
    RE: How to get entity attribute value

    Thanks a lot. It worked

    Sorry for late reply .

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to get entity attribute value

    I agree. But this can confuse not experienced developer.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get entity attribute value

    LOL. don't be so particular about this.

    We are not living in a compiler world : )

    Anyway.. good catch!

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to get entity attribute value

    melvin fong 

    Just FYI. Your code could not be compiled because Entity doesn't have getAttributeValue method. It has GetAttributeValue if I'm not wrong.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get entity attribute value

    use the shortcut way...

    var x_value = r.getAttributeValue<string>("xxx1"); //if xxx1 is string

    or

    var x_value = r.getAttributeValue<int>("xxx1"); //if xxx1 is integer

  • Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: How to get entity attribute value

    Hi,

    What is the datatype of the fields xx1 and xx3?

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to get entity attribute value

    string k1 =r["xxx1"].ToString();

    It's giving you error because you are setting it to null before:

    r.Attributes["xxx1"] = null;

    Not sure you ever get to k2 if you are getting an error for k1:)

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to get entity attribute value

    Try following:

    string k1 = (r.Contains("xxx1") && r["xxx1"] != null) ? r["xxx1"].ToString() : "null";

    string k2 = (r.Contains("xxx3") && r["xxx3"] != null) ? r["xxx3"].ToString() : "null";

  • Suggested answer
    M I Mostafa Profile Picture
    M I Mostafa 4,257 on at
    RE: How to get entity attribute value

    Just to add, from the same reference link above, you could get attribute values using any of the below approaches/examples:

    Task

    Example

    Access an attribute by name

    var nameAttribute = Xrm.Page.getAttribute("name");

    Assigns the attribute for the Account Name field to the nameAttribute variable

    Access an attribute by index

    var firstAttribute = Xrm.Page.getAttribute(0);

    Assigns the first attribute in the Xrm.Page.data.entity.attributes collection to the firstAttribute variable.

    Access all attributes

    var allAttributes = Xrm.Page.getAttribute();

    Assigns an array of all the attributes in the Xrm.Page.data.entity.attributes collection to the allAttributes variable.

    Access all attributes that meet a specific criteria

    var optionsetAttributes = Xrm.Page.getAttribute(function (attribute, index) {
    return attribute.getAttributeType() == "optionset";
    });

    Assigns an array of all the attributes in the Xrm.Page.data.entity.attributes collection that meet the criteria set in the anonymous function, which returns true when the attribute type is “optionset” to the optionsetAttributes variable.

    Hope this Helps!

  • Suggested answer
    M I Mostafa Profile Picture
    M I Mostafa 4,257 on at
    RE: How to get entity attribute value

    Just to confirm, the error you are getting is related to these 2 lines:

    string k1 =r["xxx1"].ToString(); //giving error

    string k2 = r.Attributes["xxx3"].ToString();  // giving error

    If that's the case, could you explain a bit more what you are trying to do? Are you just trying to get an attribute value from an entity form client side?

    if the answer is yes, here is a simple example and reference to how to do it:

    msdn.microsoft.com/.../jj602964.aspx

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,253 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans