Advertisement
RyanFarley

Untitled

Mar 21st, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. define([
  2.     'dojo/_base/declare',
  3.     'dojo/_base/lang',
  4.     'dijit/Dialog',
  5.     'dojo/aspect',
  6.     'Sage/UI/SDataLookup'
  7. ],
  8. function (
  9.     declare,
  10.     lang,
  11.     dialog,
  12.     aspect,
  13.     SDataLookup
  14. ) {
  15.  
  16.     // define new column type for grid
  17.     var contactColumn = declare('FX.ContactColumn', null, {
  18.         icon: '',
  19.         defaultValue: '',
  20.         constructor: function (args) {
  21.             lang.mixin(this, args);
  22.         },
  23.         format: function (val, entity) {
  24.             // column will call show in the object defined below
  25.             var exec = "javascript:FX.ViewContacts.show('"  + val + "', '" + entity.$descriptor + "')";
  26.             return '<a href="' + exec + '">Contacts</a>';
  27.         }
  28.     });
  29.  
  30.     var __viewContacts = {
  31.         show: function(accountId, accountName) {
  32.             // define dialog
  33.             var dlg = new dialog({
  34.                 title: 'Contacts for ' + (accountName || 'Account'),
  35.                 style: 'width: 400px;',
  36.                 content: '<i>Loading contacts...</i>'
  37.             });
  38.            
  39.             // use SData to retrieve contacts for account
  40.             var service = Sage.Data.SDataServiceRegistry.getSDataService('dynamic');
  41.             var request = new Sage.SData.Client.SDataResourceCollectionRequest(service)
  42.                 .setResourceKind('contacts')
  43.                 .setQueryArg('where', 'Account.Id eq "' + accountId + '"')
  44.                 .setQueryArg('select', 'LastName,FirstName,Title');
  45.              
  46.             request.read({
  47.                 success: function (results) {
  48.                     // create content for dialog
  49.                     var content = '';
  50.                     results.$resources.forEach(function(contact) {
  51.                         content += '<li>' + contact.FirstName + ' ' + contact.LastName;
  52.                         if (contact.Title) content += ' - ' + contact.Title;
  53.                         content += ' <a href="Contact.aspx&entityId=' + contact.$key + '" tabindex="-1" title="View Contact"><i class="fa fa-external-link"></i></a></li>';
  54.                     });
  55.                     content = (content == '' ? '<i>Account does not have any contacts</i>' : '<ul>' + content + '</ul>');
  56.                    
  57.                     // set dialog content & show
  58.                     dlg.set('content', content);
  59.                     dlg.show();
  60.                 },
  61.                 failure: function (result) {
  62.                     Sage.UI.Dialogs.showError(result);
  63.                 }
  64.             });
  65.         },
  66.        
  67.         // add new column to lookups that are for accounts
  68.         initLookupColumns: function() {
  69.             aspect.before(SDataLookup.prototype, 'initGrid', function() {
  70.                 var col = {
  71.                     name: 'FXViewContacts',
  72.                     field: '$key',
  73.                     label: '',
  74.                     type: contactColumn,
  75.                     sortable: false,
  76.                     width: 5
  77.                 };
  78.                 if (this.sdataStore.resourceKind === 'accounts' && this.structure.filter(function(c) { return c.name === 'FXViewContacts' }).length === 0) {
  79.                     this.structure.push(col);
  80.                 }
  81.             });
  82.         }
  83.     };
  84.    
  85.     __viewContacts.initLookupColumns();
  86.    
  87.     // add to window namespace so can be invoked from hyperlink
  88.     window.FX = window.FX || {};
  89.     window.FX.ViewContacts = __viewContacts;
  90. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement