[SalesForce] Get Record from wrapper class

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
                    lst.add(wrap.FlowInstance);
                    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; 
        }

Best Answer

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;
           lst.add(wrap.FlowInstance);
        }
     }
   update lst; // **Doing update here but it is updating **same** message for all the records that satisfies the condition**
}
Related Topic