user2421945
user2421945

Reputation: 11

trying to send email request but i get exception

I am trying to create and send an email request in crm2011. I have the email, but when I try to send it I get an exception:

email With Id = 00000000-0000-0000-0000-000000000000 Does Not Exist.

Here's my code:

OrganizationServiceProxy p = new OrganizationServiceProxy(
    new Uri(""), null, ccr, null);

WhoAmIRequest systemUserRequest = new WhoAmIRequest();
WhoAmIResponse systemUserResponse = (WhoAmIResponse)p.Execute(systemUserRequest);
Guid _userId = systemUserResponse.UserId;

Entity email = new Entity("email");
email.Attributes.Add("subject", "test");

Entity[] To = new Entity[1];
To[0] = new Entity("activityparty");
To[0]["partyid"] = new EntityReference("contact", new Guid("some guidid"));
email.Attributes.Add("to", To);

Entity[] From = new Entity[1];
From[0] = new Entity("activityparty");
From[0]["partyid"] = new EntityReference("systemuser", _userId);
email.Attributes.Add("from", From);

try
{
    Guid emailGuid = p.Create(email);         
}
catch (Exception e)
{
    Console.WriteLine("error " + e.Message);
    Console.ReadLine();
}

OrganizationRequest request = new OrganizationRequest() { RequestName = "SendEmail" };

request["EmailId"] = email.Id;
request["TrackingToken"] = "";
request["IssueSend"] = true;

// THE CODE FAILS HERE:
OrganizationResponse rsp = p.Execute(request);

Upvotes: 0

Views: 865

Answers (1)

Guido Preite
Guido Preite

Reputation: 15128

The main error is in this line:

request["EmailId"] = email.Id;

when you create the email, the Id property is not filled inside the record but the Guid is inside the variable emailGuid

I suggest to change the code in this way :

try
{
    Guid emailGuid = p.Create(email);        
    OrganizationRequest request = new OrganizationRequest() { RequestName = "SendEmail" };
    request["EmailId"] = emailGuid; // now is the right variable
    request["TrackingToken"] = "";
    request["IssueSend"] = true;
    OrganizationResponse rsp = p.Execute(request);
}
catch (Exception e)
{
    Console.WriteLine("error " + e.Message);
    Console.ReadLine();
}

Upvotes: 1

Related Questions