You need to sign in to do that
Don't have an account?
Daniel 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>
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.
Make sure that this statement is outside the for loop to avoid hitting the governer limits.
All Answers
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.
Make sure that this statement is outside the for loop to avoid hitting the governer limits.
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.
I have added a set and then used this set to check the duplicacy. I hope you can build on it. :)
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:
to be something like
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
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.