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

who can help me to create a trigger for the custom object Entree/Sortier that the custom object MatierPremiere be updated each time that there is an input or an output
that is to say, if one selects entrée (input), the number of stores of raw material object is updated (+) and if one selects sortie (output), the number of stock of raw material object is updated (- )

User-added image
pconpcon
You could do this as two roll up summaries on the MatierPremiere objects.  One that sums all Entree and one that sums all Sortier.  Then have a formula field that subtracts the two.  If this doesn't work, you'd have to do a trigger that does an aggregate query and adds up all the Entree and Sortier fields groups by the MatierPremiere.
Vishnu VaishnavVishnu Vaishnav
Hi Micky,

Your code is here bud  ::

trigger EntreeAndSortier on MatierPremiere__c (before update) {    
    for( MatierPremiere__c mp : trigger.new ){
        if( mp.type__c == 'entrée' ){
            mp.quantity__c  = Trigger.oldMap.get(opp.ID).quantity__c + 1;
        }
        else if( mp.type__c == 'sortie' ){
            mp.quantity__c  = Trigger.oldMap.get(opp.ID).quantity__c 1 1;
        }        
    }
}

I dont know field and object api names so please verify before use this code..

:::======================================================================:::
Qusetion Solved ? then mark as best answer to make helpful to others .....
pconpcon
If you have to write this as a trigger, this would be a better solution.  Vishnu's answer does not actually update the parent object, it only updates the object, and does not sum up all instances of the child object.

You will need to substitute the correct object and field names in for ChildObject__c, ParentObject__c, Type__c, Quantity__c, MatierPremiere__c also, you will need ot make sure that the if Type__c is stored with the e-acute.  Might be worth also doing a toLowerCase on the Type__c field and making your if statements use a lowercase version of entree/sortie
 
trigger updateParentQuantity on ChildObject__c (after insert, after update) {
    Set<Id> parentIds = new Set<Id>();

    for (ChildObject__c child: Trigger.new) {
        parentIds.add(child.MatierPremiere__c);
    }

    parentIds.remove(null);

    if (!parentIds.isEmpty()) {
        Map<Id, Decimal> totalMap = new Map<Id, Decimal>();
        for (Id id: parentIds) {
            totalMap.put(id, 0);
        }
        
        for (AggregateResult ar : [
            select SUM(Quantity__c) tot,
                MatierPremiere__c,
                Type__c
            from ChildObject__c
            where MatierPremiere__c = :parentIds
        ]) {   
            Id parentId = ar.get('MatierPremiere__c');
            if (ar.get('Type__c') == 'Entrée') {
                totalMap.put(parentId, totalMap.get(parentId) + ar.get('tot'));
            } else if (ar.get('Type__c') == 'Sortie') {
                totalMap.put(parentId, totalMap.get(parentId) - ar.get('tot'));
            }
        }   

        List<ParentObject__c> parentsToUpdate = new List<ParentObject__c>();

        for (Id id: totalMap.keySet()) {
            parentsToUpdate.add(new ParentObject__c(
                Id = id,
                Total__c = totalMap.get(id)
            ));
        }

        update parentsToUpdate;
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors.
Mick ManMick Man
Hi Pcon
thank you for your answer
I want to try to change the code with my own API, but there is an error message as "Error: Erreur de compilation : Illegal assignment from Object to Id à la ligne 23 colonne 25"