0
votes

I have a button in a google-app-maker page, it's function is to sending Invoice by email. So once this button clicked, it will do two function. 1. Sending invoice by email. 2. Change status of EmailStatus to 'YES'.

/* var widgets = widget.parent.descendants; */
var to = "[email protected]";
var subject = "Prepare Invoice : " + widget.datasource.item.Client_Name;
var msg = "Please Prepare Invoice for " + "\n\nClient Name : " + 
widget.datasource.item.Client_Name + "\n\nService : " + 
widget.datasource.item.Service + "\n\nCase : " + 
widget.datasource.item.Subjects + "\n\nScope :" + 
widget.datasource.item.Scope + "\n\nSubject : " + 
widget.datasource.item.Subjects + "\n\nStart :" + 
widget.datasource.item.Start + "\n\nInterim : " + 
widget.datasource.item.Interim + "\n\nStatus :" + 
widget.datasource.item.Statusx + "\n\nCA : " + 
widget.datasource.item.Client_Ref + "\n\nBilling : " + 
widget.datasource.item.Billing + "\n\nFee VS : " + 
widget.datasource.item.Fee_VS + "\n\nFee VI" + 
widget.datasource.item.Fee_VI + "\n\nNotes : " + 
widget.datasource.item.Notes + "\n\nPrep Invoice : " + 
widget.datasource.item.Prep_Invoice + "\n\nInvoiced : " + 
widget.datasource.item.Prep_Invoice + "\n\nInvoice Number : " + 
widget.datasource.item.Invoice_Number;

SendEmail(to, subject, msg);

widget.datasource.modes.create.item.EmailStatus = 'YES';

There's no problem with sending email, but for EmailStatus change to field have problem. It can not change value from NULL to 'YES'. Do you have any idea how to solved it ?

Thanks

2
Is your EmailStatus item an existing item? Your last line of code is in create mode, so you are suggesting that you are creating a new item vs editing an existing item. Also, even though you are in create mode you never actually create the item unless you are not showing us all your code. Furthermore, your SendEmail function will execute and your last line of code will execute simultaneously, since the actual email sending is a server function you should implement the status update in the call back of your send email function instead. - Markus Malessa

2 Answers

0
votes

Here is a scenario how to handle this assuming that your EmailStatus is just field in the same model/datasource as where you have all of your invoice information.

Delete this line of code:

widget.datasource.modes.create.item.EmailStatus = 'YES';

Change this line of code:

SendEmail(to, subject, message);

to:

SendEmail(to, subject, message, widget.datasource.item);

Change your client send email function to this:

function SendEmail(to, subject, message, item) {
  google.script.run
    .withSuccessHandler(function() {
      item.EmailStatus = 'YES';
    })
    .withFailureHandler(function(error) {
      //include a failure message popup or something here
    })
    .ServerSendEmailFunction(to, subject, message);
}

Hope this helps and solves your problem.

0
votes

Is "EmailStatus" a field in the datasource of the widget, or is it a field in a different datasource? If it is actually a field in a different datasource/model, then you have to specify which record in that other datasource you want to connect to (in this case, the record called "Yes").

I'd recommend creating another server script function to set the status to "Yes" - here's the instructions for doing that: https://developers.google.com/appmaker/models/relations#server_script

Then call this new function in your success handler for the SendEmail function.