[SalesForce] Compile Error: Illegal string literal: Line breaks are not allowed in string literals

I am getting error for below statement.

string xmloutput = '<?xml version="1.0" encoding="UTF-8"?><ns2:gaTransactionType xmlns:ns2="http://abc.com">gaPaymentTransaction.xsd "> 
    <merchantName>Test Merchant</merchantName> 
    <merchantNumber>SNA_TEST</merchantNumber> 
    <service>SNA_TEST_DONATION</service>  
                <orderNumber>1001</orderNumber>
    <gaBatchAgency>SNA</gaoBatchAgency> 
    <gaDocNoAgency>SNA</gaoDocNoAgency> 
    <totalAmount>5000</totalAmount> 
    <summaryDescription>This is a test.</summaryDescription>     
                <test>test</test> 
    <lineItemList> 
        <lineItem> 
            <gaoProductCode>SN000001</gaoProductCode> 
            <gaoAgency>SNA</gaoAgency> 
            <function>19000</function> 
            <revenueSource>4612</revenueSource> 
            <departmentRevenueSource></departmentRevenueSource> 
            <accountingTemplate></accountingTemplate> 
            <gaoDescription>BORDER FD</gaoDescription> 
            <amount>5000</amount> 
            <quantity>1</quantity> 
        </lineItem> 
    </lineItemList> 
</ns2:gaTransactionType>';

Best Answer

You are facing this issue because you are trying to add multi line statement. You can't directly add this. You have two options here.

  • Convert this into single line and then add

    <?xml version="1.0" encoding="UTF-8"?><ns2:gaTransactionType xmlns:ns2="http://abc.com">gaPaymentTransaction.xsd "><merchantName>Test Merchant</merchantName><merchantNumber>SNA_TEST</merchantNumber><service>SNA_TEST_DONATION</service><orderNumber>1001</orderNumber><gaBatchAgency>SNA</gaoBatchAgency><gaDocNoAgency>SNA</gaoDocNoAgency><totalAmount>5000</totalAmount><summaryDescription>This is a test.</summaryDescription><test>test</test><lineItemList><lineItem><gaoProductCode>SN000001</gaoProductCode><gaoAgency>SNA</gaoAgency><function>19000</function><revenueSource>4612</revenueSource><departmentRevenueSource></departmentRevenueSource><accountingTemplate></accountingTemplate><gaoDescription>BORDER FD</gaoDescription><amount>5000</amount><quantity>1</quantity></lineItem></lineItemList></ns2:gaTransactionType>

  • Second option to you need to do something like. Add them together and then use.

     string xmloutput = 'gaPaymentTransaction.xsd ">'+ 
        'Test Merchant '+
        'SNA_TEST'+ 
        'SNA_TEST_DONATION'+
    
    
                // complete code
        '</lineItem>'+ 
    '</lineItemList>'+ 
    
    '</ ns2:gaTransactionType>';