2

I hope somone can help me further, as I'm really much stuck here. Im trying to add a contract line to my contract entity from a post-creat plugin. My code:

Guid c_Id = (Guid)entity.Attributes["contractid"];
DateTime start = (DateTime)entity["activeon"];
DateTime end = (DateTime)entity["expireson"];
Money money = new Money();
money.Value = 0;


//Set Instance for Contract Line 
Entity ContractLine = new Entity();
ContractLine.LogicalName = "contractdetail";

//Create Contract Line for the Contract
ContractLine["title"] = "PLUGIN FIRED";
ContractLine["activeon"] = start;
ContractLine["expireson"] = end;
ContractLine["totalallotments"] = 1;
//ContractLine["customerid"] = entity["customerid"];  
//ContractLine["productid"] = entity["productid"];
ContractLine["price"] = money;
ContractLine["contractid"] = c_Id;

service.Create(ContractLine);

For some reason I get the error that "Attribute: contractid cannot be set to NULL" which really stragnge because it actually does get the GUID for the contractid, as I checkd it on another field on another entity. I would really much appreciate if somone can help me out here. Thanks.

Peter Majeed
  • 5,304
  • 2
  • 32
  • 57
A Robben
  • 299
  • 2
  • 5
  • 20

2 Answers2

2

Should ContractLine["contractid"] = c_Id; be ContractLine["contractid"] = new EntityReference("contract", c_Id);?

Also should c_Id be an EntityReference instead of a GUID?

John Hoven
  • 4,085
  • 2
  • 28
  • 32
  • Thank you very much Boone, Appreciate it soo much worked for me. I had to change it to ContractLine["contractid"] = new EntityReference("contract", c_Id). – A Robben May 23 '12 at 16:26
1

Any chance you have another plugin that is firing as a result of yours that is throwing the exception. That always seems to bite me. Try disabling all other plugins except for the one you're working on...

Daryl
  • 18,592
  • 9
  • 78
  • 145
  • Hi Daryl, I'm working on a dev environment, that is not the case. But it is really strange because it actaully gets the contractId but when I set it for the ContractLine["contractid"] I get this error. – A Robben May 23 '12 at 16:05
  • 1
    So if you debug it, the exception is getting thrown on this line: ContractLine["contractid"] = c_Id; or is it getting thrown during the create? – Daryl May 23 '12 at 16:19
  • Hi Daryl, thank for your effore the answer is show below, I had to set it like this ContractLine["contractid"] = new EntityReference("contract", c_Id); Appreicate your reply again. – A Robben May 23 '12 at 16:27
  • No prob. I generated and use the early bound objects instead of the plain Entity Object, which would catch that sort of error at compile time, rather than run time. – Daryl May 23 '12 at 20:46