Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Object reference not set to an instance of an object

(0) ShareShare
ReportReport
Posted on by 111

4572.1.JPG

I have written custom workflow for unsharing the records. workflow triggers on change of some option set option. but I'm getting the abouv error.

below is my code:

Entity _entity = (Entity)context.InputParameters["Target"];

                //Share Opportunity

                if (_entity.LogicalName == "opportunity")

                {

                    EntityReference createdBy = _entity.GetAttributeValue<EntityReference>("createdby");

                    int oppbusiness = ((OptionSetValue)(_entity.Attributes["jd_businessunit"])).Value;

                    //get the all the teams based on User

                    string fetchxml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +

                                      "<entity name='team'>" +

                                        "<attribute name='name' />" +

                                        "<attribute name='businessunitid' />" +

                                        "<attribute name='teamid' />" +

                                        "<attribute name='teamtype' />" +

                                        "<order attribute='name' descending='false' />" +

                                        "<filter type='and'>" +

                                          "<condition attribute='teamid' operator='not-null' />" +

                                        "</filter>" +

                                        "<link-entity name='teammembership' from='teamid' to='teamid' visible='false' intersect='true'>" +

                                          "<link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='ae'>" +

                                            "<filter type='and'>" +

                                              "<condition attribute='systemuserid' operator='eq' uiname='Beau Garrett' uitype='systemuser' value='" + createdBy.Id + "' />" +

                                            "</filter>" +

                                          "</link-entity>" +

                                        "</link-entity>" +

                                      "</entity>" +

                                    "</fetch>";

                    EntityCollection teams = service.RetrieveMultiple(new FetchExpression(fetchxml));

                    if (teams.Entities.Count > 0)

                    {

                        foreach (Entity team in teams.Entities)

                        {

                            QueryExpression query = new QueryExpression("jd_securityconfig");

                            query.Criteria.AddCondition("jd_team", ConditionOperator.Equal, team.Id);

                            query.Criteria.AddCondition("jd_opportunity", ConditionOperator.Equal, true);

                            query.Criteria.AddCondition("jd_account", ConditionOperator.Null);

                            query.ColumnSet = new ColumnSet(true);

                            EntityCollection _secConfig = service.RetrieveMultiple(query);

                            foreach (Entity secConfig in _secConfig.Entities)

                            {  

                                if (secConfig.Attributes.Contains("jd_teams"))

                                {

                                    EntityReference shareWith = secConfig.GetAttributeValue<EntityReference>("jd_teams");

                                    if (secConfig.Attributes.Contains("jd_businessunit"))

                                    {

                                        int Sec_business = ((OptionSetValue)(secConfig.Attributes["jd_businessunit"])).Value;

                                        if (Sec_business != oppbusiness)

                                        {

                                            shareRecord(shareWith, _entity, service);

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

                }

            catch(Exception ex)

            {

                throw new InvalidPluginExecutionException(" " + ex.Message);

            }

        

        }

        public void shareRecord(EntityReference shareWith, Entity entity, IOrganizationService service)

        {

           

                    RevokeAccessRequest revokeRequest = new RevokeAccessRequest()

                    {

                        //ex: new EntityReference("salesorder", new Guid(orderId)),

                        Target = new EntityReference(entity.LogicalName, entity.Id),

                        //ex: new EntityReference("systemuser", new Guid(userId)),

                        Revokee = new EntityReference(shareWith.LogicalName, shareWith.Id),

                    };

                    RevokeAccessResponse revoked = (RevokeAccessResponse)service.Execute(revokeRequest);

                 }

}

*This post is locked for comments

  • Amos Dave Profile Picture
    111 on at
    RE: Object reference not set to an instance of an object

    Its working now.

    Thank you Neil and Andrew,

  • Amos Dave Profile Picture
    111 on at
    RE: Object reference not set to an instance of an object

    Hi Neil,

    As u said I'm getting null value for createdby field. How do i get that value?

    is there any alternative?

  • Verified answer
    Nflannery Profile Picture
    360 on at
    RE: Object reference not set to an instance of an object

    One of your fields will be null you are trying to assign in your request.  Easiest way is to debug it in the Plugin Registration tool that way you'll see which one is causing the error and you will be able to work it out.

    Please see below link

    ssharmacrm.wordpress.com/.../how-to-debug-a-custom-workflow-activity-in-dynamics-crm

  • Amos Dave Profile Picture
    111 on at
    RE: Object reference not set to an instance of an object

    Andrew,

    I've used the below,

                   IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

                   IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

                   IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Object reference not set to an instance of an object

    First step for you is to check following - docs.microsoft.com/.../use-iorganization-web-service-custom-workflow-activity

  • Amos Dave Profile Picture
    111 on at
    RE: Object reference not set to an instance of an object

    Hi Andrew ..

          Yes it is a custom workflow.. This is my first custom workflow. I din understand what you said. can you help me resolve this.

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Object reference not set to an instance of an object

    Hello,

    Is it custom workflow activity or plugin? If it is custom workflow activity then your very first line generates exception.

    Use IOrganizationService instance and Retrieve method to retrieve your record.

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 > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans