function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kanth kKanth k 

HOW TO UPLOAD XML FILE USING BATCH CLASS IN SALESFORCE?

Hi, 
 Can any body help me that how to upload .XML file using batch class to insert account record?
I have written code and excetute it in developer console and getting error that is "You've exceeded the limit of 100 jobs in the flex queue for org 00D2E000001G0Xe. Wait for some of your batch jobs to finish before adding more. To monitor and reorder jobs, use the Apex Flex Queue page in Setup."

Code in developer console :
contact cnt =[select id,name from contact where name='Dummy'];
attachment att =[select id,name,body,parentid from attachment where parentid=:cnt.id];

Product_XML_ParserDemo.makeAccount(att);

 

global class Product_XML_ParserDemo
{   
    public class wrap
    {
        public String accname{get; set;}
        public String accnumb{get; set;}
        public String accphone{get; set;}
        public String accwebsite{get; set;}
        public String accbstrt{get; set;}
        public String accbcity{get; set;}
        public String accbste{get; set;}
        public String accbpcodde{get; set;}
        public String accbcntry{get; set;}
    }
   webservice static void makeAccount(attachment att) {     
        DOM.Document doc = new DOM.Document();
        blob myfile = att.body;
        doc.load(String.valueOf(myfile.toString()));    
        Product_XML_ParserDemo pxmlprs = new Product_XML_ParserDemo();
        pxmlprs.parseproducts(doc.getRootElement());
        
    }
 public List<wrap> wrplist{get;set;}
 private void parseproducts(DOM.XMLNode node) 
    {
        node=node.getChildElement('list',null);
        for (Dom.XMLNode child : node.getChildElements()) 
        {
            
            if(child.getName() == 'Account')
            { 
                System.debug('child'+child);
                parseReport(child);
            }
        }
    }
    private void parseReport(DOM.XMLNode node ) 
    {
        wrap r = new wrap();
        wrplist = new List<wrap>(); 
        for (Dom.XMLNode child : node.getChildElements()) 
        {
            if (child.getName() == 'Name') 
            {
                r.accname = child.getText().trim();
            } 
            else if (child.getName() == 'AccountNumber') 
            {
                r.accnumb= child.getText().trim();
            } 
            else if (child.getName() == 'Phone') 
            {
                r.accphone= child.getText().trim();
            }  
            else if (child.getName() == 'Website') 
            {
                r.accwebsite= child.getText().trim();
            }
            else if (child.getName() == 'Billingstreet') 
            {
                r.accbstrt= child.getText().trim();
            }
            else if (child.getName() == 'BillingCity') 
            {
                r.accbcity= child.getText().trim();
            }
            else if (child.getName() == 'BillingState') 
            {
                r.accbste= child.getText().trim();
            }
            else if (child.getName() == 'BillingPostalCode') 
            {
                r.accbpcodde= child.getText().trim();
            }
            else if (child.getName() == 'BillingCountry') 
            {
                r.accbcntry= child.getText().trim();
            }
        }
        wrplist.add(r);
        list<account> prodlist=new list<account>();
        for(wrap w: wrplist)
        {
            
            account acc=new account();
            
                acc.Name = w.accname;
                acc.AccountNumber = w.accnumb;
                acc.Phone = w.accphone;
                acc.Website = w.accwebsite;
                acc.Billingstreet = w.accbstrt;
                acc.BillingCity = w.accbcity;
                acc.BillingState = w.accbste;
                acc.BillingPostalCode = w.accbpcodde;
                acc.BillingCountry = w.accbcntry;
                
                
               
                prodlist.add(acc);
          
           }
            batchTestNew dbbbt = new batchTestNew(prodlist);
           Id batchJobId= database.executeBatch(dbbbt,50);
}
____________________________________________
Batch Class
global class batchTestNew implements Database.Batchable<sObject>{
public List <account> accountsToParseSet = new List<account>();

   global batchTestNew(List<account> accountsToParseGet){
   accountsToParseSet = accountsToParseGet;          
   }

   global List<SObject> start(Database.BatchableContext BC){
      return accountsToParseSet;
   }

   global void execute(Database.BatchableContext BC, 
                       List<sObject> scope){
      for(sObject s : scope){
           upsert accountsToParseSet;
                  }      
      INSERT scope;
   }

   global void finish(Database.BatchableContext BC){

   }

}

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Kanth,

Greetings to you!

With the Apex flex queue, you can submit up to 100 batch jobs.
The outcome of Database.executeBatch is as follows.
  • The batch job is placed in the Apex flex queue, and its status is set to Holding.
  • If the Apex flex queue has the maximum number of 100 jobs, Database.executeBatch throws a LimitException and doesn’t add the job to the queue.

If your org doesn’t have Apex flex queue enabled, Database.executeBatch adds the batch job to the batch job queue with the Queued status. If the concurrent limit of queued or active batch job has been reached, a LimitException is thrown, and the job isn’t queued.

Related Salesforce docs:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

https://help.salesforce.com/articleView?id=code_apex_flex_queue.htm&type=5

Please refer to the below links which might help you further with the above issue.

https://salesforce.stackexchange.com/questions/207445/new-apex-batch-jobs-always-end-up-in-the-flex-queue-in-holding

https://salesforce.stackexchange.com/questions/84434/explain-salesforce-flexqueue-limit

https://developer.salesforce.com/forums/?id=9060G000000I4EzQAK

https://salesforce.stackexchange.com/questions/136688/how-to-check-to-avoid-system-asyncexception-youve-exceeded-the-limit-of-100-j

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Kanth kKanth k
Hi khan Anas,

          can you confirm once that what i am written code is correct or not? or can you send sample code for upload xml file and process it to insert record using bacth class.