function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mick ManMick Man 

Trigger

I want to know where is the problem for this trigger
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;
        }
    }

User-added image
thank you in advance for your help
Best Answer chosen by Mick Man
Bhanu MaheshBhanu Mahesh
Hi,

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

Sagar PareekSagar Pareek
At line number 3 you have declared       Set<Id> MatPremIds= new Set<Id>(); and at line number 22 you have again used MatPremIds as Id variable, trychanging names as similar names might creating errors for you!
Bhanu MaheshBhanu Mahesh
Hi Micky,

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
Shaijan ThomasShaijan Thomas
MatierePremiere__c - Hope this is an id. 
Id MatPremId = ar.MatierePremiere__c;
else change your code to make it id.
Try this
Shaijan
Salesforce DeveloperSalesforce Developer
Replace 22 line 
 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. 
Mick ManMick Man
I think the previous one problem is solved, but there is still the other problem I can't solve  please

Error: Erreur de compilation : Arithmetic expressions must use numeric arguments à la ligne 26 colonne 57

 
Shaijan ThomasShaijan Thomas
How did you solve the Issue, we would like the understand the correct solution
Can you paste the new code. The above one you might have changed
Shaijan
Mick ManMick Man
thank for your answer Shaijan Thomas 
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 IN :MatPremIds])    
                //for (AggregateResult ar : [select SUM(Quantite__c) tot, MatierePremiere__c, Type__c from EntreeSortie__c where MatierePremiere__c = :MatPremIds])
                    {   
                        Id MatPremId =  (Id)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;
        }
    }

and here is the error message

Error: Erreur de compilation : Arithmetic expressions must use numeric arguments à la ligne 26 colonne 57

 
Shaijan ThomasShaijan Thomas
totalMap.put(MatPremId, (Decimal.valueof(totalMap.get(MatPremId)) + Decimal.valueof(ar.get('tot')))); Line 26
totalMap.put(MatPremId, (Decimal.valueof(totalMap.get(MatPremId)) - Decimal.valueof(ar.get('tot')))); Line 28
try this
Shaijan
Mick ManMick Man
but if I change the line 26 and 28 there to other error

Error: Erreur de compilation : Variable does not exist: Decimal à la ligne 27 colonne 58

 
Bhanu MaheshBhanu Mahesh
Try this

totalMap.put(MatPremId, (totalMap.get(MatPremId) + (Decimal)(ar.get('tot'))));
totalMap.put(MatPremId, (totalMap.get(MatPremId) - (Decimal)(ar.get('tot'))));

Regards,
Bhanu Mahesh
 
Mick ManMick Man
I still have a problem, please help me,
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 IN :MatPremIds])    
                //for (AggregateResult ar : [select SUM(Quantite__c) tot, MatierePremiere__c, Type__c from EntreeSortie__c where MatierePremiere__c = :MatPremIds])
                    {   
                        Id MatPremId =  (Id)ar.get('MatierePremiere__c');
                        if (ar.get('Type__c') == 'Entrée') 
                            {
                                //totalMap.put(MatPremId, totalMap.get(MatPremId) + ar.get('tot'));
                                totalMap.put(MatPremId, (totalMap.get(MatPremId) + (Decimal)(ar.get('tot'))));
                            } 
                         else if (ar.get('Type__c') == 'Sortie') 
                            {
                                //totalMap.put(MatPremId, totalMap.get(MatPremId) - ar.get('tot'));
                                totalMap.put(MatPremId, (totalMap.get(MatPremId) - (Decimal)(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;
        }
    }

User-added image
Bhanu MaheshBhanu Mahesh
Hi,

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
This was selected as the best answer