简体   繁体   中英

How to set if condition attribute Name bool in Dynamics CRM

I have some simple code to check if data exist in entity, when I save new data with isoverdue = true and data exists, it will show alert that data exists.

private string _entityname = "tss_rating";

QueryExpression Query = new QueryExpression(_entityname);  
Query.ColumnSet = new ColumnSet(true);
Query.Criteria.AddCondition("tss_isoverdue", ConditionOperator.Equal, true);

EntityCollection Items = organizationService.RetrieveMultiple(Query);

if (Items.Entities.Count > 0)
{
    Entity entity = new Entity(_entityname);

    if (entity.Contains("tss_isoverdue") && entity.Attributes["tss_isoverdue"].Equals(true))   // this if condition cannot be fire when input data
    {
        throw new Exception("IsOverdue with Yes Value is Exist....cannot create same data...!");
    }
}

Need suggestions.

UPDATE CODE AND IT WORKED:

public void Form_OnCreate_PreOperation(IOrganizationService organizationService, IPluginExecutionContext pluginExceptionContext, Entity CurrentEntity)
        {
            try
            {
                QueryExpression Query = new QueryExpression(pluginExceptionContext.PrimaryEntityName);  
                Query.ColumnSet = new ColumnSet(true);
                Query.Criteria.AddCondition("tss_isoverdue", ConditionOperator.Equal, true);

                EntityCollection Items = organizationService.RetrieveMultiple(Query);
                if (Items.Entities.Count > 0)
                {
                    if (CurrentEntity.Contains("tss_isoverdue"))
                    {
                        if ((bool)CurrentEntity.Attributes["tss_isoverdue"] == true)
                            throw new Exception("IsOverdue with Yes Value is Exist....cannot create same data...!");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(_classname + ".Form_OnCreate_PreOperation: " + ex.Message.ToString());
            }
        }

It's not entirely clear what you are asking.

I think you are trying to: Make a function that will throw an error if it finds a tss_rating record, where tss_isoverdue is true .

I'm going to guess this isn't working.

I think the problem is here: Entity entity = new Entity(_entityname);

You are making a new Entity object which won't have any attributes, which means entity.Contains("tss_isoverdue") will return false.

So your if condition will never be true.

You should either;

Use an Entity object from the returned collection, eg Items.Entities.First() .

Simplify your code to this. You don't need to check the Entity object tss_isoverdue value again, as you have already done that in your query. Knowing that at least one record was returned means at least one record has tss_isoverdue is true

EntityCollection Items = organizationService.RetrieveMultiple(Query);

if (Items.Entities.Count > 0)
{
    throw new Exception("IsOverdue with Yes Value is Exist....cannot create same data...!");
}

A side point, you probably want to use entity.Attributes["tss_isoverdue"] == true , rather than entity.Attributes["tss_isoverdue"].Equals(true) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM