acumatica - Updating default contact on customer adds new record -


i have method either adds or updates customer using customermaint graph.

my methods works apart 1 strange thing happening.

after customer inserted. if go through method update contact second contact record created on contacts table. if update again behaves correctly , no new contact record created , default contact record updated.

here method

    private px.objects.ar.customer updatecontact(contactread rexcontact, px.objects.ar.customer m, bool insert = true)     {          px.objects.cr.contact defcontact = null;          px.objects.ar.customermaint graph = pxgraph.createinstance<px.objects.ar.customermaint>();          graph.clear(pxclearoption.clearall);          //add customer , baccount records         m.acctcd = "v" + rexcontact._id;         m.acctname = rexcontact.system_search_key;         m.type = "cu";          if (insert) {             m = graph.currentcustomer.insert(m);             defcontact = graph.defcontact.current;         }           else {             defcontact = pxselect<px.objects.cr.contact, where<px.objects.cr.contact.contactid, equal<required<px.objects.cr.contact.contactid>>>>.select(this, m.defcontactid);         }          //update default contact record         defcontact.contacttype = "ap";         defcontact.fullname = rexcontact.system_search_key;          if (rexcontact._related.contact_emails != null)         {             if (rexcontact._related.contact_emails.length > 0) defcontact.email = rexcontact._related.contact_emails[0].email_address;         }          if (rexcontact._related.contact_phones != null)         {             if (rexcontact._related.contact_phones.length > 0) defcontact.phone1 = rexcontact._related.contact_phones[0].phone_number;         }          graph.defcontact.update(defcontact);          //change customer class vendor         m.customerclassid = "vendor";         graph.currentcustomer.update(m);          graph.actions.presssave();          return m;      } 

it looks inserting customer on view different primary view. inserting in graph.currentcustomer not set current record, may result in unpredictable behaviour when try access graph.defcontact.current. should instead insert contact graph.baccount, primary view of customer maintenance graph.

also, when you're updating existing customer's contact (instead of inserting new customer), should still set graph.baccount.current value customer retrieved using locate or pxselect, , retrieve default contact using graph.defcontact.select() instead of doing full pxselect.