You need to sign in to do that
Don't have an account?
Mick Man
Trigger
I want to know where is the problem for this trigger
thank you in advance for your help
trigger updateParentQuantity on EntreeSortie__c (after insert, after update) { Set<Id> MatPremIds= new Set<Id>(); for (EntreeSortie__c ES: Trigger.new) { MatPremIds.add(ES.MatierePremiere__c); } MatPremIds.remove(null); if (!MatPremIds.isEmpty()) { Map<Id, Decimal> totalMap = new Map<Id, Decimal>(); for (Id id: MatPremIds) { totalMap.put(id, 0); } for (AggregateResult ar : [select SUM(Quantite__c) tot, MatierePremiere__c, Type__c from EntreeSortie__c where MatierePremiere__c = :MatPremIds]) { Id MatPremId = ar.get('MatierePremiere__c'); if (ar.get('Type__c') == 'Entrée') { totalMap.put(MatPremId, totalMap.get(MatPremId) + ar.get('tot')); } else if (ar.get('Type__c') == 'Sortie') { totalMap.put(MatPremId, totalMap.get(MatPremId) - ar.get('tot')); } } List<MatierePremiere__c> MatToUpdate = new List<MatierePremiere__c>(); for (Id id: totalMap.keySet()) { MatToUpdate.add(new MatierePremiere__c(Id = id,Nombre_au_Stock__c = totalMap.get(id))); } update MatToUpdate; } }
thank you in advance for your help
Try replacing the following
for (AggregateResult ar : [select SUM(Quantite__c) tot, MatierePremiere__c, Type__c from EntreeSortie__c where MatierePremiere__c IN :MatPremIds])
with
for (AggregateResult ar : [select SUM(Quantite__c) tot, MatierePremiere__c, Type__c from EntreeSortie__c where MatierePremiere__c IN :MatPremIds Group By MatierePremiere__c,Type__c ])
And
if (ar.get('Type__c') == 'Entrée') with if ((String)ar.get('Type__c') == 'Entrée')
else if (ar.get('Type__c') == 'Sortie') with else if ((String)ar.get('Type__c') == 'Sortie')
Regards,
Bhanu Mahesh
All Answers
Try changing this line
for (AggregateResult ar : [select SUM(Quantite__c) tot, MatierePremiere__c, Type__c from EntreeSortie__c where MatierePremiere__c = :MatPremIds])
To
for (AggregateResult ar : [select SUM(Quantite__c) tot, MatierePremiere__c, Type__c from EntreeSortie__c where MatierePremiere__c IN :MatPremIds])
Regards,
Bhanu Mahesh
Id MatPremId = ar.MatierePremiere__c;
else change your code to make it id.
Try this
Shaijan
Id MatPremId = ar.get('MatierePremiere__c');
with
Id MatPremId = (Id) ar.get('MatierePremiere__c');
You are getting a reference of the Object, you need to typecast this to Id.
Error: Erreur de compilation : Arithmetic expressions must use numeric arguments à la ligne 26 colonne 57
Can you paste the new code. The above one you might have changed
Shaijan
and here is the error message
Error: Erreur de compilation : Arithmetic expressions must use numeric arguments à la ligne 26 colonne 57
totalMap.put(MatPremId, (Decimal.valueof(totalMap.get(MatPremId)) - Decimal.valueof(ar.get('tot')))); Line 28
try this
Shaijan
Error: Erreur de compilation : Variable does not exist: Decimal à la ligne 27 colonne 58
totalMap.put(MatPremId, (totalMap.get(MatPremId) + (Decimal)(ar.get('tot'))));
totalMap.put(MatPremId, (totalMap.get(MatPremId) - (Decimal)(ar.get('tot'))));
Regards,
Bhanu Mahesh
Try replacing the following
for (AggregateResult ar : [select SUM(Quantite__c) tot, MatierePremiere__c, Type__c from EntreeSortie__c where MatierePremiere__c IN :MatPremIds])
with
for (AggregateResult ar : [select SUM(Quantite__c) tot, MatierePremiere__c, Type__c from EntreeSortie__c where MatierePremiere__c IN :MatPremIds Group By MatierePremiere__c,Type__c ])
And
if (ar.get('Type__c') == 'Entrée') with if ((String)ar.get('Type__c') == 'Entrée')
else if (ar.get('Type__c') == 'Sortie') with else if ((String)ar.get('Type__c') == 'Sortie')
Regards,
Bhanu Mahesh