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)
Answered

Value cannot be null. Parameter name: Value when creating a new Email activity.

(0) ShareShare
ReportReport
Posted on by

I'm trying to write an outlook plugin that will create a lead and then attach the cooresponding email as an activity to the new lead. So far, I'm creating the lead successfully but when I try to create the email, I get a ArgumentNullException no matter what I do. The code I'm using is:

                var systemUserRequest = new WhoAmIRequest();
                var systemUserResponse = (WhoAmIResponse)_serviceProxy.Execute(systemUserRequest);
                var userId = systemUserResponse.UserId;

                var to = new ActivityParty {PartyId = new EntityReference(SystemUser.EntityLogicalName, userId)};
                var from = new ActivityParty { PartyId = new EntityReference { LogicalName = "lead", Id = entityId } };

                var emailCreate = new Email();                
                emailCreate.To= new[] {to};
                emailCreate.From = new[] {from};
                emailCreate.Subject = emailItem.Subject;
                emailCreate.Description = emailItem.HTMLBody;
                emailCreate.RegardingObjectId = new EntityReference("lead", entityId);
                emailCreate.DirectionCode = true;
                _serviceProxy.Create(emailCreate);


I've already called the EnableProxyTypes() function on the OrganizationServiceProxy object and as I said, there's no errors creating the lead. I just don't know what value(s) are null and aren't supposed to be. Looking at the sdk samples, all of the ones that create an email activity has the same properties set.

Any ideas? Or is there any way I can  somehow track down which null value it's complaining about?

Thanks,

Nick 

*This post is locked for comments

