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)

Problem creating inactive record of a custom entity

(0) ShareShare
ReportReport
Posted on by

Hi!

I'm trying to create a new record of a custom entity from a plugin, setting in the new entity object the state and statuscode in this way:

var customEntity = new Entity("new_customentity")

{

["new_name"] = chequePostImage.GetAttributeValue<string>("gval_codcheque"),
["new_cheque"] = chequeRef,
["new_chequeid"] = chequeRef.Id.ToString(),

["statecode"] = new OptionSetValue(1),
["statuscode"] = new OptionSetValue(2)

};

_service.Create(customEntity);

But CRM throw me this error: "2 is not a valid status code for state code new_customentityState.Active on new_customentity with Id a9fc4065-aa41-e911-a955-000d3ac02db8."

I know that I can update the record just after create it, but the plugin must be a sync process and call the server twice (first for create and then for update) may give me low performance. I remember this was posible in previous versions, and I'm not sure if I'm doing something diferent or it can't be done anymore

Any suggestions?

Best Regards!

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try to use

    ["statuscode"] = new OptionSetValue(-1)

    instead of

    ["statuscode"] = new OptionSetValue(2)

  • Suggested answer
    Abhishek@CRM Profile Picture
    70 on at

    1. Add some flag field to entity and import it along with entity - it will mean that record should be deactivated after creation.

    2. Add Real-Time workflow that will trigger on Create of record, check flag field from step 1 and then deactivate reco

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Hi

    Could you try the following code instead?

    var customEntity = new Entity("new_customentity")
    {
    ["new_name"] = chequePostImage.GetAttributeValue<string>("gval_codcheque"),
    ["new_cheque"] = chequeRef,
    ["new_chequeid"] = chequeRef.Id.ToString(),
    };

    var recordId =_service.Create(customEntity); // Returns the ID
    SetStateRequest state = new SetStateRequest();
    state.State = new OptionSetValue(1);
    state.Status = new OptionSetValue(2);
    state.EntityMoniker = new EntityReference(customEntity.LogicalName, recordId);
    SetStateResponse stateSet = (SetStateResponse)_service.Execute(state);

  • Community Member Profile Picture
    on at

    Hi Kokulan, thank you for your response!

    I tried that code, but that method takes two servers call to create and inactive the record. I want to do it in one server call, this procces will be executed a lot of times and I need the best performance as posible.

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    If you do not want to make two server call, you could have a flag to say make it inactive and a workflows kicks in and makes the record inactive?

    var customEntity = new Entity("new_customentity")

    {

    ["new_name"] = chequePostImage.GetAttributeValue<string>("gval_codcheque"),

    ["new_cheque"] = chequeRef,

    ["new_chequeid"] = chequeRef.Id.ToString(),

    ["new_deactivaterecord"] = true

    };

    _service.Create(customEntity);

    And your workflow triggers on Create of the record, checks this flag and deactivates if this is set to true

  • Community Member Profile Picture
    on at

    Hi Andrew! Thank you for your help!

    I tried this, using -1 in statuscode, but the record still creating in active state and active statuscode

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I had to check metadata first (you can check it as well). statecode field is not available for create. Record will be always created as active. The only way for you is to do 2 steps operation - create and set as inactive after.

  • Community Member Profile Picture
    on at

    Ok, I will do it that way

    Thank you all for the responses!

  • mesteves Profile Picture
    25 on at

    Hi @Andrew Butenko,

    I had the same issue, and just wondering...is there a logic reason for such behavior ? Why can't an account be created as inactive?

    Best regards,

    Manuel

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