Bulk Update from List view using flow and invocable apex


Selector class
public class QuerySelector {

    public static sobject GetRecord (string sql, id Val)
    {
        return database.query(sql + ' :val');
    }
    
     public static list<sobject> GetRecord (string sql)
    {
        return database.query(sql);
    }
    
    
    public static list<sobject> GetRecords(string sql, list<id> Val)
    {
        return database.query(sql + ' :val');
    }
    
    public static list<sobject> GetRecords(string sql, set<id> Val)
    {
        return database.query(sql + ' :val');
    }
    
    public static void UpdateRecords(list<sobject> UpdateList, boolean PartialUpdate)
    {
        list<database.SaveResult> Result=   database.update(UpdateList,PartialUpdate);
        
        for (database.SaveResult s : Result)
        {
            if (s.isSuccess())
            {
                system.debug('success');
            }
            else
            {
                for(database.Error e : s.geterrors())
                {
                    system.debug(e.getmessage());
                }
            }
            
        }
        
        
    }
    
    public static void InsertRecords(list<sobject> InsertList, boolean PartialUpdate)
    {
        list<database.SaveResult> Result=   database.Insert(InsertList,PartialUpdate);
        
        for (database.SaveResult s : Result)
        {
            if (s.isSuccess())
            {
                system.debug('success');
            }
            else
            {
                system.debug( s.geterrors());
                for(database.Error e : s.geterrors())
                {
                    system.debug(e.getmessage());
                }
            }
            
        }
        
        
    }
    
    
    
    
}



Invocable code
public class InvocableQueryUpdate {
    
    // Input values for class 
    public class InputValues {

        @InvocableVariable(required=true label='API Name of Object' )
        public String objectName;

        @InvocableVariable(required=true label='List of field Api Names for select query' )
        public List<String> fieldNames;

        @InvocableVariable(required=true label='List of ids for where clause only supports object ids' )
        public List<Id> ids;

        @InvocableVariable(required=true label='Values which needs to be updated' )
        public List<String> values;

        @InvocableVariable(required=true label='FieldNames which needs to be updated' )
        public List<String> fieldNamesToUpdate;
 
        
    }

    @InvocableMethod(label='UpdateRecordBasedOnIds')
    public static void BulkUpdateRecordList(List<InputValues> inputValue) {
        list<sobject> UpdateList = new list<sobject>();
        try {
            String objectName = inputValue.get(0).ObjectName;
            List<String> fieldNames = inputValue.get(0).fieldNames;
            List<Id> ids = inputValue.get(0).ids;
            List<String> values = inputValue.get(0).values;
            List<String> fieldNamesToUpdate = inputValue.get(0).fieldNamesToUpdate;
            string Soql = 'select ' +string.join(fieldNames, ',')+ ' from '+ objectName+ ' where id in ';
            list<sobject> ObjectList = QuerySelector.GetRecords(Soql,ids); 
            system.debug(Soql);
            system.debug(ObjectList);
            
            for(sobject so : ObjectList){
            Integer count = 0;
                for(String fldName : fieldNamesToUpdate)
                {
                string dataval =values[count] ;
                so.put(fldName, dataval);
                count++;
            }
                UpdateList.add(so);
            }
            database.update(UpdateList, false); 
            system.debug(UpdateList);
        }
        catch (exception ex) {
           system.debug(ex.getMessage());
        }

    }


    

}

Comments

Popular posts from this blog

The Salesforce Certification Frenzy: Balancing Quantity with Quality in a Changing Market