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 :
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

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans