You need to sign in to do that
Don't have an account?
i need to add two aggregate results with 1 value each into my for loop; but i need both ventasSemanales and ventasMensuales to run just one time per user and add those values into Posiciones Map. Any ideas how can i achieve that??
Can i create another map and fill with the results of those AggregateResults and the update the map posiciones with those values i need to add those two values per user to the other values im getting in posiciones MAP. Any ideas i have been stuck all day with this
public class tablaVentas {
public list <Leads__c> puntos = [select ownerid, Puntos__c, Estado__c from leads__c where ownerid IN (select id from user) order by Puntos__c DESC];
public list <User> users= [select name from user];
//I need to get this values per user and insert them into posiociones Map
public AggregateResult[] ventasSemanales = [Select COUNT(Estado__c)Ventas from Leads__c where Estado__c = 'Vendido' and Fecha_Venta__c = THIS_WEEK group by owner.name ];
public AggregateResult[] = [Select COUNT(Estado__c)Ventas from Leads__c where Estado__c = 'Vendido' and Fecha_Venta__c = THIS_MONTH group by owner.name ];
public List<Posicion> getPositions(){
Map<Id,User> user_map = new Map<Id,User>();
for( User u : users){
user_map.put(u.id,u);
}
Map<Id, Posicion> posiciones = new Map<Id, Posicion>();
for(leads__c c : puntos){
Posicion temp = posiciones.get(c.ownerId);
if(posiciones.get(c.ownerId)==null){ temp = new Posicion() ;}
Integer puntos = integer.valueof(c.puntos__c);
temp.nombre = user_map.get(c.ownerid).name;
temp.puntostotal += puntos;
/*
if (c.Estado__c == 'vendido'){
// i need to insert those values here im pretty new to apex what can i do ??
if (ventasSemanales == null)
temp.VentasSemanales = (integer)ventasSemanales[0].get('Ventas');
else {
temp.VentasSemanales = 0;
}
if (ventasMensuales == null){
temp.VentasMensuales = 0;
}
else{
temp.VentasMensuales = (integer)ventasMensuales[0].get('Ventas');
}
}
*/
posiciones.put(c.ownerid,temp);
}
return posiciones.values();
}
public class tablaVentas {
public list <Leads__c> puntos = [select ownerid, Puntos__c, Estado__c from leads__c where ownerid IN (select id from user) order by Puntos__c DESC];
public list <User> users= [select name from user];
//I need to get this values per user and insert them into posiociones Map
public AggregateResult[] ventasSemanales = [Select COUNT(Estado__c)Ventas from Leads__c where Estado__c = 'Vendido' and Fecha_Venta__c = THIS_WEEK group by owner.name ];
public AggregateResult[] = [Select COUNT(Estado__c)Ventas from Leads__c where Estado__c = 'Vendido' and Fecha_Venta__c = THIS_MONTH group by owner.name ];
public List<Posicion> getPositions(){
Map<Id,User> user_map = new Map<Id,User>();
for( User u : users){
user_map.put(u.id,u);
}
Map<Id, Posicion> posiciones = new Map<Id, Posicion>();
for(leads__c c : puntos){
Posicion temp = posiciones.get(c.ownerId);
if(posiciones.get(c.ownerId)==null){ temp = new Posicion() ;}
Integer puntos = integer.valueof(c.puntos__c);
temp.nombre = user_map.get(c.ownerid).name;
temp.puntostotal += puntos;
/*
if (c.Estado__c == 'vendido'){
// i need to insert those values here im pretty new to apex what can i do ??
if (ventasSemanales == null)
temp.VentasSemanales = (integer)ventasSemanales[0].get('Ventas');
else {
temp.VentasSemanales = 0;
}
if (ventasMensuales == null){
temp.VentasMensuales = 0;
}
else{
temp.VentasMensuales = (integer)ventasMensuales[0].get('Ventas');
}
}
*/
posiciones.put(c.ownerid,temp);
}
return posiciones.values();
}
Instead of grouping by owner.name you should use group by ownerId as it is unique and use owner id as map key.
AggregateResult[] ventasSemanales = [Select COUNT(Estado__c)Ventas, ownerId ownerId from Leads__c where Estado__c = 'Vendido' and Fecha_Venta__c = THIS_WEEK group by ownerId];
Map<String,AggregateResult> ventasSemanalesUserResult = new Map<String,AggregateResult>(); // This contains key as user id and value as aggregate result
for(AggregateResult res: ventasSemanales){
ventasSemanalesUserResult.put(res.get('ownerId')+'', res);
}
using ventasSemanalesUserResult.get('[USER_ID]') you will get aggregate result for that user.
All Answers
Instead of grouping by owner.name you should use group by ownerId as it is unique and use owner id as map key.
AggregateResult[] ventasSemanales = [Select COUNT(Estado__c)Ventas, ownerId ownerId from Leads__c where Estado__c = 'Vendido' and Fecha_Venta__c = THIS_WEEK group by ownerId];
Map<String,AggregateResult> ventasSemanalesUserResult = new Map<String,AggregateResult>(); // This contains key as user id and value as aggregate result
for(AggregateResult res: ventasSemanales){
ventasSemanalesUserResult.put(res.get('ownerId')+'', res);
}
using ventasSemanalesUserResult.get('[USER_ID]') you will get aggregate result for that user.