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)

Exception Shooting, Not found

(0) ShareShare
ReportReport
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

I have the same question (0)
  • David Jennaway Profile Picture
    14,065 on at

    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

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