Skip to main content

Notifications

Announcements

No record found.

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
  • Verified answer
    Pawar Pravin  Profile Picture
    5,237 on at
    RE: Object reference not set to an instance of an object in custom workflow activity in CRM 2016 onpremise

    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.

  • Jharana Baliyar Singh Profile Picture
    2,667 on at
    RE: Object reference not set to an instance of an object in custom workflow activity in CRM 2016 onpremise

    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

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at
    RE: Object reference not set to an instance of an object in custom workflow activity in CRM 2016 onpremise

    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

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 97

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 82 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans