+ Start a Discussion

trigger OppUpBsdOnStage Error How to Resolve

trigger OppUpBsdOnStage on Opportunity (before update) {
    map<id,Opportunity> oppold= trigger.oldMap;
    map<id,Opportunity> oppnew= trigger.newMap;
    set<id> soi=oppold.keySet();
    for(id i:soi)
        Opportunity oldop=oppold.get(i);
        Opportunity nwop=oppnew.get(i);
        if(oldop.StageName=='Closed Won'  &&  nwOp.StageName=='Closed Won')
            nwop.type='New Customer';
            oppnew.put(i, nwop);

    update oppnew.values();
}Trigger Error
David Zhu 🔥David Zhu 🔥
As the error message suggests, you will need to remove DML line.
update oppnew.values();
Ankit Kalsara 6Ankit Kalsara 6
Hi KapavariVenkatramana,

Explicit DMLs are not required in before triggers. System automatically updates the value provided during the runtime.

Try below code.
trigger OppUpBsdOnStage on Opportunity (before update) {

    for(Integer i = 0 ; i < trigger.new.size() ; i++){
        Opportunity oldOpp = (Opportunity)trigger.old[i];
        Opportunity newOpp = (Opportunity)trigger.new[i];

        if(oldOpp.StageName == 'Closed Won' && newOpp.StageName == 'Closed Won'){
            newOpp.ClosedDate = System.today();
            newOpp.type = 'New Customer';