You need to sign in to do that
Don't have an account?
Another parsing XML Query
Hi,
I'm doing a HTTP Callout from a trigger on a specific object. The trigger basically runs when a record is updated and checks online for related records.
I thought I had worked it out but I'm getting an error of:
System.ListException: Before Insert or Upsert list must not have two identically equal elements
here is my code basically it should go off to an external site - then grab the xml response - then go through an create a new record for each child element based on my if statements and also fill in the a lookup field with the record id the the trigger was initiated from.
i think it's because of how i'm grabbing the recordid and formid at the beginning but am not sure.
public class MonitoringQuestionsUpdater { @Future(callout=true) public static void updateMQ() { List<Monitoring_Form__c> mfcs = [SELECT Id, Form_Id__c FROM Monitoring_Form__c]; for(Monitoring_Form__c a:mfcs){ string formid = a.form_id__c; string recordid = a.id; Http http = new Http(); HttpRequest req = new HttpRequest(); req.setEndpoint('https://magpi.com/api/questions?username=myusername&accesstoken=mytoken&surveyid='+formid); req.setMethod('POST'); HTTPResponse res = http.send(req); System.debug(res.getBody()); List<Monitoring_Questions__c> newquestions = new List<Monitoring_Questions__c>(); set<string> existingmQuestSetId = 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_questions__c mquest: [SELECT Id, questionid__c FROM Monitoring_Questions__c]){ existingmQuestSetId.add(mquest.questionid__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) { for(dom.XmlNode magpi : child.getchildren() ) { Monitoring_Questions__c mq = new Monitoring_Questions__c(); if (magpi.getname() == 'Id') { System.debug(magpi.getname() == 'Id'); mq.QuestionID__c = magpi.gettext(); } if (magpi.getname() == 'Prompt') { System.debug(magpi.getname() == 'Prompt'); mq.Question_Prompt__c = magpi.gettext(); } if (magpi.getname() == 'Name'){ System.debug(magpi.getname() == 'Name'); mq.Question_Name__c = magpi.gettext(); } mq.monitoring_form__c = recordid; if(!existingmQuestSetId.contains(mq.questionid__c)){ Newquestions.add(mq); } } } insert newquestions; } } }
any help is appreciated.
dan
Try to initialize the list "Newquestions" inside the " for loop " and see if it works.
Thanks,
Prashant Tiwari
mark it as solved if the solution worked as it might help others
All Answers
change
if(!existingmQuestSetId.contains(mq.questionid__c)){
to
if(!existingmQuestSetId.containsIgnoreCase(mq.questionid__c)){
and see if it helps.. because abc and ABC are treated as different values..
Try to initialize the list "Newquestions" inside the " for loop " and see if it works.
Thanks,
Prashant Tiwari
mark it as solved if the solution worked as it might help others
i.e once with the questionid, one with the question name and then one with the prompt.
ideally this should be one single record that is being created, is this a loop thing again?
cheers for your help.
i have this running from a trigger after update - how do i can the record from the trigger into my apex class - i thought that part would be simple but cna't figure it out.
trigger=
at the top of my class i have:
how can i include a where id =: trigger record id
i was looking at the trigger map key but couldn't get that working so thought maybe i just need a different approach?
yes so my trigger ended up looking like this:
and then i updated my future class to this:
so now when ever i update a form it will check to see if all the questions are in salesforce :)
cheers for your assistance i got there in the end.