This post is related to my previous post. So, I suggest to read that first using the following link, in case you did not read it yet.
http://ankit.inkeysolutions.com/2014/10/sdksoapjs.html
In this post I’m sharing the sample code for the most common messages of the Sdk.Sync namespace.
Examples:
· Create
function SyncCreateRecord() {
var contact = new Sdk.Entity("contact");
var userID = new Sdk.EntityReference("systemuser", "GUID"); //Use the GUID value of user record
contact.addAttribute(new Sdk.String("lastname", "Sync record Last Name"));
contact.addAttribute(new Sdk.Lookup("ownerid", userID));
var createdRecordID = Sdk.Sync.create(contact);
alert("Contact created successfully : \"" + createdRecordID + "\"");
}
· Update
function SyncUpdateRecord() {
var contact = new Sdk.Entity("contact");
contact.addAttribute(new Sdk.Guid("contactid", "GUID")); //Use the GUID value of contact record
contact.addAttribute(new Sdk.String("lastname", "Sync record Last Name Updated"));
var result = Sdk.Sync.update(contact);
if (result == true) {
alert("Contact updated successfully.");
}
else {
alert("Contact not updated.");
}
}
function SyncDeleteRecord() {
//Use the GUID value of contact record
Sdk.Sync.del("contact", "GUID");
alert("Contact deleted successfully.");
}
· Retrieve
function SyncReteriveRecord() {
var columnSet = new Sdk.ColumnSet();
columnSet.addColumn("fullname");
//Use the GUID value of contact record
var contact = Sdk.Sync.retrieve("contact", "GUID", columnSet);
alert("Retrieved contact : \"" + contact.getValue("fullname") + "\"");
}
· RetrieveMultiple
o QueryByAttribute
function SyncRetrieveMultipleByQueryByAttribute() {
var fetchQuery = new Sdk.Query.QueryByAttribute("contact");fetchQuery.setColumnSet("fullname");
fetchQuery.addAttributeValue(new Sdk.String("middlename", "middle"));var result = Sdk.Sync.retrieveMultiple(fetchQuery);
MethodToBePassedTheResults(result);}
o QueryExpression
function SyncRetrieveMultipleByQueryExpression() {
var fetchQuery = new Sdk.Query.QueryExpression("contact");fetchQuery.addColumn("fullname");
var middleName = "middle";var values = new Sdk.Query.Strings([middleName]);fetchQuery.addCondition("contact", "middlename", Sdk.Query.ConditionOperator.Equal, values);var result = Sdk.Sync.retrieveMultiple(fetchQuery);
MethodToBePassedTheResults(result);}
o FetchExpression
function SyncRetrieveMultipleByFetchXML() {
var fetchXml = ["<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\">","<entity name=\"contact\"> ",
" <attribute name=\"fullname\" /> ",
" <order attribute=\"fullname\" descending=\"false\" /> ",
" <filter type=\"and\"> ",
" <condition attribute=\"middlename\" operator=\"eq\" value=\"middle\" /> ",
" </filter> ",
" </entity> ",
" </fetch>"].join("");var fetchQuery = new Sdk.Query.FetchExpression(fetchXml);var result = Sdk.Sync.retrieveMultiple(fetchQuery);
MethodToBePassedTheResults(result);
o MethodToBePassedTheResults, a common method which is being used in above RetrieveMultiple options.
function MethodToBePassedTheResults(cb) {
if (cb.getEntities().getCount() >= 1) {
alert(cb.getEntities().getCount());}else {
alert("No result");
}}
· Associate
For this message, I’ve created custom many-to-many relationship between account and contact i.e. new_contact_account.
function SyncAssociateRecord() {
//Use the GUID value of account record
var account = new Sdk.EntityReference("account", "GUID");
var collection = new Sdk.Collection(Sdk.EntityReference, new Array(account));
//Use the GUID value of contact record
Sdk.Sync.associate("contact", "GUID", "new_contact_account", collection);
alert("Contact - Account Associated");
}
· Disassociate
For this message, I’ve created custom many-to-many relationship between account and contact i.e. new_contact_account.function SyncDisassociateRecord() {
//Use the GUID value of account record
var account = new Sdk.EntityReference("account", "GUID");
var collection = new Sdk.Collection(Sdk.EntityReference, new Array(account));
//Use the GUID value of contact record
Sdk.Sync.disassociate("contact", "GUID", "new_contact_account", collection);
alert("Contact - Account Disassociated");
}
· SetState
This message is not included in common messages file i.e. Sdk.Soap.vsdoc.js. They provided a separate file, the Sdk.SetState.vsdoc.js to incorporate this message. To use this file we need to slightly change the existing code. We need to add the following line as the first line of the Sdk.SetState.vsdoc.js file,
Sdk = window.Sdk || { __namespace: true };
And we need to include two files at entity form load event:
Sdk.Soap.vsdoc.js
Sdk.SetState.vsdoc.js
Now, we are all set to use the following method.
function jQuerySetStateActivateRecord() {
//Use the GUID value of contact record
var contactMoniker = new Sdk.EntityReference("contact", "GUID");
var setStateRequest = new Sdk.SetStateRequest(contactMoniker, 0, 1);
//To deactivate record, use same code just pass state = 1 and status = 2) at above line
Sdk.Sync.execute(setStateRequest);
alert("Contcat Activated.");
}
I hope this would be helpful. In my upcoming post, I will be covering the sample code for Sdk.JQ and Sdk.Q namespaces.
Thanks for reading this far.