Using a CRM Client Script Button to create a Books Invoice

Using a CRM Client Script Button to create a Books Invoice

Hello,
I need help handling error messages returned to my client script from a function.

The scenario
I have setup a client script button which is available from each Deal.
This CS executes a crm function, which in turn creates an invoice based on the information in the Deal.
The client script checks the deal is at the correct stage and displays the amount to be invoiced, before proceeding to execute the function.

The problem
It all works perfectly well except for the error handling. I'm only testing for a 'response' from the function ( in the Client Script) before showing a success or error message, so even if the function returns null or undefined, it still says it has been successful.

What I tried
So, I wanted to pass something more useful to the client script, like the newly created invoice number or id but it just doesn't seem to be working. I have tried using variations of this approach:
var output = JSON.parse(response.details.output)
but get this error amongst others...
"undefined" is not valid JSON 
What doesn't help matters is that I can't fully test the invoice creation process, even using a crm sandbox, because it will still create real invoices in Books, which I would rather avoid.

What are your thoughts on how to approach this?  Below are the relevant parts of my code, in case it is useful to see.

CRM Function
  1. string standalone.createtradeinvoicesa(String orderId,String userId,String invType)
  2. {
  3. // earlier code creates invMap with invoice items
  4. // CREATE NEW INVOICE IN ZOHO BOOKS
  5. resp = zoho.books.createRecord("Invoices",orgId,invMap,"zbooksall");
  6. if(resp.get("code") == 0)
  7. {
  8. //for client script
  9. invoiceid = resp.get("invoice").get("id");
  10. invoiceinfo = zoho.books.getRecordsByID("invoices",orgId,invoiceid,"zbooksall");
  11. info "client-script-output=" + invoiceinfo;
  12. invoiceNum = resp.get("invoice").get("invoice_number");
  13. invoiceDate = today.toDate("dd/mm/yyyy");
  14. invoiceDate = invoiceDate.toString("dd/MM/yyyy");
  15. //user id and name for note
  16.    else
  17. {
  18. returnMsg = "Error : an invoice has not been created in Zoho Books.";
  19. }
  20. return invoiceinfo;
  21. }

Client Script

  1. var functionName = "xxx";
  2. // get the order and user ID of this page
  3. var record_id = $Page.record_id;
  4. var user_id = $Crm.user.id;
  5. // get field values
  6. var stage_value = ZDK.Page.getField('Stage').getValue();
  7. var orderNo = ZDK.Page.getField('Deal_Name').getValue();
  8. // other checks go before this in the full script
  9. //If user clicks Proceed button
  10. if (isProceed) {
  11. // Message
  12. var successText = 'You will find your draft invoice in the Zoho Finance section below.';
  13. var errorText = 'There has been a problem, your invoice has not been created.';
  14. // show loader
  15. ZDK.Client.showLoader({ type: 'page', template: 'spinner', message: 'Creating Invoice...' });
  16. // call function in variable
  17. var response = ZDK.Apps.CRM.Functions.execute(functionName, { "orderId": record_id, "userId": user_id });
  18. ZDK.Client.hideLoader();
  19. // show sucess or error messages and execute function
  20. // response from function is the new invoice information, so in theory, the response should be null/undefined if the invoice wasn't created.
  21. if (response) {
  22. var sucessMessage = ZDK.Client.showAlert(successText, 'Invoice Created', 'Close');
  23. return response & successMessage & $Client.refresh();
  24. } else {
  25. errorMessage = ZDK.Client.showMessage(errorText, { type: 'error' });
  26. return errorMessage;
  27. }
  28. }
  29. //If user clicks Cancel button from invoice creation
  30. else {
  31. return false;
  32. }

    Nederlandse Hulpbronnen