• Ashutosh Tripathi 66
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
I am fetching prospects from pardot  into salesforce. When I make a API call to pardot it fetches 200 records from pardot and get stored into SF custom object as expected. But when I make a second call it should call another 200 prospects from pardot and do the incremental store in SF custom object. Which is not happening, everytime when I make a call it returns the same 200 records even I am using "output=bulk".

Endpoint: 'https://pi.pardot.com/api/prospect/version/3/do/query?user_key='+userKey+'&'+'api_key='+apiKey+'&'+'output=bulk&format=json&created_after='+CreatedAfter+'&'+'created_before='+CreatedBefore+'&'+'sort_by=created_at&sort_order=ascending'

Here's my rest class:
           HttpRequest req = new HttpRequest();
           HttpResponse res = new HttpResponse();
           req.setEndpoint( 'https://pi.pardot.com/api/prospect/version/3/do/query?user_key='+userKey+'&'+'api_key='+apiKey+'&'+'output=bulk&format=json&created_after='+CreatedAfter+'&'+'created_before='+CreatedBefore+'&'+'sort_by=created_at&sort_order=ascending');
           req.setMethod( 'GET' );
           req.setBody( 'user_key='+userKey+'&'+'api_key='+apiKey);
           req.setHeader('Accept', 'application/json ');
           req.setHeader('Content-Type','application/json');
           res = new Http().send( req );
           System.debug('***Response****'+res.getBody());
           wrapper = (PardotProspectWrapper)JSON.deserialize(res.getBody(), PardotProspectWrapper.class);
          system.debug('******Wrapper*****'+wrapper);
        
            //this I tried to add to store the data in Pardot_Prospect__c 
            ppobjlst = new List<Pardot_Prospect__c>();
             prospectIdlist = new List<String>();
           Pardot_Prospect__c ppobj = new Pardot_Prospect__c();
            for (PardotProspectWrapper.prospect p : wrapper.result.prospect) {
                ppobj = new Pardot_Prospect__c();
                ppobj.Prospect_Id__c = string.valueOf(p.id);
                ppobj.First_name__c = p.first_name;
                ppobj.Last_Name__c = p.last_name;
                ppobj.Website__c = p.website;
                ppobj.Campaign_Name__c = p.campaign.name;
                prospectIdlist.add(string.valueOf(p.id));
                ppobjlst.add(ppobj);
            }
            
        List<Pardot_Prospect__c> dupProspectList = [Select Id, Prospect_Id__c from Pardot_Prospect__c where Prospect_Id__c in: prospectIdlist ];
            if(!dupProspectList.isEmpty()){
                System.debug('********dupProspectList*******'+dupProspectList);
                ppobj.Id = dupProspectList[0].Id;
                update dupProspectList;
            }
            else{
                System.debug('********ppobjlst*******'+ppobjlst);
               insert ppobjlst; 
            }
    
******END************
Can someone please let me know why I am getting same set of records in each call ? How would I get all the prospects from pardot using API ?
Need to write a test class for the below REST class.
public class callApi {
    public String Response;
	public String apiKey;
	List<String> prospectValues = new List<String>();
    public PardotProspectWrapper wrapper {get; set;}
    public List<PardotProspectWrapper> wrapperList {get;set;}
    public List<Pardot_Prospect__c> ppobjlst {get;set;}
    //getting values from customSetting
            SFtoPardot__c pdcred = SFtoPardot__c.getInstance('PardotCredentials');
	        String email = pdcred.PardotUserName__c;
            String password = pdcred.PardotPassword__c;
            String userKey = pdcred.API_User_Key__c;
            String CreatedAfter = pdcred.Created_After__c;
            String CreatedBefore = pdcred.Created_Before__c;
			
     //Declaring wrapper for custom object Pardot_Prospect__c
	 Pardot_Prospect__c PP = new Pardot_Prospect__c();
    
    //Authenticating Pardot with Salesforce
    public void Auth() {
		    HttpRequest req = new HttpRequest();
            req.setEndpoint( 'https://pi.pardot.com/api/login/version/4' );
            req.setMethod( 'POST' );
            req.setBody( 'email=' + email + '&password=' + password + '&user_key=' + userKey );
            
            HttpResponse res = new Http().send( req );
			
		    String response = res.getBody();
            Integer startIdx = response.indexOf( '<api_key>' ) + 9;
            Integer endIdx = response.indexOf( '</api_key>' );
            String apiKey = response.substring( startIdx, endIdx );
	        System.debug('******'+apiKey );
			getAndParse(apiKey,userKey);
        }
	
    //Getting and Parsing the response received from Pardot
	public void getAndParse(String apiKey, String userKey){
		   HttpRequest req = new HttpRequest();
           HttpResponse res = new HttpResponse();
           req.setEndpoint( 'https://pi.pardot.com/api/prospect/version/4/do/query?user_key='+userKey+'&'+'api_key='+apiKey+'&'+'output=bulk&format=json&created_after='+CreatedAfter+'&'+'created_before='+CreatedBefore+'&'+'sort_by=created_at&sort_order=ascending');
           req.setMethod( 'GET' );
           req.setBody( 'user_key='+userKey+'&'+'api_key='+apiKey);
           req.setHeader('Accept', 'application/json ');
           req.setHeader('Content-Type','application/json');
		   res = new Http().send( req );
	       System.debug('***Response****'+res.getBody());
           wrapper = (PardotProspectWrapper)JSON.deserialize(res.getBody(), PardotProspectWrapper.class);
          system.debug('******Wrapper*****'+wrapper);
        
            //this I tried to add to store the data in Pardot_Prospect__c 
			ppobjlst = new List<Pardot_Prospect__c>();
           Pardot_Prospect__c ppobj = new Pardot_Prospect__c();
			for (PardotProspectWrapper.prospect p : wrapper.result.prospect) {
                ppobj = new Pardot_Prospect__c();
                ppobj.Prospect_Id__c = string.valueOf(p.id);
                ppobj.First_name__c = p.first_name;
                ppobj.Last_Name__c = p.last_name;
                ppobj.Company__c = p.company;
                ppobj.Grade__c = p.grade;
                ppobj.Score__c = p.score;
				ppobjlst.add(ppobj);
			}
			insert ppobjlst;     
		}    
    
    }

 
Need to write a test class for the below REST class.
public class callApi {
    public String Response;
	public String apiKey;
	List<String> prospectValues = new List<String>();
    public PardotProspectWrapper wrapper {get; set;}
    public List<PardotProspectWrapper> wrapperList {get;set;}
    public List<Pardot_Prospect__c> ppobjlst {get;set;}
    //getting values from customSetting
            SFtoPardot__c pdcred = SFtoPardot__c.getInstance('PardotCredentials');
	        String email = pdcred.PardotUserName__c;
            String password = pdcred.PardotPassword__c;
            String userKey = pdcred.API_User_Key__c;
            String CreatedAfter = pdcred.Created_After__c;
            String CreatedBefore = pdcred.Created_Before__c;
			
     //Declaring wrapper for custom object Pardot_Prospect__c
	 Pardot_Prospect__c PP = new Pardot_Prospect__c();
    
    //Authenticating Pardot with Salesforce
    public void Auth() {
		    HttpRequest req = new HttpRequest();
            req.setEndpoint( 'https://pi.pardot.com/api/login/version/4' );
            req.setMethod( 'POST' );
            req.setBody( 'email=' + email + '&password=' + password + '&user_key=' + userKey );
            
            HttpResponse res = new Http().send( req );
			
		    String response = res.getBody();
            Integer startIdx = response.indexOf( '<api_key>' ) + 9;
            Integer endIdx = response.indexOf( '</api_key>' );
            String apiKey = response.substring( startIdx, endIdx );
	        System.debug('******'+apiKey );
			getAndParse(apiKey,userKey);
        }
	
    //Getting and Parsing the response received from Pardot
	public void getAndParse(String apiKey, String userKey){
		   HttpRequest req = new HttpRequest();
           HttpResponse res = new HttpResponse();
           req.setEndpoint( 'https://pi.pardot.com/api/prospect/version/4/do/query?user_key='+userKey+'&'+'api_key='+apiKey+'&'+'output=bulk&format=json&created_after='+CreatedAfter+'&'+'created_before='+CreatedBefore+'&'+'sort_by=created_at&sort_order=ascending');
           req.setMethod( 'GET' );
           req.setBody( 'user_key='+userKey+'&'+'api_key='+apiKey);
           req.setHeader('Accept', 'application/json ');
           req.setHeader('Content-Type','application/json');
		   res = new Http().send( req );
	       System.debug('***Response****'+res.getBody());
           wrapper = (PardotProspectWrapper)JSON.deserialize(res.getBody(), PardotProspectWrapper.class);
          system.debug('******Wrapper*****'+wrapper);
        
            //this I tried to add to store the data in Pardot_Prospect__c 
			ppobjlst = new List<Pardot_Prospect__c>();
           Pardot_Prospect__c ppobj = new Pardot_Prospect__c();
			for (PardotProspectWrapper.prospect p : wrapper.result.prospect) {
                ppobj = new Pardot_Prospect__c();
                ppobj.Prospect_Id__c = string.valueOf(p.id);
                ppobj.First_name__c = p.first_name;
                ppobj.Last_Name__c = p.last_name;
                ppobj.Company__c = p.company;
                ppobj.Grade__c = p.grade;
                ppobj.Score__c = p.score;
				ppobjlst.add(ppobj);
			}
			insert ppobjlst;     
		}    
    
    }