I get error on rec.GetRecordField(Constants.Form.AnswersJsonField).Values.Clear(); null reference I guess its either the form field alias or i should be passing in form field guid?
I think you need to pass in the Guid instead, looking at some old Contour API code on one of my sites (although its a slightly different part of the API).
awesome many thanks. bit more information the user fills in form that takes them to another page. On that page is another form but this is not contour its a radio button matrix set of questions with weighting values could have done it with my own field type etc but no time so I did this instead. When they submit this i take the values convert to json string and set to hidden field called answers. I suspect that becuase on the first form the field was blank it does not end up in contour.
Right bit more progress with this. I know what the issue is. If you have a blank field then in the record values its not there. So I have work around in my hidden field I have added a default value now I can get the field and update it. However when I save I get error "Object must implement IConvertible."
My code now looks like
string formJson = GetFormJson();
using (var recordStorage = new RecordStorage())
{
var rec = recordStorage.GetRecord(new Guid(recordId));
var form = new FormStorage().GetFormFromRecordId(rec.Id);
Cool, was going to say was the value empty, had a similar issue with my code by the look of it, as I check for nulls on them first. I haven't ever run into the error you're getting now though, sorry!
var rf = record.GetRecordField(field.Id);
if (rf == null)
{
var key = Guid.NewGuid();
var recordField = new RecordField
{
Field = field,
Key = key,
Record = record.Id,
Values = currentFieldValue
};
using (var recordFieldStorage = new RecordFieldStorage())
recordFieldStorage.InsertRecordField(recordField);
record.RecordFields.Add(key, recordField);
}
else
{
rf.Values = currentFieldValue;
using (var recordFieldStorage = new RecordFieldStorage())
recordFieldStorage.UpdateRecordField(rf);
}
I've made my own Contour Workflow and need to null check some fields before executing certain code.
Any idea how to check for a null value of if you dont have the id and only the name as a string? for example record.GetRecordField("Your email") always gives me the exception {"Sequence contains no elements"} so I cant figure out how to null check the value.
Update existing record
I am using umbraco 6.1.1 and latest contour. In a razor macro i am trying to update an existing contour record my code looks like:
string formJson = GetFormJson();
using (var recordStorage = new RecordStorage())
{
var rec = recordStorage.GetRecord(new Guid(recordId));
var form = new FormStorage().GetFormFromRecordId(rec.Id);
var rs = new RecordService(rec);
rs.Open();
rec.GetRecordField(Constants.Form.AnswersJsonField).Values.Clear();
rec.GetRecordField(Constants.Form.AnswersJsonField).Values.Add(new List<string> { formJson });
recordStorage.UpdateRecord(rec, form);
recordStorage.UpdateRecordXml(rec, form);
rs.Submit();
rs.Dispose();
}
I get error on rec.GetRecordField(Constants.Form.AnswersJsonField).Values.Clear(); null reference I guess its either the form field alias or i should be passing in form field guid?
Regards
Ismial
Is it GetRecordField that's null, or the .Values that's null?
Seems to be rec.GetRecordField(Constants.Form.AnswersJsonField)
the value i am passing is the name of the field setup in contour its called Answers i just tried lower casing it no joy.
Regards
Ismail
Hmm just updated code to
using (var recordStorage = new RecordStorage())
{
var rec = recordStorage.GetRecord(new Guid(recordId));
var form = new FormStorage().GetFormFromRecordId(rec.Id);
Guid answerFieldGuid = (from f in form.AllFields where f.Caption == Constants.Form.AnswersJsonField select f.FieldTypeId).FirstOrDefault();
var rs = new RecordService(rec);
rs.Open();
rec.GetRecordField(answerFieldGuid).Values.Clear();
rec.GetRecordField(answerFieldGuid).Values.Add(new List<string> { formJson });
recordStorage.UpdateRecord(rec, form);
recordStorage.UpdateRecordXml(rec, form);
rs.Submit();
rs.Dispose();
}
still no joy get Object reference not set to an instance of an object on line rec.GetRecordField(answerFieldGuid).Values.Clear();
I think you need to pass in the Guid instead, looking at some old Contour API code on one of my sites (although its a slightly different part of the API).
Passed in guid still get null grrrr
Odd. I'll have a dig through oneof my old sites when I get home tonight, I might have some example code somewhere I think.
awesome many thanks. bit more information the user fills in form that takes them to another page. On that page is another form but this is not contour its a radio button matrix set of questions with weighting values could have done it with my own field type etc but no time so I did this instead. When they submit this i take the values convert to json string and set to hidden field called answers. I suspect that becuase on the first form the field was blank it does not end up in contour.
Regards
Ismail
Right bit more progress with this. I know what the issue is. If you have a blank field then in the record values its not there. So I have work around in my hidden field I have added a default value now I can get the field and update it. However when I save I get error "Object must implement IConvertible."
My code now looks like
string formJson = GetFormJson();
using (var recordStorage = new RecordStorage())
{
var rec = recordStorage.GetRecord(new Guid(recordId));
var form = new FormStorage().GetFormFromRecordId(rec.Id);
var rs = new RecordService(rec);
rec.GetRecordField(Constants.Form.AnswersJsonField).Values.Clear();
rec.GetRecordField(Constants.Form.AnswersJsonField).Values.Add(new List
recordStorage.UpdateRecord(rec, form);
recordStorage.UpdateRecordXml(rec, form);
rs.Submit();
rs.Dispose();
}
Full stack trace is
System.InvalidCastException was caught
HResult=-2147467262
Message=Object must implement IConvertible.
Source=mscorlib
StackTrace:
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at Umbraco.Forms.Data.Storage.RecordFieldValueStorage.InsertRecordFieldValues(RecordField rf)
at Umbraco.Forms.Data.Storage.RecordFieldStorage.UpdateRecordField(RecordField recordField)
at Umbraco.Forms.Data.Storage.RecordStorage.UpdateRecord(Record record, Form form)
Any ideas?
Regards
Ismail
Cool, was going to say was the value empty, had a similar issue with my code by the look of it, as I check for nulls on them first. I haven't ever run into the error you're getting now though, sorry!
Comment author was deleted
Yup I can confirm that it needs the record id
Here is a snippet that might help
This may be a bit off topic but I'm desperate.
I've made my own Contour Workflow and need to null check some fields before executing certain code.
Any idea how to check for a null value of if you dont have the id and only the name as a string?
for example record.GetRecordField("Your email") always gives me the exception {"Sequence contains no elements"} so I cant figure out how to null check the value.
- Tim
record.RecordFields.Any(x=> x.Value.Field.Caption == "Your email")
Taken from the following post:
http://our.umbraco.org/forum/umbraco-pro/contour/36380-Custom-workflow-test-if-RecordField-exists
HTH, Si
Whoop! there it is... thanks a bunch Simon.
is working on a reply...