I have the same question (0)
  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Nick,

    If you don't use Outlook, is that okay?

    And your user has privilege to read the Lead, System User, and create Email.

    Another reason, I think the 'From' field in the Email in CRM is not possible set to Lead, it only supports for Queue and User, see this if you create manually using UI:

    So your code in this line:

     var from = new ActivityParty { PartyId = new EntityReference { LogicalName = "lead", Id = entityId } };

    I think will not work for 'From' field.

    I also tried using Workflow, the Lead is not available in the 'From' field.

    Especially for Outgoing EMail

    emailCreate.DirectionCode = true;

    But, you can still using Lead as Regarding.

    Maybe you can try first to change the 'From' field not from the Lead, but instead, change From field set to User or Queue, to prove it, it is just assumption now.

    Or

    Maybe you can also try to change the direction to false as incoming email if you mean that it is from the Lead.

    emailCreate.DirectionCode = false;

    Hope this helps!

    Thanks.

  • Waqar Sohail Profile Picture
    on at

    Hi Nick, You need to set the User in the From attribute. You can't add the Lead in From but in to, bcc,cc and regarding. Also i don't think you need to set the direction. You can remove the direction line. As it is auto populated Flag.

  • Community Member Profile Picture
    on at

    Thanks for the suggestions. I've tried it several different ways repeatedly but I continue to get the same result. I've modified my code to try adding myself in the From field, and I've tried adding just an email address in the To field, as well as the Lead entity guid, as well as nothing, with and without the DirectionCode parameter, with and without the RegardingObjectId parameter, I've even tried replacing the Body with a fixed-text test value to eliminate the possibility that there's something in HTMLBody causing it but still getting the Null error. Both entity guids (userId and entityId) are also valid values.

                    var systemUserRequest = new WhoAmIRequest();
                    var systemUserResponse = (WhoAmIResponse)_serviceProxy.Execute(systemUserRequest);
                    var userId = systemUserResponse.UserId;
    
                    var from = new ActivityParty {PartyId = new EntityReference(SystemUser.EntityLogicalName, userId)};
                    var to = new ActivityParty { PartyId = new EntityReference { LogicalName = "lead", Id = entityId } };
    
                    var emailCreate = new Email();                
                    emailCreate.To= new[] {to};
                    emailCreate.From = new[] {from};
                    emailCreate.Subject = emailItem.Subject;
                    emailCreate.Description = emailItem.HTMLBody;
                    emailCreate.RegardingObjectId = new EntityReference("lead", entityId);
                    _serviceProxy.Create(emailCreate);

    I just wish I knew what null value it was actually complaining about.

    It works just fine if I use the built-in CRM ribbon button to "Set Regarding" or "Track" so it's not a permissions issue - plus, I'm a domain admin and a CRM admin with all roles.

  • Waqar Sohail Profile Picture
    on at

    Can you put tracer in it?  ITracingService tracer

    and get the all the objects one by one in tracers?

  • Community Member Profile Picture
    on at

    Okay. This is weird. Just for testing, I copied and pasted this code straight out of the Microsoft Playbook and I still get a Null error. For some reason, I can create a lead all day long, but not even a simple Contact?

                        _serviceProxy.EnableProxyTypes();
                        Contact emailContact = new Contact
                        {
                            FirstName = "Nancy",
                            LastName = "Anderson",
                            EMailAddress1 = "nancy@contoso.com"
                        };
                        var _contactId = _serviceProxy.Create(emailContact);
    


  • Community Member Profile Picture
    on at

    Waqar, can I use the tracing service in an Outlook Plugin? All the MS samples I see regarding it appears to be for making a CRM server plugin.

  • Verified answer
    Community Member Profile Picture
    on at

    Okay, I got it to work. Not really sure why yet, but will investigate further. 

    I have a global static object: 

    public static OrganizationServiceProxy _serviceProxy;

    In my calling routine, I set that object:

    var serverConnect = new ServerConnection();
    var config = serverConnect.GetServerConfiguration();
    _serviceProxy = ServerConnection.GetOrganizationProxy(config);

    Then I call my method:

    CreateNewLead();

    At the create part of my CreateLead() method, what I had was:

    using (_serviceProxy)
    {
        _serviceProxy.EnableProxyTypes();
        var guid = _serviceProxy.Create(lead);
        AttachEmail(oMailItem, guid);
        MessageBox.Show("Lead: '" + fName + " " + lName + "' sucessfully added to CRM.",
            "Lead Created Sucessfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    With that code, it would create the lead just fine, but it would fail creating anything else. Even changing the code to:

    using (_serviceProxy)
    {
        _serviceProxy.EnableProxyTypes();
        Contact emailContact = new Contact
        {
            FirstName = "Nancy",
            LastName = "Anderson",
            EMailAddress1 = "nancy@contoso.com"
        };
        var _contactId = _serviceProxy.Create(emailContact);    
        var guid = _serviceProxy.Create(lead);
        AttachEmail(oMailItem, guid);   
        MessageBox.Show("Lead: '" + fName + " " + lName + "' sucessfully added to CRM.",
            "Lead Created Sucessfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    With that code, it would FAIL creating the contact, SUCCESSFULLY create the lead, and then FAIL again creating the email.

    However, when I changed the code to:

    using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
    {
        _serviceProxy.EnableProxyTypes();
        Contact emailContact = new Contact
        {
            FirstName = "Nancy",
            LastName = "Anderson",
            EMailAddress1 = "nancy@contoso.com"
        };
        var _contactId = _serviceProxy.Create(emailContact);
        var guid = _serviceProxy.Create(lead);
        AttachEmail(oMailItem, guid);
        MessageBox.Show("Lead: '" + fName + " " + lName + "' sucessfully added to CRM.",
            "Lead Created Sucessfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    Everything is created just fine with no errors!

    I don't know why, but it only seems to work if I re-initialize the _serviceProxy object. And if not, I don't understand why all 3 create calls wouldn't fail. Why would it fail on the first one (contact), succeed on the second (lead) and then fail again on the third (email)?

    Strange and kinda sucks from a performance standpoint because every time I have to re-initialize that proxy, it takes time... Especially for a user connecting to the IFD over a slower connection. I haven't tried taking the using statement out of the equation yet. Perhaps eliminating that and just manually calling the Dispose() method in a Finally block would work.

    Thanks all for the suggestions!

    Nick

  • Community Member Profile Picture
    on at

    Quick update. I tried moving the _serviceProxy.EnableProxyTypes() outside of the using statement and then removed the initialization part of the using statement and that works.

    I know in theory, a variable in a using block is supposed to be read-only and I guess calling the EnableProxyTypes() inside of it changes it (even though that's the way all the MS samples are written) so I guess there's something going on underneath with the C# compiler or something.

    Still doesn't make sense as to why the lead would be created but nothing before or after it though.

    _serviceProxy.EnableProxyTypes();
    using (_serviceProxy) { Contact emailContact = new Contact { FirstName = "Nancy", LastName = "Anderson", EMailAddress1 = "nancy@contoso.com" }; var _contactId = _serviceProxy.Create(emailContact); var guid = _serviceProxy.Create(lead); AttachEmail(oMailItem, guid); MessageBox.Show("Lead: '" + fName + " " + lName + "' sucessfully added to CRM.", "Lead Created Sucessfully", MessageBoxButtons.OK, MessageBoxIcon.Information); }


  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Nick,

    So the error also happened before when creating the Lead not only email?

    Just want to ask, are you able to create email with From field set to lead, not user nor queue?

    Thanks.

  • Community Member Profile Picture
    on at

    No, the lead ALWAYS created successfully. It was always the email that failed. And then when I put in the test code for the Contact, that would fail also.

    And yes, I just tried it and it DID let me use the Lead in the From field.

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