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:


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



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


Custom code:


  $sugarQuery = new SugarQuery();
  $letter = $_GET['letter'];
  $returnElement = [];
  $returnElement['letterId'] = '';
  $returnElement['error'] = '';
  if($letter == 'NoLetterNeeded'){
  $returnElement['error'] = 'No Letter Needed is selected.';
  $bean = BeanFactory::newBean('PdfManager');

  $sql = new SugarQuery();
  $sql->Where()->equals('name', $letter)
  $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.';
  $returnElement['error'] = 'Letter name and template do not match.';
  echo json_encode($returnElement); exit;




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




    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');
  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);'address-ok', {
                            level: 'success',
                            messages: 'Letter downloaded',
                            autoClose: true
  } else {'address-ok', {
                            level: 'error',
                            messages: letter.error,
                            autoClose: false