Asked by:
Error message when creating a new entity via batch job code - CRM 2011

General discussion
-
I'm trying to create a new Phone Call entity via batch job code for CRM 2011 (on premise). I'm using the generated Library class file that is produced by using CRMSvcUtil. I have the .cs file included in a library, then have a partial class per entity as needed. I have a Phone Call class, and in that, there's a Create. When I call it, I'm getting an unusualy error message back - "Opportunity". There's more to it, but thas was the main message. Here's the code that creates the Phone Call (I'm including the complete partial Phone Call class). This is in a Library solution. That solution is then added as a reference to each project in my batch job solution.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk; namespace MyCompany.CRM.Library { public partial class PhoneCall : Microsoft.Xrm.Sdk.Entity //Microsoft.Xrm.Client.CrmEntity { public enum PhoneCallOutcome { Expired = 100000019 } public static ColumnSet RetrieveColumns() { ColumnSet cols = new ColumnSet(); cols.Columns.AddRange(new string[] { "activityid", "from", "to", "subject", "regardingobjectid", "scheduledend" }); return cols; } /// <summary> /// Load the Phone Call, return true if the load is successful, false otherwise /// </summary> /// <param name="id"></param> /// <returns></returns> public static PhoneCall Load(string id) { PhoneCall result = (PhoneCall)CRMConnection.Service.Retrieve("phonecall", new Guid(id), RetrieveColumns()); return result; } /// <summary> /// Updates an instance of an Phone Call /// </summary> /// <returns></returns> public bool Update() { bool updateStatus = false; try { CRMConnection.Service.Update(this); updateStatus = true; } catch (Exception ex) { updateStatus = false; throw (ex);//Raise error up one level. } finally { } return updateStatus; } /// <summary> /// Create a new instance of an Phone Call /// </summary> /// <returns></returns> public string Create() { try { this.Id = CRMConnection.Service.Create(this); return this.Id.ToString(); } catch (Exception ex) { throw (ex); } finally { } return null; } } }
Here's the code that calls the Create function from the batch job project:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using VulcanSolutions.Acorn.CRM.Library; namespace CreateHealthNewBusinessPhoneCalls { public class CreateHealthNewBusinessPhoneCalls { public static void Main(string[] args) { try { Opportunities oppsWithRenewalDateInRange = new Opportunities(); oppsWithRenewalDateInRange.LoadAllOpportunitiesByRenewalDateTypeAndCategory(2, (int)Opportunity.OpportunityType.Health, (int)Opportunity.Category.New); foreach (Opportunity opp in oppsWithRenewalDateInRange) { // Get contact phone number details Contact currentContact = Contact.Load(opp.CustomerId.Id.ToString()); if (currentContact.new_DoNotContact == false) { ActivityParty from = new ActivityParty(); from.ActivityPartyId = opp.new_PreferredAgent.Id; ActivityParty to = new ActivityParty(); to.ActivityPartyId = opp.CustomerId.Id; PhoneCall newBusinessPhoneCall = new PhoneCall(); newBusinessPhoneCall.RegardingObjectId = new EntityReference("Opportunity", opp.OpportunityId.Value); newBusinessPhoneCall.ScheduledStart = DateTime.Now; newBusinessPhoneCall.new_Direction = "Outgoing"; newBusinessPhoneCall.new_PreferredAgent = opp.new_PreferredAgent; newBusinessPhoneCall.Subject = opp.Name + " Phone Call"; newBusinessPhoneCall.From = new ActivityParty[] { from }; newBusinessPhoneCall.To = new ActivityParty[] { to }; newBusinessPhoneCall.PhoneNumber = currentContact.Telephone2; newBusinessPhoneCall.Create(); // Set Phone Call Created via Batch job to true. opp.new_PhoneCallcreatedviaBatchJob = true; opp.Update(); } } } catch (Exception ex) { } finally { } } } }
Stack Trace:
"\r\nServer stack trace: \r\n at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)\r\n at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)\r\n at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)\r\n at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)\r\n\r\nException rethrown at [0]: \r\n at MyCompany.CRM.Library.PhoneCall.Create()\r\n at CreateHealthNewBusinessPhoneCalls.CreateHealthNewBusinessPhoneCalls.Main(String[] args) in C:\\TFS\\ALife\\ALife\\Development\\BatchJobs\\PhoneCallCreation_Expiration\\CreateHealthNewBusinessPhoneCalls\\CreateHealthNewBusinessPhoneCalls.cs:line 47"
Message field just has "Opportunity" in it.
Thanks.
- Edited by crmNewbie1978 Tuesday, September 23, 2014 2:58 PM
Tuesday, September 23, 2014 2:56 PM
All replies
-
similar to your new phone call object, create a new opportunity object and add the opportunityid and opp.new_PhoneCallcreatedviaBatchJob field and call the update.
assuming that you want to update the new_Ph oneCallcreatedviaBatchJob field on opportunity after phone call is updated.
regards
Jithesh
Tuesday, September 23, 2014 9:05 PM -
Hi,
Thanks for that, I made those code changes, but I'm actually getting the error when trying to create the new Phone Call entity, ie. at these lines:
ActivityParty from = new ActivityParty(); from.ActivityPartyId = opp.new_PreferredAgent.Id; ActivityParty to = new ActivityParty(); to.ActivityPartyId = opp.CustomerId.Id; PhoneCall newBusinessPhoneCall = new PhoneCall(); newBusinessPhoneCall.RegardingObjectId = new EntityReference("Opportunity", opp.OpportunityId.Value); newBusinessPhoneCall.ScheduledStart = DateTime.Now; newBusinessPhoneCall.new_Direction = "Outgoing"; newBusinessPhoneCall.new_PreferredAgent = opp.new_PreferredAgent; newBusinessPhoneCall.Subject = opp.Name + " Phone Call"; newBusinessPhoneCall.From = new ActivityParty[] { from }; newBusinessPhoneCall.To = new ActivityParty[] { to }; newBusinessPhoneCall.PhoneNumber = currentContact.Telephone2; newBusinessPhoneCall.Create();
Wednesday, September 24, 2014 7:56 AM -
I just realised what the error was - I have an upper case 'O' at the start of 'Opportunity' when setting it up as an entity reference when creating the phone call! I changed it to a lower case 'o' and that got rid of that error.
newBusinessPhoneCall.RegardingObjectId = new EntityReference("opportunity", opp.OpportunityId.Value);
Wednesday, September 24, 2014 8:03 AM