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
Akansha Singh 28Akansha Singh 28 

Error Line: -1, Column: -1 System.StringException: Invalid id: Id

apex Class :  public class NewClass {
    public list<account> acct{get;set;}
    public String csvString1{get;set;}
    public String csvString{get;set;}
    public string renderAsExcel{get;set;}
    public NewClass(){
        List<String> listOfAccountsFields = new List<String>{'Id','Name','Type' ,'Rating' ,'Ownership' , 'Active__c'};
            List<String> listOfContactsFields = new List<String>{'Id','LastName'};
                String csvString = String.join(listOfAccountsFields,',')+'\n';
        String csvString1= String.join(listOfContactsFields,',')+'\n';
        acct=[SELECT Id,Name,Type ,Rating ,Ownership , Active__c ,(SELECT Id,lastName from Contacts)
              From Account WHERE Name LIKE'%Account%' LIMIT 10];
        List<Contact> listOfContact = new List<Contact>();
        for(Account accRecord: acct) {
            listOfContact.addAll(accRecord.Contacts);
            //System.debug(listOfContact);
        }
        Map<Id,String> mappingAccAndCon = new Map<Id,String>();
        for(Contact abc: [SELECT AccountId, lastName FROM Contact]) {
            mappingAccAndCon.put(abc.AccountId,abc.lastName);
            system.debug(mappingAccAndCon);
            if(mappingAccAndCon.containsKey(abc.AccountId)){
                mappingAccAndCon.put(abc.AccountId,mappingAccAndCon.get(abc.AccountId)+abc.lastName);   
            }
        }
        for(Account acc:acct){
            for(String fieldName : listOfAccountsFields){
                csvString += '\"'+acc.get(fieldName)+'\",';
            }
            csvString += '\n';
            //csvString += map.get(acc.Id);
            for(String fieldName1 :listOfContactsFields){
                     csvString1 += '\"'+mappingAccAndCon.get(fieldName1)+'\",';
                }
                  csvString1 += '\n';
                system.debug(csvString1);
        }
        renderAsExcel= csvString;
        //system.debug(renderAsExcel);
    }
    public PageReference CSVExporter() {
        PageReference p = new PageReference('/apex/CSVExporter');
        p.setRedirect(true);
        return p;
        
    }
}



vf page 1:<apex:page controller="NewClass" tabStyle="Account">
    <apex:form >
        <apex:pageBlock > 
            <style>
                .myClass{
                color:white !important;
                background: #66b3ff !important;
                }
            </style>
            <Apex:commandButton value="Export as CSV" action="{!CSVExporter}" StyleClass="myClass" />       
        </apex:pageBlock>
    <apex:pageBlock >
            <table border="1">
                <tr>
                    <th>Name</th>
                    <th>Rating</th>
                    <th>Type</th>
                    <th>Active</th>
                    <th>Ownership</th>
                </tr>
                <apex:repeat var="acc" value="{!acct}">
                    <tr>
                        <td>{!acc.Name}</td>
                        <td>{!acc.Rating}</td>
                        <td>{!acc.Type}</td>
                        <td>{!acc.Active__c}</td>
                        <td>{!acc.Ownership}</td></tr>
                    <tr>
                        <th>   </th>
                        <th> Contact Id</th>   
                        <th>LastName</th>
                    </tr>
                    <apex:repeat var="c" value="{!acc.Contacts}">
                        <tr>
                            <td></td>
                            <td>{!c.id}</td>
                            <td>{!c.lastName}</td>
                        </tr>
                    </apex:repeat>
                </apex:repeat> 
            </table>
        </apex:pageBlock>
    </apex:form>
            </apex:page>

vf page 2:
<apex:page controller="NewClass" contentType="text/csv#Test.csv">
    <apex:outputText value="{!renderAsExcel}">
    </apex:outputText>
</apex:page>

please reply asap
Suraj Tripathi 47Suraj Tripathi 47
Hi Akansha,

Visualforce page is Case sensitive so you have to put Name as API Name Of contact.
In VisualForce page, correct as the below code
<apex:repeat var="c" value="{!acc.Contacts}">
                        <tr>
                            <td></td>
                            <td>{!c.Id}</td>
                            <td>{!c.LastName}</td>
                        </tr>
                    </apex:repeat>

And in apex, for(Contact abc: [SELECT AccountId, lastName FROM Contact]) correct as the below code, if you have not put the accountid is not equal to null then it gives null pointer exception.
Map<Id,String> mappingAccAndCon = new Map<Id,String>();
        for(Contact abc: [SELECT AccountId, lastName FROM Contact where accountid!=null]) {
            mappingAccAndCon.put(abc.AccountId,abc.lastName);
            system.debug(mappingAccAndCon);
            if(mappingAccAndCon.containsKey(abc.AccountId)){
                mappingAccAndCon.put(abc.AccountId,mappingAccAndCon.get(abc.AccountId)+abc.lastName);   
            }
        }
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

 
Akansha Singh 28Akansha Singh 28
Hi 
Suraj Tripathi 47


that can be the problem too but rn my vf page is showing invaild id.