Skip to main content

Notifications

Microsoft Dynamics CRM forum
Suggested answer

Try-Catch Issue in Async Plugin

Posted on by
Hi, 
 
i have a plugin triggering on create post operation asynchronously .
Basically i am looping on a list of systemusers, and for each user if they do not have a certain role assign, im assigning it to them.
The assignment is taking place in a try-catch, whereby im updating techfields values (ERROR to No, Description to Blank and SyncDate to Now) if try block executing
else if  it goes in the catch(ERROR to Yes, Description to theExceptionMessage and SyncDate to Now)
 
The codes in my catch block  to set the variables doesnt seem to be executing as even for the error rows my ERROR field remains No, and description remains blank. Can anybody help?
Find below a code snippet
 
 
   foreach (var user in users.Entities)
                    {
                        User userEntity = user.ToEntity<User>();
                        bool errored = false;
                        String error = //;
 
                          try
                            {
 
                                var userRolesCollection = GetService<CompanyService>().getUserRoles(userEntity.Id);
                                if (userRolesCollection != null && userRolesCollection.Entities.Any())
                                {
 
                                    // Check if the role to be associated is already present in the user's roles
                                    bool roleAlreadyAssigned = userRolesCollection.Entities.Any(entity =>
                                    {
                                        var roleId = (Guid)entity[/roleid/];
                                        return roleId == roleWithNewBU.Id;
                                    });
 
                                    if (!roleAlreadyAssigned)
                                    {
                                        GetRepository<UserRepository>().AssociateUserRole(userEntity.Id, role);
                                       
 
                                    }
 
                                }
 
                            }
                            catch (Exception e)
                            {
                                errored = true;                            
                                error = e.Message.ToString();
                               
                                if (context.Mode == 0)
                                {
 
                                    throw new InvalidPluginExecutionException(e.Message);
                                }
                               
                            }
                            finally
                            {
                                userEntity.AccessMatrixFamilySyncError = errored;
                                userEntity.AccessMatrixFamilySyncErrorDesc = error;
                                userEntity.AccessMatrixFamilySyncDate = DateTime.Now;
                                OrganizationServiceAdmin.Update(userEntity);
 
                            }
 
                           
                           
                            if (errored)
                            {
                             
                                continue;
 
                            }
                             
 
                        }
                   
  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,937 Super User on at
    Try-Catch Issue in Async Plugin
    Hi,
     
    I would suggest you to write trace log to see the execution flow.
     
     
    Thanks,
    Bipin
  • Dengliang Li Profile Picture
    Dengliang Li Microsoft Employee on at
    Try-Catch Issue in Async Plugin

Helpful resources

Quick Links

Take the Community feedback survey!

Answer this brief 15-question survey about your Community experience…

Demystifying Copilot: Service Edition with Sundar Raghavan

Sundar answers more questions about Copilot for Service...

Dynamics 365 Business Central vs Finance and SCM

Take a look at the key differences between Business Central and…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,377 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 223,308 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,140

Featured topics

Product updates

Dynamics 365 release plans