Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Exception Shooting, Not found

Posted on by 270
Hello Community, 
 protected void ExecutePreStakeholderUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            var context = localContext.PluginExecutionContext;
            var service = localContext.OrganizationService;

                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.
                    Entity stakeholder = (Entity)context.InputParameters["Target"];

                    var stakeholderEntity = service.Retrieve("new_stakeholder", stakeholder.Id, new ColumnSet("new_projects", "new_stakeholders"));
                    var projectId = stakeholder.Contains("new_projects") ? stakeholder.GetAttributeValue<EntityReference>("new_projects").Id : 
                        (stakeholderEntity.Contains("new_projects") ? stakeholderEntity.GetAttributeValue<EntityReference>("new_projects").Id : Guid.Empty );
                    var companyId = stakeholder.Contains("new_stakeholders") ? stakeholder.GetAttributeValue<EntityReference>("new_stakeholders").Id : 
                        (stakeholderEntity.Contains("new_stakeholders") ? stakeholderEntity.GetAttributeValue<EntityReference>("new_stakeholders").Id : Guid.Empty);

                    Entity project = service.Retrieve("opportunity", projectId, new ColumnSet("name"));
                    Entity company = service.Retrieve("account", companyId, new ColumnSet("name"));
                    var projectName = project != null && project.Contains("name") ? project.GetAttributeValue<string>("name") : string.Empty;
                    var companyName = company.Contains("name") ? company.GetAttributeValue<string>("name") : string.Empty;

                    if (!string.IsNullOrEmpty(projectName) && !string.IsNullOrEmpty(companyName))
                    {
                        var name = projectName + " - " + companyName;
                        var mobileName = stakeholder.Contains("new_name") ? stakeholder.GetAttributeValue<string>("new_name") : string.Empty;
                        if (mobileName != name)
                        {
                            stakeholder["new_name"] = name;
                        }
                    }
                }
       }   




The above code raise a NullReferenceException, I dont why or when, the error has been catched on the servers logs. Y only guess is this line of code (the company object) :
commpanyName = company.Contains("name") ? company.GetAttributeValue<string>("name") : "";

Am I missing anything else?

*This post is locked for comments

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Exception Shooting, Not found

    Either the stakeholderEntity or company instances might be null, and you're not testing for either

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans