Skip to main content

Notifications

Announcements

No record found.

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,665

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
    Pawar Pravin 5,227 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
    Jharana Baliyar Singh 2,665 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
    Pawar Pravin 5,227 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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans