0

I have created REST which fetching the data from pardot and displaying it on the vf page. It is working fine. But I need to store this data in the custom object's corresponding fields on the vf page. As of now I bind the wrapper class on the page.

Suppose, I have FirstName and LastName fields in the wrapper class which I need to store in the First_Name__c and Last_Name__c fields on the custom object from wrapper class.

Here is my REST class:

{{
       wrapper = (PardotProspectWrapper)JSON.deserialize(res.getBody(), PardotProspectWrapper.class);
      system.debug('******Wrapper*****'+wrapper); // getting response here

        //iterating wrapper response and adding to Pardot_Prospect__c object
        List<Pardot_Prospect__c> csbs = new List<Pardot_Prospect__c>();
        for (PardotProspectWrapper w : wrapper) { // Error - Loop must iterate over collection: PardotProspectWrapper
            csbs.add(w.csb);
        }
        insert csbs;      
    }    



//wrapper created for custom object Pardot_Prospect__c
public class PardotProspectWrapper{
public Pardot_Prospect__c csb {get;set;}
public result result{get;set;}
public class result{
    public list<prospect> prospect{get;set;}
}
public class prospect{
    public String first_name{get;set;}
    public String last_name{get;set;}

}
 }

Can somebody please tell me how can I achieve it. it would be great help.

Thank you in advance.

0

Your wrapper has a confusing structure:

public class PardotProspectWrapper {

    public class result {
        public list<prospect> prospect{get;set;}
    }

    public class prospect {
        public String first_name {get;set;}
        public String last_name {get;set;}

    }

    public Pardot_Prospect__c csb {get;set;}
    public result result {get;set;}
}

because it has a property csb that is a single record yet a property result that can represent many records. Also just a list of prospect objects would be enough to represent the data (or indeed a list of Pardot_Prospect__c).

However, using the current wrapper classes this should work:

List<Pardot_Prospect__c> csbs = new List<Pardot_Prospect__c>();
for (PardotProspectWrapper.prospect p : wrapper.result) {
    csbs.add(new Pardot_Prospect__c(
        First_Name__c = p.first_name,
        Last_Name__c = p.last_name
    ));
}
insert csbs;  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.