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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Object reference not set to an instance of an object in custom workflow activity in CRM 2016 onpremise

(0) ShareShare
ReportReport
Posted on by 2,667

Hi All,

I am getting object reference not set to an instance of an object from below code :

Below error message :

Unhandled Exception: Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute): BMS_InternalCRM.WorkflowActivities.User.GetUserBySecurityRole: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)
at Microsoft.Crm.Workflow.Services.ProxyCustomActivity.Execute(CodeActivityContext executionContext)

Whats wrong in the below code any idea ?

public sealed class GetUserBySecurityRole : CodeActivity
    {
        [Input("Security Role")]
        public InArgument<string> SecurityRole { getset; }
 
        [Output("User")]
        [ReferenceTarget("systemuser")]
        public OutArgument<EntityReference> User { getset; }
 
        IOrganizationService service = null;
        OrganizationServiceContext organizationServiceContext = null;
 
        protected override void Execute(CodeActivityContext executionContext)
        {
            //Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();
 
            //Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            service = serviceFactory.CreateOrganizationService(context.UserId);
 
            //create the organization service context that gives the ability of querying
            organizationServiceContext = new OrganizationServiceContext(service);
 
            string securityRole = SecurityRole.Get<string>(executionContext);
 
            //get the first user id from the security  role
            EntityReference userId = GetUserFromRoleName(securityRole);
 
            if (userId == null)
                throw new Exception("No user with security role " + securityRole + " was found.");
 
            //set the user as the output parameter
            this.User.Set(executionContext, userId);
        }
 
        private EntityReference GetUserFromRoleName(string rolename)
        {
            var user = (from u in organizationServiceContext.CreateQuery("systemuser")
                        join ur in organizationServiceContext.CreateQuery("systemuserroles"on (Guid)u["systemuserid"equals (Guid)ur["systemuserid"]
                        join r in organizationServiceContext.CreateQuery("role"on (Guid)ur["roleid"equals (Guid)r["roleid"]
                        where ((string)r["name"]) == rolename
                        select new
                        {
                            Id = u.Contains("systemuserid") ? (Guid)u["systemuserid"] : Guid.Empty,
                        }).FirstOrDefault();
            EntityReference userRecord = new EntityReference("systemuser", user.Id);
 
            return userRecord;
        }
    }
Please let me know if any one knows :
Thanks,
Jharana
I have the same question (0)
  • Suggested answer
    Pawar Pravin Profile Picture
    5,237 on at

    Have you tried to debug your code ?

    It looks like somewhere you are getting value as null and hence it's not allowing to assign or pass null value. Check if you are getting all input parameter values using plugin trace log or debug once

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi Pravin,

    Thanks for the reply.I cant debug this code as i dont have access to server as its onpremise crm.

    This custom dll is calling on oob wf & in wf its passing Security Role as single line of text with Sales Manager value.

    Thanks,

    Jharana

  • Verified answer
    Pawar Pravin Profile Picture
    5,237 on at

    Thanks for clarification on your requirement. I would rather say you can convert your code into console application (Pass hard coded valued as input to the console app) and test it.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Hamza H Profile Picture

Hamza H 142 Super User 2026 Season 1

#2
Nagaraju_Matta Profile Picture

Nagaraju_Matta 128

#3
11manish Profile Picture

11manish 121

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans