I just want to update one field in collection of records from one object.

I have wrapper class with sObject variable, like below

public Class ShipWrapper {
    public Boolean matchingRecordFound;
    public String Name;
    public Flow__c FlowInstance;
    public ShipmentFlexFlowWrapper(Shipment_Flexflow__c flowInstance, String name) {
        matchingComponentFound = false;
        Name = name;
        FlowInstance = flowInstance;

after the wrapper class i am having one method that processes the Flow__c object records and returns list of wrapper class records.

Now from another method am giving the processed wrapperclass parameters. Inside this method am checking with another object records for matching. If matches found, I need to update field by using wrapper class instance. I am pasting sample code below for better understanding.

public void somemethod(List < ShipWrapper > processedwrapper, anotherObject__c obj) {
        List < Flow__c > lst = new List < Flow__c > ();
        for (ShipWrapper wrap: processedwrapper) {
            for (anotherObj__c anotherObj: anotherlst) {
                if (wrap.FlowInstance.Name == anotherObj.Name) {
                    //here am updating record that matches
                    for (Flow__c objIns: lst) {
                        objIns.Name = 'Duplicate ' + anotherObj.UniqueCode;
            // **Doing update here but it is updating **same** message 
            // for all the records that satisfies the condition**
            update lst; 

1 Answer 1


Why do you have that for loop inside the if statement? That's what's causing your code to update every record with same name.

Try with this code below:

public void somemethod(List<ShipWrapper> processedwrapper, anotherObject__c obj){
   List<Flow__c> lst = new List<Flow__c>();
   for(ShipWrapper wrap : processedwrapper){
     for(anotherObj__c anotherObj : anotherlst){
        if(wrap.FlowInstance.Name == anotherObj.Name){
           //here am updating record that matches          
           wrap.FlowInstance.Name = 'Duplicate '+anotherObj.UniqueCode;
   update lst; // **Doing update here but it is updating **same** message for all the records that satisfies the condition**
  • thanks very much for this. It works. I was tried without loop but it gave duplicate error. But now working fine
    – sfdcdev
    Commented Jun 1, 2016 at 11:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .