function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brian Cherry FWIBrian Cherry FWI 

Compile Error: line breaks not allowed in string literals at line 14 column -1

Here is my code:
public expenseController(ApexPages.StandardController controller) { this.proj = (Expense_Report__c) controller.getSubject(); this.delivers = '[SELECT d.Name, d.Billable__c, d.Comments__c, d.Travel_type__c, d.Purchase_type__c, d.Expense_Report__c, d.Id, d.Purchase_Date__c, d.Quantity__c, d.Sales_Tax__c, d.Shipping__c, d.total__c, d.type__c, d.unit_price__c, d.Description__c,d.Price_Override__c,d.Override_Price__c'+ 'FROM Expense_Line_Items__c d' + 'WHERE d.Expense_Report__c = : proj.id' + 'Order by' + 'sortOrder' + 'DESC' + ']'; }


Any thoughts why I'm getting this error?
 
Deepak GulianDeepak Gulian
Please make sure that String this.delivers should be in one single line, like in a screenshot
User-added image
Mahesh DMahesh D
public expenseController(ApexPages.StandardController controller) { 
	this.proj = (Expense_Report__c) controller.getSubject(); 
	this.delivers = '[SELECT d.Name, d.Billable__c, d.Comments__c, d.Travel_type__c, '+ 			'd.Purchase_type__c, d.Expense_Report__c, d.Id, d.Purchase_Date__c, '+ 
	'd.Quantity__c, d.Sales_Tax__c, d.Shipping__c, d.total__c, d.type__c, d.unit_price__c, '+ 	'd.Description__c,d.Price_Override__c,d.Override_Price__c FROM Expense_Line_Items__c d ' + 
	'WHERE d.Expense_Report__c = '+ proj.id +' Order by Name DESC ]'; 
}
Please try this.

Regards,
Mahesh
 
Brian Cherry FWIBrian Cherry FWI
Using that gives me this:
 Compile Error: Illegal assignment from String to List<Expense_Line_Items__c> at line 16 column 5

What I am trying to do is have a variable string determine the sort order. I can't do it by simply Order by :SortVariable.  
Mahesh DMahesh D
Please paste your full program so that it will be easy to troubleshoot.
Richard Jimenez 9Richard Jimenez 9
Hi Brian,

I think you need to use database.query to execute the query string. This will then return you a list of sobjects.

For more information on using Dynamic SOQL see: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm

Hope that helps,
Richard Jimenez
Brian Cherry FWIBrian Cherry FWI
public class expenseController {
    // class variables
    private string sortOrder = 'd.PurchaseDate__c';
    PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
    Expense_Report__c proj;
    Expense_Line_Items__c[] delivers;
    public string SelectedAccountId {get;set;}
    

    // Constructor
    public expenseController(ApexPages.StandardController controller) {
        this.proj = (Expense_Report__c) controller.getSubject();
     
        this.delivers = [SELECT
            d.Name, d.Billable__c, d.Comments__c, d.Travel_type__c, d.Purchase_type__c,
            d.Expense_Report__c, d.Id, d.Purchase_Date__c, 
            d.Quantity__c, d.Sales_Tax__c, d.Shipping__c, 
            d.total__c, d.type__c, d.unit_price__c, d.Description__c,d.Price_Override__c,d.Override_Price__c
            FROM Expense_Line_Items__c d
            WHERE d.Expense_Report__c = : proj.id
            Order by d.Travel_type__c DESC
            
        ];
    }

    public pagereference insertmethod() {
        Expense_Line_Items__c cc = new Expense_Line_Items__c();
        cc.Expense_Report__c = proj.id;
        insert cc;
        return null;
    }

    public Expense_Line_Items__c[] getDeliverables() {
        return this.delivers;
    }

    // Action Method called from page button
    public pagereference saveChanges() {
        upsert this.delivers;
        pageRef.setRedirect(true);
        return pageRef;
    }

    // Action Method called from page link
    public pagereference newDeliverable() {
       Expense_Line_Items__c d = new Expense_Line_Items__c();
        d.Expense_Report__c = this.proj.id;
        delivers.add(d);
        getDeliverables();
        return null;
    }

    public pagereference DeleteAccount() {
        // if for any reason we are missing the reference
        if (SelectedAccountId == null) {
            return null;
        }
        // find the account record within the collection
        Expense_Line_Items__c tobeDeleted = null;
        for (Expense_Line_Items__c a: this.delivers)
            if (a.Id == SelectedAccountId) {
                tobeDeleted = a;
                break;
            }
            //if account record found delete it
        if (tobeDeleted != null) {
            Delete tobeDeleted;
        }
        pageRef.setRedirect(true);
        return pageRef;
    }
 //Attachments
   public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }

  
 public PageReference upload() {
  if(Test.isRunningTest()){
       Blob b = Blob.valueOf('Test Data');
    attachment.Name = 'Name of the attachment';
    attachment.body = b;
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId =  proj.id;
    attachment.IsPrivate = false;
    insert attachment;
    pageRef.setRedirect(true);
    return pageRef;

    }
    else
        {
      attachment.OwnerId = UserInfo.getUserId();
      attachment.ParentId =  proj.id;
      attachment.IsPrivate = false;
      insert attachment;
      pageRef.setRedirect(true);
      return pageRef;
        }
    }
}

I want to bea ble to use the string  SortOrder to do the sorting as I plan to update the string through VS page
 
Mahesh DMahesh D
Please use the below code:
 
String sortOrder {get; set;}
public expenseController(ApexPages.StandardController controller) { 
	sortOrder = ((sortOrder != null && sortOrder != '') ? sortOrder : 'Name');
	String queryStr = (Expense_Report__c) controller.getSubject(); 
	this.delivers = '[SELECT d.Name, d.Billable__c, d.Comments__c, d.Travel_type__c, '+ 			'd.Purchase_type__c, d.Expense_Report__c, d.Id, d.Purchase_Date__c, '+ 
	'd.Quantity__c, d.Sales_Tax__c, d.Shipping__c, d.total__c, d.type__c, d.unit_price__c, '+ 	'd.Description__c,d.Price_Override__c,d.Override_Price__c FROM Expense_Line_Items__c d ' + 
	'WHERE d.Expense_Report__c = '+ proj.id +' Order by ' +sortOrder +' DESC ]'; 
	this.delivers = Database.query(queryStr);
}

Regards,
Mahesh
Brian Cherry FWIBrian Cherry FWI
Yeah I’m still getting the Illegal assignment from Expense_Report__c to String at line 13 column 12 Brian Cherry Business Intelligence Analyst
Mahesh DMahesh D
Please paste your latest code here with proper line numbers (which it is showing the error).
Brian Cherry FWIBrian Cherry FWI
public class expenseController {
    // class variables
    PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
    Expense_Report__c proj;
    Expense_Line_Items__c[] delivers;
    public string SelectedAccountId {get;set;}
    String sortOrder {get; set;} 
    
public expenseController(ApexPages.StandardController controller) {  
    sortOrder = ((sortOrder != null && sortOrder != '') ? sortOrder : 'Name'); 
    String queryStr = (Expense_Report__c) controller.getSubject();  
    this.delivers = '[SELECT d.Name, d.Billable__c, d.Comments__c, d.Travel_type__c, '+             'd.Purchase_type__c, d.Expense_Report__c, d.Id, d.Purchase_Date__c, '+  
    'd.Quantity__c, d.Sales_Tax__c, d.Shipping__c, d.total__c, d.type__c, d.unit_price__c, '+   'd.Description__c,d.Price_Override__c,d.Override_Price__c FROM Expense_Line_Items__c d ' +  
    'WHERE d.Expense_Report__c = '+ proj.id +' Order by ' +sortOrder +' DESC ]';  
    this.delivers = Database.query(queryStr); 
} 

    public pagereference insertmethod() {
        Expense_Line_Items__c cc = new Expense_Line_Items__c();
        cc.Expense_Report__c = proj.id;
        insert cc;
        return null;
    }

    public Expense_Line_Items__c[] getDeliverables() {
        return this.delivers;
    }

    // Action Method called from page button
    public pagereference saveChanges() {
        upsert this.delivers;
        pageRef.setRedirect(true);
        return pageRef;
    }

    // Action Method called from page link
    public pagereference newDeliverable() {
       Expense_Line_Items__c d = new Expense_Line_Items__c();
        d.Expense_Report__c = this.proj.id;
        delivers.add(d);
        getDeliverables();
        return null;
    }

    public pagereference DeleteAccount() {
        // if for any reason we are missing the reference
        if (SelectedAccountId == null) {
            return null;
        }
        // find the account record within the collection
        Expense_Line_Items__c tobeDeleted = null;
        for (Expense_Line_Items__c a: this.delivers)
            if (a.Id == SelectedAccountId) {
                tobeDeleted = a;
                break;
            }
            //if account record found delete it
        if (tobeDeleted != null) {
            Delete tobeDeleted;
        }
        pageRef.setRedirect(true);
        return pageRef;
    }
 //Attachments
   public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }

  
 public PageReference upload() {
  if(Test.isRunningTest()){
       Blob b = Blob.valueOf('Test Data');
    attachment.Name = 'Name of the attachment';
    attachment.body = b;
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId =  proj.id;
    attachment.IsPrivate = false;
    insert attachment;
    pageRef.setRedirect(true);
    return pageRef;

    }
    else
        {
      attachment.OwnerId = UserInfo.getUserId();
      attachment.ParentId =  proj.id;
      attachment.IsPrivate = false;
      insert attachment;
      pageRef.setRedirect(true);
      return pageRef;
        }
    }
}

Error: Error Error: Compile Error: Illegal assignment from Expense_Report__c to String at line 11 column 12

I really appreciate you looking into this. I think it has to do with this.delivers being a list versus a string.
Deepak GulianDeepak Gulian
This is an array Expense_Line_Items__c[] delivers; , you cannot assign String to Array at line 11.
At line 11 your mistake is whatever you put in b/w '' single quotes is considered as string & delivers is an Array.
Mahesh DMahesh D
Hi Brian,

Plese find the below modified code (Also added comments to understand it easily.)
 
public class expenseController {
    // class variables
    PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
    Expense_Report__c proj;
    Expense_Line_Items__c[] delivers;
    public string SelectedAccountId {get;set;}
    String sortOrder {get; set;} 
    
public expenseController(ApexPages.StandardController controller) {  
    sortOrder = ((sortOrder != null && sortOrder != '') ? sortOrder : 'Name'); 
	// MD -- START
    //String queryStr = (Expense_Report__c) controller.getSubject();  
    String queryStr = '[SELECT d.Name, d.Billable__c, d.Comments__c, d.Travel_type__c, '+             'd.Purchase_type__c, d.Expense_Report__c, d.Id, d.Purchase_Date__c, '+  
    'd.Quantity__c, d.Sales_Tax__c, d.Shipping__c, d.total__c, d.type__c, d.unit_price__c, '+   'd.Description__c,d.Price_Override__c,d.Override_Price__c FROM Expense_Line_Items__c d ' +  
    'WHERE d.Expense_Report__c = '+ proj.id +' Order by ' +sortOrder +' DESC ]';  
	// MD -- End
    this.delivers = Database.query(queryStr); 
} 

    public pagereference insertmethod() {
        Expense_Line_Items__c cc = new Expense_Line_Items__c();
        cc.Expense_Report__c = proj.id;
        insert cc;
        return null;
    }

    public Expense_Line_Items__c[] getDeliverables() {
        return this.delivers;
    }

    // Action Method called from page button
    public pagereference saveChanges() {
        upsert this.delivers;
        pageRef.setRedirect(true);
        return pageRef;
    }

    // Action Method called from page link
    public pagereference newDeliverable() {
       Expense_Line_Items__c d = new Expense_Line_Items__c();
        d.Expense_Report__c = this.proj.id;
        delivers.add(d);
        getDeliverables();
        return null;
    }

    public pagereference DeleteAccount() {
        // if for any reason we are missing the reference
        if (SelectedAccountId == null) {
            return null;
        }
        // find the account record within the collection
        Expense_Line_Items__c tobeDeleted = null;
        for (Expense_Line_Items__c a: this.delivers)
            if (a.Id == SelectedAccountId) {
                tobeDeleted = a;
                break;
            }
            //if account record found delete it
        if (tobeDeleted != null) {
            Delete tobeDeleted;
        }
        pageRef.setRedirect(true);
        return pageRef;
    }
 //Attachments
   public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }

  
 public PageReference upload() {
  if(Test.isRunningTest()){
       Blob b = Blob.valueOf('Test Data');
    attachment.Name = 'Name of the attachment';
    attachment.body = b;
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId =  proj.id;
    attachment.IsPrivate = false;
    insert attachment;
    pageRef.setRedirect(true);
    return pageRef;

    }
    else
        {
      attachment.OwnerId = UserInfo.getUserId();
      attachment.ParentId =  proj.id;
      attachment.IsPrivate = false;
      insert attachment;
      pageRef.setRedirect(true);
      return pageRef;
        }
    }
}

Please do let me know if it helps you.

Regards,
Mahesh