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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin Update entity from another entity

(0) ShareShare
ReportReport
Posted on by 87

Hi All,

I am trying to update a record on case entity. A field on case record needs to be updated with a value from another entity(labor rates) based on certain criteria. Below is the code I have used.

Entity PostEntity = (Entity)Context.PostEntityImages["POST_IMAGE"];
Entity a = (Entity)Context.InputParameters["Target"];
if (PostEntity.LogicalName != "incident")
{
return;
}
if (Context.MessageName == "Create" || Context.MessageName == "Update")
{

if (PostEntity.Attributes.Contains("new_sitecountry"))
{
string siteCountry = PostEntity.GetAttributeValue<string>("new_sitecountry");
string serviceProviderPreference = PostEntity.GetAttributeValue<string>("new_3rdpartyinstallationordealerreplacement");
int serviceProviderType = ((OptionSetValue)PostEntity["new_fieldactiontype"]).Value;
// Query the corresponding values from Labor rates entity

QueryExpression laborRateQuery = new QueryExpression("new_laborrates"); //Another Entity labor rates
ColumnSet rateColumns = new ColumnSet("new_country", "new_serviceprovidertype", "new_inspectionlaborrate", "new_cableinstalllaborrate", "new_replacementlaborrate", "new_laborratesid");
laborRateQuery.ColumnSet = rateColumns;
laborRateQuery.Criteria.AddCondition("new_country", ConditionOperator.Equal, siteCountry);
laborRateQuery.Criteria.AddCondition("new_serviceprovidertype", ConditionOperator.Equal, serviceProviderPreference);

EntityCollection rates = Service.RetrieveMultiple(laborRateQuery);

if(rates.Entities.Count == 0)
{

#if DEBUG
debugString += "\nRecord not found: ";
#endif

}

else
{
foreach (var v in rates.Entities)
{

Guid RateId = (Guid)v.Attributes["new_laborratesid"];   //primary key of labor rates entity
a["new_fieldactionlaborrate"] = new EntityReference("new_replacementlaborrate", RateId); 
}
}

Service.Update(a);

After scratching my head for so long I am not able to find out why I am getting the error:

System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Incorrect attribute value type System.String (Fault Detail is equal to Exception details: 
ErrorCode: 0x80040203
Message: Incorrect attribute value type System.String; 
[Microsoft.Crm.ObjectModel: Microsoft.Crm.ObjectModel.TargetAttributeValidationPlugin]
[fc743e7d-fbea-4695-bdb9-7d78334c8474: TargetAttributeValidationPlugin]


TimeStamp: 2019-03-20T06:00:11.7932911Z
OriginalException: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Incorrect attribute value type System.String (Fault Detail is equal to Exception details: 
ErrorCode: 0x80040203
Message: Incorrect attribute value type System.String; 
[Microsoft.Crm.ObjectModel: Microsoft.Crm.ObjectModel.TargetAttributeValidationPlugin]
[fc743e7d-fbea-4695-bdb9-7d78334c8474: TargetAttributeValidationPlugin].

The fields "new_fieldactionlaborrate" and "new_replacementlaborrate" are  whole number fields. Please help to resolve this error.

Thanks

Amrita

*This post is locked for comments

I have the same question (0)
  • Verified answer
    David Jennaway Profile Picture
    14,065 on at

    The problem is that you are using an EntityReference, which only applies for lookup fields. As your attributes are both whole number fields, you can directly assign one to the other:

    a["new_fieldactionlaborrate"] = v.Attributes["new_replacementlaborrate"];


  • Community Member Profile Picture
    on at

    Hi

    Since you are updating the target directly this plugin is going in infinite loop.

    Entity PostEntity = (Entity)Context.PostEntityImages["POST_IMAGE"];

    Entity a = (Entity)Context.InputParameters["Target"];

    if (PostEntity.LogicalName != "incident")

    {

    return;

    }

    if (Context.MessageName == "Create" || Context.MessageName == "Update")

    {

    if (PostEntity.Attributes.Contains("new_sitecountry"))

    {

    string siteCountry = PostEntity.GetAttributeValue<string>("new_sitecountry");

    string serviceProviderPreference = PostEntity.GetAttributeValue<string>("new_3rdpartyinstallationordealerreplacement");

    int serviceProviderType = ((OptionSetValue)PostEntity["new_fieldactiontype"]).Value;

    // Query the corresponding values from Labor rates entity

    QueryExpression laborRateQuery = new QueryExpression("new_laborrates"); //Another Entity labor rates

    ColumnSet rateColumns = new ColumnSet("new_country", "new_serviceprovidertype", "new_inspectionlaborrate", "new_cableinstalllaborrate", "new_replacementlaborrate", "new_laborratesid");

    laborRateQuery.ColumnSet = rateColumns;

    laborRateQuery.Criteria.AddCondition("new_country", ConditionOperator.Equal, siteCountry);

    laborRateQuery.Criteria.AddCondition("new_serviceprovidertype", ConditionOperator.Equal, serviceProviderPreference);

    EntityCollection rates = Service.RetrieveMultiple(laborRateQuery);

    if(rates.Entities.Count == 0)

    {

    #if DEBUG

    debugString += "\nRecord not found: ";

    #endif

    }

    else

    {

    foreach (var v in rates.Entities)

    {

    Guid RateId = (Guid)v.Attributes["new_laborratesid"];   //primary key of labor rates entity

    Entity caseupdate = new Entity("incident");

    caseupdate.Id = a.Id;

    caseupdate["new_fieldactionlaborrate"] = new EntityReference("new_replacementlaborrate", RateId);

    Service.Update(caseupdate);

    }

    }

  • Amrita P Profile Picture
    87 on at

    David,

    Thanks. It helped for me. I am new to CRM. Can you please let me know where I can get sample code for similar plugins.

    Thanks

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans