0

Json response i got from facebook API. Used wrapper class to deserialize.

{
"accounts" : {
    "data" : [{
            "picture" : {
                "data" : {
                    "is_silhouette" : true,
                    "url" : "https://scontent.xx.fbcdn.net/v/t1.0-1/c15.0.50.50/p50x50/405770_10150000700901972_765915345_n.png?oh=b9dfdfgg40ebc96d0992e27255ccc5a16d678&oe=58F973E1"
                }
            },
            "id" : "746973423232342126509",
            "name" : "Sneakpick"
    } 
    ],
    "paging" : {
        "cursors" : {
            "before" : "NzQ2OTczNfdgfDQyMTI2NTA5",
            "after" : "MTYxNjM2NdfgfzE3ODY1ODk5MQZDZD"
        }
    }
},
"id" : "115020323171435432395"
}

Wrapper class created using https://json2apex.herokuapp.com/

global class facebookpages {
global facebookpages()
{
} 
 global Accounts accounts {get;set;} 
global String id {get;set;}    
// inner class level 1 accounts 
global class Accounts {
    global List<Data_Z> data {get;set;} 
    global Paging paging {get;set;}

    global Accounts(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'data') {
                        data = new List<Data_Z>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            data.add(new Data_Z(parser));
                        }
                    } else if (text == 'paging') {
                        paging = new Paging(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Accounts consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}
// inner class level 1 paging
global class Paging {
    global Cursors cursors {get;set;} 

    global Paging(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'cursors') {
                        cursors = new Cursors(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Paging consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}
 //    inner class  level 2 Cursors 
global class Cursors {
    global String before {get;set;} 
    global String after {get;set;} 

    global Cursors(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'before') {
                        before = parser.getText();
                    } else if (text == 'after') {
                        after = parser.getText();
                    } else {
                        System.debug(LoggingLevel.WARN, 'Cursors consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}
// inner classes level 2 Data_Z 
global class Data_Z {
    global Picture picture {get;set;} 
    global String name {get;set;} 
    global String id {get;set;} 

    global Data_Z(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'picture') {
                        picture = new Picture(parser);
                    } else if (text == 'name') {
                        name = parser.getText();
                    } else if (text == 'id') {
                        id = parser.getText();
                    } else {
                        System.debug(LoggingLevel.WARN, 'Data_Z consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}     
 // innner class level 3 picture     
global class Picture {
    global Data data {get;set;} 

    global Picture(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'data') {
                        data = new Data(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Picture consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}
// inner class level 4 data 
global class Data {
    global Boolean is_silhouette {get;set;} 
    global String url {get;set;} 

    global Data(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'is_silhouette') {
                        is_silhouette = parser.getBooleanValue();
                    } else if (text == 'url') {
                        url = parser.getText();
                    } else {
                        System.debug(LoggingLevel.WARN, 'Data consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

 // level 0 main class methods and properties 
global static void consumeObject(JSONParser parser) {
    Integer depth = 0;
    do {
        JSONToken curr = parser.getCurrentToken();
        if (curr == JSONToken.START_OBJECT || 
            curr == JSONToken.START_ARRAY) {
            depth++;
        } else if (curr == JSONToken.END_OBJECT ||
            curr == JSONToken.END_ARRAY) {
            depth--;
        }
    } while (depth > 0 && parser.nextToken() != null);
}

global facebookpages(JSONParser parser) {
    while (parser.nextToken() != JSONToken.END_OBJECT) {
        if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
            String text = parser.getText();
            if (parser.nextToken() != JSONToken.VALUE_NULL) {
                if (text == 'accounts') {
                    accounts = new Accounts(parser);
                } else if (text == 'id') {
                    id = parser.getText();
                } else {
                    System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
                    consumeObject(parser);
                }
            }
        }
    }
}
global static facebookpages parse(String json) {
    return new facebookpages(System.JSON.createParser(json));
}
}

Debug log after this i am getting data in this class object that is perfect.

|DEBUG|facebookpages:[accounts=Accounts:[data=(Data_Z:[id=74‌​6973509, name=Sneakpick, picture=Picture:[data=Data:[is_silhouette=true, url=scontent.xx.fbcdn.net/v/t1.0-1/c15.0.50.50/p50x50/…, Data_Z:[id=1616367158991, name=Busyday, picture=Picture:[data=Data:[is_silhouette=true, url=scontent.xx.fbcdn.net/v/t1.0-1/c15.0.50.50/p50x50/…), paging=Paging:[cursors=Cursors:[after=MTYxNjM2NzE3Y1ODMQZDZD‌​, before=NzQ2OTczNDTI2A5]]], id=11470363662]

I want to insert these pages names and ID using batch apex. tried as below. stucked in Start method what to return and is there require to fire query ?.

global class addfbpagestosystem implements Database.Batchable<sObject> 
{

 global final list<facebookpages.Data_Z> fbpages;
 global final String id;
 global addfbpagestosystem(list<facebookpages.Data_Z> fbpages,String id) 
 {
    this.id =id;
    this.fbpages=fbpages;
 }   
 global Iterable<sObject> start(Database.BatchableContext BC)
{ // stucked in this method.
 /* MBT_SFFB__Facebook_Page__c objfbpage = new MBT_SFFB__Facebook_Page__c()
  return MBT_SFFB__Facebook_Page__c;*/
}
global void execute(Database.BatchableContext BC,List<sObject> scope)
{    
   list<MBT_SFFB__Facebook_Page__c> objmtfbpage = new list<MBT_SFFB__Facebook_Page__c>();
   for(facebookpages.Data_Z objfbpage :fbpages)
   {
       MBT_SFFB__Facebook_Page__c objrecfbpage = new MBT_SFFB__Facebook_Page__c();
       objrecfbpage.MBT_SFFB__Facebook_ID__c=id;
       objrecfbpage.MBT_SFFB__Facebook_Page_ID__c=objfbpage.id;
       objrecfbpage.MBT_SFFB__Facebook_Page_Name__c=objfbpage.name;
       objrecfbpage.MBT_SFFB__Image__c=objfbpage.picture.data.url;           
       objrecfbpage.MBT_SFFB__Facebook_User_Relates_To_Page__c='a0H2800000B6nR0';
       objmtfbpage.add(objrecfbpage);
   }
    insert objmtfbpage;

}
global void finish(Database.BatchableContext BC)
{
}
}
2
  • 1
    I don't really see why you need a batch Apex class at all just to insert what appears to be a handful of records. What are you trying to do?
    – sfdcfox
    Commented Dec 16, 2016 at 12:30
  • There are No of facebook pagesnames and facebook pages post i need to insert into Salesforce object. for that i want to use batch apex. Page may contain No of posts. Commented Dec 16, 2016 at 12:34

1 Answer 1

0

I believe you are getting confused with the working of the batch. Batch works via querying the records in the database in start Method and iterating it in execute. What I can see is you are not querying anything in start and hence your Batch wont work.

You can read here. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

7
  • Do i need to fire query for the object in which i am inserting ? Here i am confused Please clarify. Commented Dec 16, 2016 at 12:26
  • You cannot querry something that you have not inserted. As you are not querying anything I doubt if you really need a batch in the first place. Let me give you a use case for batch. For every Account in SF you need to make a callout ont fb fetch data and save into database. Commented Dec 16, 2016 at 12:33
  • Right now batch is working but not inserting anything.. Commented Dec 16, 2016 at 12:37
  • How are you executing batch? Provide syntax Commented Dec 16, 2016 at 12:40
  • 2
    Pranay, if you examine the batchable documentation more carefully, you will see that any Iterable can be returned from the start method; its most common for the Iterable (or QueryLocator) to be for an SObject but that is not required and sometimes it makes sense to return something else.
    – Keith C
    Commented Dec 16, 2016 at 12:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .