AnsweredAssumed Answered

Ajax redirecting to login instead of custom code

Question asked by Mary Warner on Apr 11, 2016
Latest reply on Dec 8, 2016 by Dan Ross

Sugar 7.6 On-Site

I have added a custom button in my instance which takes some information from the page and downloads a PDF.

 

The button does a ajax call, but instead of returning 200, it is returning 302 Found.

When looking at the Network tab under the browser debugger it goes to the url that I want:

http://[sugarURL]/index.php?module=FUND_Donation&action=getPDFLetter&letter=DDMemorial&to_pdf=1

but then throws a 302 found and redirects to this url:

http://[sugarURL]/index.php?action=Login&module=Users&login_module=FUND_Donation&login_action=getPDFLetter&login_letter=DDMemorial&login_to_pdf=1

 

I'm just not really sure where this is erroring out.

 

Custom code:

/custom/CUSTOM_MODULE/getPDFLetter.php

<?php
  require_once('include/SugarQuery/SugarQuery.php');
  $sugarQuery = new SugarQuery();
  $letter = $_GET['letter'];
  $returnElement = [];
  $returnElement['letterId'] = '';
  $returnElement['error'] = '';
  if($letter == 'NoLetterNeeded'){
  $returnElement['error'] = 'No Letter Needed is selected.';
  }else{
  $bean = BeanFactory::newBean('PdfManager');

  $sql = new SugarQuery();
  $sql->select('id');
  $sql->from($bean);
  $sql->Where()->equals('name', $letter)
  ->equals('published','yes');
  $result = $sql->execute();


  $count = count($result);


  if ($count == 1){
  $bean = BeanFactory::getBean('PDFManager', $result[0]['id']);
  $returnElement['letterId'] = $result[0]['id'];
  }else if($count > 1){
  $returnElement['error'] = 'There is more than one letter with that name.  Contact Support to resolve.';
  }else{
  $returnElement['error'] = 'Letter name and template do not match.';
  }
  }
  echo json_encode($returnElement); exit;

 

custom/Extension/modules/FUND_Donation/Ext/Language/en_us.printDonationLetter.php

 

$mod_strings['LBL_PRINT_DONATION_LETTER'] = "Print Letter";

 

 

custom/modules/CUSTOM_MODULE/clients/base/views/record

({
    extendsFrom: 'RecordView',
    zipJSON: {},
    initialize: function (options) {
        app.view.invokeParent(this, {type: 'view', name: 'record', method: 'initialize', args:[options]});
        //add listener for custom button
        this.context.on('button:print_donation_letter:click', this.print_donation_letter, this);
    },
    print_donation_letter: function() {
        //example of getting field data from current record
  var pdfLetter = this.model.get('thank_you_letter');
  var donationId = this.model.get('id');
  $.ajax({
  url: '/index.php?module=FUND_Donation&action=getPDFLetter&letter='+pdfLetter+'&to_pdf=1',
  dataType: 'json',
  success: function(data) {
  var letter = data;
  if(letter.letterId != undefined && letter.letterId != '')
  {
  window.location.replace('/index.php?module=FUND_Donation&record='+donationId+'&action=sugarpdf&sugarpdf=pdfmanager&pdf_template_id='+letter.letterId);
  app.alert.show('address-ok', {
                            level: 'success',
                            messages: 'Letter downloaded',
                            autoClose: true
                        });
  } else {
  app.alert.show('address-ok', {
                            level: 'error',
                            messages: letter.error,
                            autoClose: false
                        });
  }
  }
  });
  }
})

Outcomes