Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jun 05, 2013 @ 17:56
    Ismail Mayat
    0

    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

     

  • Tim 1193 posts 2675 karma points MVP 3x c-trib
    Jun 05, 2013 @ 17:59
    Tim
    0

    Is it GetRecordField that's null, or the .Values that's null?

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jun 05, 2013 @ 18:02
    Ismail Mayat
    0

    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

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jun 05, 2013 @ 18:09
    Ismail Mayat
    0

    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();

     

  • Tim 1193 posts 2675 karma points MVP 3x c-trib
    Jun 05, 2013 @ 18:10
    Tim
    0

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

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jun 05, 2013 @ 18:11
    Ismail Mayat
    0

    Passed in guid still get null grrrr

  • Tim 1193 posts 2675 karma points MVP 3x c-trib
    Jun 05, 2013 @ 18:11
    Tim
    0

    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.

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jun 05, 2013 @ 18:15
    Ismail Mayat
    0

    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

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jun 05, 2013 @ 22:15
    Ismail Mayat
    0

    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

  • Tim 1193 posts 2675 karma points MVP 3x c-trib
    Jun 06, 2013 @ 10:11
    Tim
    1

    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

    Jun 06, 2013 @ 11:22

    Yup I can confirm that it needs the record id

    Here is a snippet that might help

     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);
    
                                }
  • wolulcmit 357 posts 693 karma points
    Sep 26, 2013 @ 09:20
    wolulcmit
    0

    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

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Sep 26, 2013 @ 10:38
    Simon Dingley
    2
    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

  • wolulcmit 357 posts 693 karma points
    Sep 26, 2013 @ 10:43
    wolulcmit
    0

    Whoop! there it is... thanks a bunch Simon.

Please Sign in or register to post replies

Write your reply to:

Draft