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

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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.

  • Amos Dave Profile Picture
    111 on at

    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.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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

    Andrew,

    I've used the below,

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

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

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

  • Verified answer
    Nflannery Profile Picture
    360 on at

    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

    Hi Neil,

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

    is there any alternative?

  • Amos Dave Profile Picture
    111 on at

    Its working now.

    Thank you Neil and Andrew,

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

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans