Using the UpsertObject Web Method
5 min
this web method tries to find an existing business object using search fields if the business object is found, it updates the existing business object similar to how the updateobject web method works if it does not find the business object, it creates a new business object similar to how the createobject web method works request syntax frsheatintegrationupsertboresponse upsertobject(string sessionkey, string tenantid, objectcommanddata commanddata, string\[] searchfields) parameters sessionkey the session key from the connect web method tenantid the tenant for which the session key is authenticated commanddata a structure containing information about the upsert request searchfields a list of field names whose values are used to uniquely identify the object return value frsheatintegrationupsertboresponse object, defined as follows public class frsheatintegrationupsertboresponse { public string status { get; set; } public string exceptionreason { get; set; } public string recid { get; set; } public webservicebusinessobject obj { get; set; } } the frsheatintegrationupsertboresponse class has the following fields status provides a status value indicating whether the operation was successful a full description of the available status values is provided in the table below exceptionreason contains exception information, if the application throws an exception when running the connect web method recid the recid of the updated or newly created record, if the value of the status field is success obj contains a list of the results found, if the value of the status field is success the following table lists the available status values and describes how to interpret them \<font color="#ffffff">status\</font>\<font color="#ffffff">explanation\</font> success successfully updated or created the business object access the business object list from the obj field, which returns the results as a list of webservicebusinessobjects error cannot update or create the business object the recid and obj fields are null inspect the corresponding exceptionreason field to determine why the web method failed one typical error is table not found , which occurs when the specified business object does not exist in the tenant ensure that the name of the business object is spelled properly another common error is that the specified field does not exist for the business object the error message is objecttablemap field \<fieldname> is not found in table \<business object># ensure that the field name is spelled correctly, and that it is defined for the specified business object another common error is that the value for a specified field does not exist in the associated validation list the error message is \<businessobject> \<field> `\<fieldvalue>` is not in the validation list example the following example creates a new employee record, and links the user to the respective roles and teams if the employee already exists (identified using the login id), the application updates the existing information notice that the password value is specified in plain text the application automatically converts it to the internal hashed value, when saving the record objectcommanddata data = new objectcommanddata(); data objecttype = "profile#employee"; list\<objectcommanddatafieldvalue> datafields = new list\<objectcommanddatafieldvalue>(); dictionary\<string, object> fields = new dictionary\<string, object>(); fields\["status"] = "active"; fields\["firstname"] = "brian"; fields\["lastname"] = "wilson"; fields\["loginid"] = "bwilson"; fields\["isinternalauth"] = true; // notice when setting the password for the employee, that the plain text // password is specified here it will be converted to the hashed value // upon save of the record fields\["internalauthpasswd"] = "manage1t"; fields\["primaryemail"] = " bwilson\@example com "; fields\["phone1"] = "14158665309"; // recid for the "admin" user, to serve as the manager for the new employee fields\["managerlink"] = "fb884d18f7b746a0992880f2dffe749c"; // recid for the "gmi" org unit, for the orgunit of the new employee fields\["orgunitlink"] = "4a05123d660f408997a4fee714dad111"; fields\["team"] = "it"; fields\["department"] = "operations"; fields\["title"] = "administrator"; foreach (string key in fields keys) { datafields add(new objectcommanddatafieldvalue() { name = key, value = fields\[key] tostring() }); } data fields = datafields toarray(); data linktoexistent = new linkentry\[] { // first we link the new employee to the "selfservice" and // "servicedeskanalyst" roles by recid // the internal reference name for the relationship between // profile employee and frs def role is empty, so we leave // the relation attribute in the linkentry empty in this case // link to "selfservice" role new linkentry() { action = "link", relation = "", relatedobjecttype = "frs def role#", relatedobjectid = "0a4724d8478b451abea3fb44d33db1b6" }, // link to "servicedeskanalyst" role new linkentry() { action = "link", relation = "", relatedobjecttype = "frs def role#", relatedobjectid = "06d780f5d7d34119be0d1bc8fc997947" }, // we then link the new employee to the "it" and "hr" teams // the internal reference name for the relationship between // profile employee and standarduserteam is "rev2", so we // specify this in the relation attribute in the linkentry // link to the "it" team new linkentry() { action = "link", relation = "rev2", relatedobjecttype = "standarduserteam#", relatedobjectid = "10f60157a4f34a4f9ddb140e2328c7a6" }, // link to the "hr" team new linkentry() { action = "link", relation = "rev2", relatedobjecttype = "standarduserteam#", relatedobjectid = "1ff47b9eda3049cc92458ce3249ba349" } }; frsheatintegrationcreateboresponse result = frsvc upsertobject(authsessionkey, tenantid, data, new string \["loginid" ]); if (result status == "success") { console writeline("a new employee record is createdor updated with recid of ", result recid); }
