web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to get entity attribute value

(0) ShareShare
ReportReport
Posted on by 437

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

I have the same question (0)
  • Suggested answer
    M I Mostafa Profile Picture
    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

  • Suggested answer
    M I Mostafa Profile Picture
    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
    a33ik Profile Picture
    84,331 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
    ashlega Profile Picture
    34,477 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:)

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

    Hi,

    What is the datatype of the fields xx1 and xx3?

  • Verified answer
    Community Member Profile Picture
    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

  • a33ik Profile Picture
    84,331 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.

  • Community Member Profile Picture
    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
    84,331 Most Valuable Professional on at
    RE: How to get entity attribute value

    I agree. But this can confuse not experienced developer.

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

    Thanks a lot. It worked

    Sorry for late reply .

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans