+ Start a Discussion
Daniel B ProbertDaniel B Probert 

http call out working but xml import doesn't seem to work.

hi all,

 

i've been triyng to pull data from a remote service and have managed to get connected and can see in the logs that my child items are being read - my only issue is that the new records that I want creating aren't being created.

 

here is my code can anyone see what this isn't created montioring forms - the apex is triggered when we create a new form, basically it will then go off and check the remote service for any new forms on the server.

 

cheers

dan

 

public class MonitoringFormsUpdater {
  //Future annotation to mark the method as async.
  @Future(callout=true)
  public static void updateMF() {
    //construct an HTTP request
    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://magpi.com/api/surveys?username=myusername&accesstoken=myaccesstoken');
    req.setMethod('POST'); 
    //send the request
     HTTPResponse res = http.send(req);
     System.debug(res.getBody());
    // list object ready for insert 
    List<Monitoring_Form__c> newforms = new List<Monitoring_Form__c> ();
    string s = res.getBody();
    Dom.Document docx = new Dom.Document();
        docx.load(s);
        dom.XmlNode xroot = docx.getrootelement();
        dom.XmlNode [] xrec = xroot.getchildelements(); //Get all Record Elements
        for(Dom.XMLNode child : xrec) //Loop Through Records
            {
            Monitoring_Form__c mf = new Monitoring_Form__c();
        for(dom.XmlNode magpi : child.getchildren() ) {
           if (magpi.getname() == 'SurveyId') {
                   mf.form_id__c = magpi.gettext();
             }
           if (magpi.getname() == 'SurveyName') {
                   mf.name = magpi.gettext();
             }  
         }
        newforms.add(mf);
        }
  }
}

 the remote service returns this xml file.

 

<Surveys>
<Survey>
<SurveyId>7471</SurveyId>
<SurveyName>Bole_ASD_JHS</SurveyName>
<Owner>episurveyor@camfed.org</Owner>
</Survey>
<Survey>
<SurveyId>7450</SurveyId>
<SurveyName>Bole_ASD_Pri</SurveyName>
<Owner>episurveyor@camfed.org</Owner>
</Survey>
<Survey>
<SurveyId>7493</SurveyId>
<SurveyName>Bole_ASD_SHS</SurveyName>
<Owner>episurveyor@camfed.org</Owner>
</Survey>
<Survey>
<SurveyId>7474</SurveyId>
<SurveyName>Bongo_ASD_JHS</SurveyName>
<Owner>episurveyor@camfed.org</Owner>
</Survey>
<Survey>
<SurveyId>7494</SurveyId>
<SurveyName>Bongo_ASD_SHS</SurveyName>
<Owner>episurveyor@camfed.org</Owner>
</Survey>
</Surveys>
Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

THe problem is you're not inserting the records, i.e., there is no DML statements.

 

So to insert the records add a line after you have added all the objects in the newForms list.

 

insert newforms;

 Make sure that this statement is outside the for loop to avoid hitting the governer limits.

All Answers

jungleeejungleee

THe problem is you're not inserting the records, i.e., there is no DML statements.

 

So to insert the records add a line after you have added all the objects in the newForms list.

 

insert newforms;

 Make sure that this statement is outside the for loop to avoid hitting the governer limits.

This was selected as the best answer
Daniel B ProbertDaniel B Probert
i checked and double checked as well can't believe i missed something so simple.

how would i bring in an if exist statement within this.

i.e. if the surveyid already exists skip to the next.

i'm guessing i need to include a map of the current form_id__c field and the id of the record so:

Map<ID, Monitoring_Form__c> m = new Map<ID, Monitoring_Form__c>([SELECT Id, Form_id__c FROM Monitoring_Form__c]);

this in theory should be a complete list of all ids, form_id__c from within the monitoring form object.

now how would i include an if form_id__c = surveyid don't insert statement.

i thought it should be an if statement around my newforms.add(); but that gives me an error:


Error: Compile Error: Initial term of field expression must be a concrete SObject: MAP<Id,Monitoring_Form__c> at line 30 column 30

again i'm sure this is just something i've got the wrong way around.
jungleeejungleee

I have added a set and then used this set to check the duplicacy. I hope you can build on it. :)

 

 List<Monitoring_Form__c> newforms = new List<Monitoring_Form__c> ();
 set<id> existingmFormSetId = new set<id>();
 //iterate thru the existing mforms and add allthe form_id__c in a set. And then use this set to later check for duplicates
 for(monitoring_form__c mform: [SELECT Id, Form_id__c FROM Monitoring_Form__c]){
	existingmFormSetId.add(Form_id__c);
 }
    string s = res.getBody();
    Dom.Document docx = new Dom.Document();
        docx.load(s);
        dom.XmlNode xroot = docx.getrootelement();
        dom.XmlNode [] xrec = xroot.getchildelements(); //Get all Record Elements
        for(Dom.XMLNode child : xrec) //Loop Through Records
            {
            Monitoring_Form__c mf = new Monitoring_Form__c();
			for(dom.XmlNode magpi : child.getchildren() ) {
			   if (magpi.getname() == 'SurveyId') {
					 mf.form_id__c = magpi.gettext();
				 }
			   if (magpi.getname() == 'SurveyName') {
					   mf.name = magpi.gettext();
				}
			}
			if(!existingmFormSetId.contains(mf.form_id__c)){
				newforms.add(mf);
			}
        }
  }

 

 

Daniel B ProbertDaniel B Probert

hi, thanks for the quick response. i think i've got it working but am now getting an error:

 

First error: Invalid id: 7471

 

This is the first survey form id that is already in salesforce - am i right that to get round this I need to edit:

 

 

if(!existingmFormSetId.contains(mf.form_id__c)){
				newforms.add(mf);

 

to be something like

 

if(!existingmFormSetId.contains(mf.form_id__c)){}
else{
				newforms.add(mf);}

 which should be saying if it's there do nothing if it's not there add it and then insert it?

 

or is there a better way of doing this.

 

basically i need it to say

 

1. ok it's there ignore

2. this one is new insert

 

thanks for your help this is helping me to understand this.

 

cheers

dan

 

 

 

Daniel B ProbertDaniel B Probert

hurrah i've figured out why i was getting the error sorted now i had to change the set<id> to set<string>

 

as the form_id__c is a string not a salesforce id.

 

final code looks like this and seems to be working well.

 

 List<Monitoring_Form__c> newforms = new List<Monitoring_Form__c> ();
 set<string> existingmFormSetId = new set<string>();
 //iterate thru the existing mforms and add allthe form_id__c in a set. And then use this set to later check for duplicates
 for(monitoring_form__c mform: [SELECT Id, Form_id__c FROM Monitoring_Form__c]){
	existingmFormSetId.add(Form_id__c);
 }
    string s = res.getBody();
    Dom.Document docx = new Dom.Document();
        docx.load(s);
        dom.XmlNode xroot = docx.getrootelement();
        dom.XmlNode [] xrec = xroot.getchildelements(); //Get all Record Elements
        for(Dom.XMLNode child : xrec) //Loop Through Records
            {
            Monitoring_Form__c mf = new Monitoring_Form__c();
			for(dom.XmlNode magpi : child.getchildren() ) {
			   if (magpi.getname() == 'SurveyId') {
					 mf.form_id__c = magpi.gettext();
				 }
			   if (magpi.getname() == 'SurveyName') {
					   mf.name = magpi.gettext();
				}
			}
			if(!existingmFormSetId.contains(mf.form_id__c)){
				newforms.add(mf);
			}
        }
  }