+ Start a Discussion
Data Migration 5Data Migration 5 

Submitting multiple records using custom button

Hi, I need to create a custom buton (Detail Page Button/OnClick Java script) on opportunity page. When user clicks on custom button, all the opportunity records which have parent has current opportunity has to be submitted for approval. (I have cretaed a custom opportunity lookup field parent_opportunity__c on opportunity object). I have developed below java script to submit multiple child records to approval process. I see that only last record in the loop is getting submitted and the rest of the records are not submitted for approval process. what is missing here? What fix is required in below code.
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")} 

var result3 = sforce.connection.query("Select id,name From Opportunity o WHERE o.Parent_Opportunity__c = '{!Opportunity.Id}'");

var newOpp3 = result3.getArray("records");

for (var i=0; i<newOpp3.length; i++) {
  var temp = newOpp3[i];
  var opptyId = temp.Id;
  window.location.href = "/p/process/Submit?retURL=/" + opptyId + "&id=" +      
  opptyId;
  alert(temp.Name + " -- "  + temp.Id + opptyId );
  }

 
Best Answer chosen by Data Migration 5
SKolakanSKolakan
Unless someone have a better recommendation, I would create a web service method to pass current opportunity Id and query related opportunities (like you did) and pass it off to a method like below to bulk submit for approval
 
global class AutoSubmit{
webService static String processAutoSubmission(String oppId)
//query related opps here
submitForApproval(oppIds);
}
}


public static void submitForApproval(Set<string> recordIds){ 
    List<Approval.ProcessSubmitRequest> requests = new List<Approval.ProcessSubmitRequest> ();
    
    for(String recId: recordIds){ 
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest(); 
        req1.setComments('Auto Submit'); 
        req1.setObjectId(recId); 
        requests.add(req1); 
    } 
    Approval.ProcessResult[] processResults = Approval.process(requests); 
}

You can call it form the java script like 
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
var message = sforce.apex.execute("AutoSubmit","processAutoSubmission",{oppId:"{!Opportunity.Id}"});

All Answers

SKolakanSKolakan
Unless someone have a better recommendation, I would create a web service method to pass current opportunity Id and query related opportunities (like you did) and pass it off to a method like below to bulk submit for approval
 
global class AutoSubmit{
webService static String processAutoSubmission(String oppId)
//query related opps here
submitForApproval(oppIds);
}
}


public static void submitForApproval(Set<string> recordIds){ 
    List<Approval.ProcessSubmitRequest> requests = new List<Approval.ProcessSubmitRequest> ();
    
    for(String recId: recordIds){ 
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest(); 
        req1.setComments('Auto Submit'); 
        req1.setObjectId(recId); 
        requests.add(req1); 
    } 
    Approval.ProcessResult[] processResults = Approval.process(requests); 
}

You can call it form the java script like 
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
var message = sforce.apex.execute("AutoSubmit","processAutoSubmission",{oppId:"{!Opportunity.Id}"});
This was selected as the best answer
Data Migration 5Data Migration 5
I'm getting syntax errors while executing java script (specially near {oppId:"{!Opportunity.Id}"} ). what is the exact syntax?
SKolakanSKolakan
You can refer: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_and_ajax.htm
 
Data Migration 5Data Migration 5
Any sample code on this will be highly helpful. TIA
SKolakanSKolakan
You are correct. oppId is the input variable name of your method and Opportunity.Id refer to standard syntax to retrieve current opportunity Id. There may be an extra space or something some where. If you still could not figure out, please post your webservice method and your javascript code.
SKolakanSKolakan
Data Migration 5,
Couple of things
1. You are using wrong javascript code. Just use the javascript code that I mentioned in my first answer. 
2. Check the security for your class (Open class from Apex classes and click on security and add all profiles that need access to the class)
3. I hope you modified your processAutoSubmission method to include query, return message, error handling etc.
Data Migration 5Data Migration 5
Thanks Sreeni for your inputs.
I couldnt figure out the exact problem in my original script. But your suggested code works.