2017-07-26 5 views
0

ユーザーがエントリを作成できるようにするページフラグメントがあります。彼らは送信ボタンをクリックすると、それは次のように実行します:電子メールアドレス(またはユーザー名)を通知メールに含めようとしています

newSOEmailMessage(widget); 
widget.datasource.createItem(); 
app.closeDialog(); 

これは、ウィジェットのフィールドの値が含まれ、ユーザーに電子メールを送り、クライアントスクリプトアクティブ:

function newSOEmailMessage(sendButton) { 
    var pageWidgets = sendButton.root.descendants; 
    var currentuser = Session.getActiveUser().getEmail(); 
    var htmlbody = currentuser + 'has created new system order for: <h1><span style="color:#2196F3">' + pageWidgets.ShowName.value + ' - ' + pageWidgets.UsersPosition.value + '</h1>' + 
     '<p>R2 Order #: <b>' + pageWidgets.R2OrderNumber.value + '</b>' + 
     '<p>Delivery Date: <b>' + pageWidgets.DeliveryDate.value.toDateString() + '</b>' + 
     '<p>Start of Billing: <b>' + pageWidgets.SOB.value.toDateString() + '</b>' + 
     '<p>Sales Person: <b>' + pageWidgets.SalesPerson.value + '</b>' + 
     '<p>&nbsp;</p>' + 
     '<p>Company: <b>' + pageWidgets.Company.value + '</b>' +   
     '<p>&nbsp;</p>' + 
     '<p>Notes: <b>' + pageWidgets.Notes.value + '</b>'; 

    google.script.run 
    .withSuccessHandler(function() { 
    }) 
    .withFailureHandler(function(err) { 
     console.error(JSON.stringify(err)); 
    }) 
    .sendEmailCreate(
     '[email protected]', 
     'New order for: ' + pageWidgets.ShowName.value + ' - ' + pageWidgets.UsersPosition.value, 
     htmlbody); 
} 

をこの作品のすべて"currentuser"オプション(var htmlbody =後)以外は問題ありません。

Session is not defined 
at newSOEmailMessage (Notifications_ClientScripts:7:45) 
at SystemOrders_Add.SubmitButton.onClick:1:1 

私は電子メールアドレス(または好ましくは、ユーザーの実際の名前)を等しくなるように、「CurrentUserに」を希望:コードで、私は次のエラーを取得する上で。

例:私は何をしないのです

ジョン・ドウがために...新しいシステム順序を作成しましたか」?

ありがとうございました!

注:別のモデルのコメントセクションにユーザーの名前を表示するためのディレクトリモデルのセットアップが既にあります。そのモデルは、以下を実行している(私は私のSystemOrdersモデルにそれを追加することができると仮定している?)

// onCreate 
var email = Session.getActiveUser().getEmail(); 

var directoryQuery = app.models.Directory.newQuery(); 
directoryQuery.filters.PrimaryEmail._equals = email; 
var reporter = directoryQuery.run()[0]; 
+0

それはSession.getActiveUser()getEmail()と思われます;。 Apps Script(https://developers.google.com/apps-script/reference/base/session#getActiveUser())の一部であり、クライアントスクリプトで使用することはできません。クライアントスクリプトの場合、これを使用することができます:https://developers.google.com/appmaker/scripting/api/client#User – Wilmar

答えて

3

あなたがしたい場合は、サーバとクライアントサイドAPI

// It is server side API 
var email = Session.getActiveUser().getEmail(); 

// It is client side API 
var email = app.user.email; 

を混合しているように見えますディレクトリからユーザーのフルネームを使用し、その後、あなたはアプリの起動スクリプトでは、たとえば、それを事前にロードする必要があります。

// App startup script 
// CurrentUser - assuming that it is Directory model's datasource 
// configured to load record for current user. 
loader.suspendLoad(); 
app.datasources.CurrentUser.load({ 
    success: function() { 
    loader.resumeLoad(); 
    }, 
    failure: function(error) { 
    // TODO: Handle error 
    } 
}); 

ので、後でこのデータソースの項目を参照することができますあなたのコードで:

var fullName = app.datasources.CurrentUser.item.FullName; 

また、私は、レコードが実際に作成された場合にのみメールを送信推薦する:

// Sends async request to server to create record 
widget.datasource.createItem(function() { 
    // Record was successfully created 
    newSOEmailMessage(widget); 
}); 
+1

これはすべてうまくいった!ありがとうございました!不思議な人のために、App Startup Scriptは、App Settings(ギア)の下にある「App Startup Script」ボックスに入っています。 –

関連する問題