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
Olivia FordeOlivia Forde 

Apex Trigger If Statement

I have this trigger and I want to change it to select 1 Gateway ID if the currency of the Opportunity is x and if the Brand on the product is Y ( Through Opportunity Product and Product) or select Gateway 2 if something else etc

trigger UpdateGateway on OpportunityLineItem (after insert, after update) {

 
 Id GatewayId = 'a012000001k5yoK'; 
Set<id>ids=new set<id>();

list<opportunity> oplst=new list<opportunity>();

    For(OpportunityLineItem ol:trigger.new){
        if(ol.Continuity__c == True){
            ids.add(ol.opportunityid);
        }

    }

    if(!ids.Isempty()){

        list<opportunity >opp=[select id,CSFA__Gateway__c from Opportunity where id in:ids];

        for(Opportunity op:opp){

                op.CSFA__Gateway__c= GatewayId;
           
                oplst.add(op);

            }

        

        update oplst;

    }  

}
vishnu Rvishnu R
trigger UpdateGateway on OpportunityLineItem (after insert, after update) {

 
 Id GatewayId = 'a012000001k5yoK'; 
Set<id>ids=new set<id>();

list<opportunity> oplst=new list<opportunity>();

    For(OpportunityLineItem ol:trigger.new){
        if(ol.Continuity__c == True){
            ids.add(ol.opportunityid);
        }

    }

list<opportunity >opp=[select id,CSFA__Gateway__c from Opportunity where id in:ids];   
if(!ids.Isempty()){
  for(Opportunity op:opp){

                op.CSFA__Gateway__c= GatewayId;
           
                oplst.add(op);

            }

        }

        update oplst;
}
Olivia FordeOlivia Forde
hi - the trigger posted originally work but I want to add more choices into it ?
 
Manmohan SinghManmohan Singh
You must use - Conditional (If-Else) Statements.- Apex does not support Switch statement

if (place == 1) { medal_color = 'gold'; } else if (place == 2) { medal_color = 'silver'; } else if (place == 3) { medal_color = 'bronze'; } else { medal_color = null; }
Olivia FordeOlivia Forde
OK I'll give it a go I hope I can get the syntax correct
JeffreyStevensJeffreyStevens
Maybe something like this:
 
trigger UpdateGateway on OpportunityLineItem (after insert, after update) {

 
 Id GatewayId = 'a012000001k5yoK';
id GatewayId2 = 'a012000001k5yoK';
  
Set<id>ids=new set<id>();

list<opportunity> oplst=new list<opportunity>();

    For(OpportunityLineItem ol:trigger.new){
        if(ol.Continuity__c == True){
            ids.add(ol.opportunityid);
        }

    }

    if(!ids.Isempty()){

        list<opportunity >opp=[select id,CSFA__Gateway__c,CurrencyIsoCode FROM Opportunity where id in:ids];

        for(Opportunity op:opp){
                if(op.currencyIsoCode = 'USD') {
                  op.CSFA__Gateway__c= GatewayId;
                } else {
                  op.CSFA__Gateway__c= GatewayId2;
                }
                oplst.add(op);
            }
        update oplst;
    }  

}

 
Manmohan SinghManmohan Singh
Hi Olivia Forde,

Hope you are able to fix this. let us know otherwise ;)

Regards, Manmohan