Sunday, April 23, 2017

Change the Status Reason field value when a record is created or updated

There are occasions when we want to change the status reason of a record when the record is being created or updated.

For the record created, you can set the default value of the status reason from customisation and then it would be the value, the record would be having with the creation.

But what if you don’t want that value to be set for certain records when the record is created. Ok, so you must be thinking we can do it using a custom code. Yes, you are correct.

The next question would be which stage you can set it. It could before the record is created or after the record is created. Again, correct. You would definitely go for before creation as the value would be set in the pipeline and there would be only one request and it would be for the creation. If you use post create, then it would trigger an update to the record so it would not be good. Hence before creation it is.

However when you try to change this in pre create, set a value to the status reason using custom code and send it, the platform would throw an error saying you cannot change the value of a status reason in pre create stage. This leaves us in a bit of pickle. So now we would have to do this using post create, which we wanted to avoid in the first place as it would do an update to the record.

Is there a way around this?

As you would have guessed by now, yes there is. We could use the pre validate stage for this. Have your custom code registered for the pre validate stage and have the status reason field changed in that. When the pre create stage arrives already the value is set and the stage runs without any hiccups.

For update you can use the pre update stage and it works without any issues.

No comments:

Post a Comment

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...