Link to home
Start Free TrialLog in
Avatar of Stacie
StacieFlag for United States of America

asked on

Ajax calling a function

I'm trying call this following function in this ajax call,  var myReturn = processLinkedDocuments(reportId, localInvoiceFormatType); but is skipping it at every single time. Not sure if it's because I have it between some if/Statement but no luck... It never call the processLinkedDocuments function

 $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: '@Url.Action("PrintInvoiceStoreData", "InvoicePrinting")',
                    data: data,
                    success: function (response) {
                        if (response.Success) {
                            reportId = response.Message;

                            var reportCount = 0;
                            if ($('#s3foCallAheadList').prop('checked')) {
                                reportCount++;
                                var url = '@printInvoicesCallAheadReportUrl'.replace("CBNR", reportId);
                                console.log("test1");
                                window.open(url);
                            }

                            if ($('#s3foSchedule').prop('checked')) {
                                reportCount++;
                                var url = '@printInvoicesScheduleUrl'.replace("CBNR", reportId);
                                console.log("test2");
                                window.open(url);
                            }

                            if ($('#s3foWorksheet').prop('checked')) {
                                reportCount++;
                                var url = '@printInvoicesWorkSheetReportUrl'.replace("CBNR", reportId);
                                console.log("test3");
                                window.open(url);
                            }

                            if ($('#s3foDriverReport').prop('checked')) {
                                reportCount++;
                                var url = '@printInvoicesDriverReportUrl'.replace("CBNR", reportId);
                                console.log("test4");
                                window.open(url);
                            }
                            
                            var myReturn = processLinkedDocuments(reportId, localInvoiceFormatType);
                            Loading.end();
                            console.log("test55");
                            if (!isPreview) {

                                removeSelectedBatchRows();
                                console.log("test6");
                            }
                        }
                        else {
                            Loading.end();
                            GenerateStatus = false;
                            swal({
                                title: '@ServiceAssistant.Tools.Language.Translate(ServiceAssistant.Tools.Language.Keys.Error, ViewBag.LoggedInUser.Culture)',
                                text: response.Message,
                                type: 'error',
                                closeOnCofirm: true,
                                closeOnCancel: true
                            });
                        }
                    }
                    , error: function (jqXHR, error, errorThrown) {
                        GenerateStatus = false;
                        Loading.end();
                        swal({
                            title: '@ServiceAssistant.Tools.Language.Translate(ServiceAssistant.Tools.Language.Keys.Error, ViewBag.LoggedInUser.Culture)',
                            text: error,
                            type: 'error',
                            closeOnCofirm: true,
                            closeOnCancel: true
                        });
                    }
                });

Open in new window


 var processLinkedDocuments = function (reportId, invoiceFormatType) {
            var deferred = $.Deferred();

            var url = '@Url.Action("ProcessLinkedDocuments", "InvoicePrinting", new { area = "Invoices" })';
            console.log("test2222");
            var saveLinkedDocs = isSaveLinkedDocsChecked();
            var documentCategoryId = getSaveLinkedDocsCategory();
            var documentDescription = getSaveLinkedDocsDescription();

            var emailLinkedDocs = isEmailLinkedDocsChecked();
            var emailSubject = getEmailLinkedDocsSubject();
            var emailMessage = getEmailLinkedDocsMessage();
            var emailReplyTo = getEmailReplyToEmailAddress();

            var data = {
                guidToken: reportId,
                saveLinkedDoc: saveLinkedDocs,
                documentDescription: documentDescription,
                documentCategoryId: documentCategoryId,
                emailLinkedDoc: emailLinkedDocs,
                emailSubject: emailSubject,
                emailMessage: emailMessage,
                invoiceFormatType: invoiceFormatType,
                emailReplyTo: emailReplyTo,
            };

            if (saveLinkedDocs || emailLinkedDocs) {
                $.post(url, data)
                .done(function (response) {
                    if (response.Html) {
                        deferred.resolve(response);

                        swal({
                            html: true,
                            title: 'Linked Documents',
                            text: response.Html,
                            type: 'info',
                            showCancelButton: false,
                            confirmButtonClass: "btn-success",
                            confirmButtonColor: "#5cb85c",
                            confirmButtonText: "Ok",
                            cancelButtonColor: "#5cb85c",
                            cancelButtonText: "No",
                            closeOnConfirm: true,
                            closeOnCancel: true,
                            allowEscapeKey: true,
                        });

                    }
                })
                .fail(function (err) {
                    alert(err.responseText);
                    deferred.resolve();
                }).always(function () {
                    // nothing
                });
            }
            else {
                deferred.resolve();
            }

            return deferred.promise();
        };  //

Open in new window

Avatar of Shahzad Fateh Ali
Shahzad Fateh Ali
Flag of Pakistan image

Hi,

Can you please share how the initial function is being used and what is the use of "myReturn"?

Thanks
Avatar of Stacie

ASKER

 $.when(myReturn).done(function (response) {
                            Loading.end();

                            // EXPERIMENT - START
                            //
                            //  The callback returns the number of remaining invoices in the response.Message property.
                            //
                            // EXPERIMENT - END

                            console.log("Test is done");

                            if (response == null || (response != null && response.Message != null && (typeof (response.Message) != 'undefined') && Number(response.Message) > 0)) {
                                window.open(url);
                            }
                            else {
                                Notification.flash('No remaining invoices to print.');
                            }

                            UnselectAllCheckboxes();

Open in new window

Firstly why are you creating a promise when $.post returns a promise anyway

Secondly where does this come into your original post
$.when(myReturn).done(function (response) {

Open in new window


You set myReturn on line 59 of the first post
var myReturn = processLinkedDocuments(reportId, localInvoiceFormatType);

Open in new window


But never use it after that

Question: how do you know your function processLinkedDocuments is not being called - are you seeing the
console.log("test2222");

Open in new window


If you are referring to the if statement on line 27 of your second code snippet
if (saveLinkedDocs || emailLinkedDocs) {

Open in new window

Then what do
isSaveLinkedDocsChecked()
And
isEmailLinkedDocsChecked()
I cannot see below code using the current variable myReturn
$.when(myReturn).done(function (response) {...}

Open in new window



myReturn is a local variable to the success function with your $.post call. If will only be available if you use it within that success function or declare myReturn as global variable.

BTW, It seems like you are using unnecessary promises in your code. You can clean and simplify your code.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.