• java salesforce
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
error at comment line. 

@RestResource(urlMapping='/RestWebservice1/*')
global class RestWebservice1 {
@HttpGet
    global static string doGet(){
       List<Account> accs = [select Name,Phone from Account limit 4];
        System.JSONGenerator jg = JSON.createGenerator(true);
        jg.writeStartObject();
        jg.writeFieldName('Status');
        if(accs.size()>0){
            jg.writeString('Success');
            jg.writeNumberField('count',accs.size());
            jg.writeFieldName('Accounts');
            for(Account a: accs){
                jg.writeStartObject();    // I'm getting error here 
                jg.writeStringField('Name', a.Name);
                jg.writeStringField('Phone', a.Phone);
                jg.writeEndObject();
            }
        }else{
            jg.writeString('Failed');
            jg.writeStringField('Reason', 'No Records Found');    
        }
        jg.writeEndObject();
            string result=jg.getAsString();
            System.debug('Json String :'+result);
        return result;
    } 
}
error at comment line. 

@RestResource(urlMapping='/RestWebservice1/*')
global class RestWebservice1 {
@HttpGet
    global static string doGet(){
       List<Account> accs = [select Name,Phone from Account limit 4];
        System.JSONGenerator jg = JSON.createGenerator(true);
        jg.writeStartObject();
        jg.writeFieldName('Status');
        if(accs.size()>0){
            jg.writeString('Success');
            jg.writeNumberField('count',accs.size());
            jg.writeFieldName('Accounts');
            for(Account a: accs){
                jg.writeStartObject();    // I'm getting error here 
                jg.writeStringField('Name', a.Name);
                jg.writeStringField('Phone', a.Phone);
                jg.writeEndObject();
            }
        }else{
            jg.writeString('Failed');
            jg.writeStringField('Reason', 'No Records Found');    
        }
        jg.writeEndObject();
            string result=jg.getAsString();
            System.debug('Json String :'+result);
        return result;
    } 
}