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
Snita LalSnita Lal 

Where to place an IF on a trigger?

Hi,

We've managed to get the trigger working fine in our production system although our processes have recently changed which means that we only want the trigger to fire when certain conditions are met. Please see below the working trigger with the IF statement placed where we believe it should be however it still fires even when these conditions are met. We've tried a couple times to place the IF statement in other areas and either hit errors and we are a little lost.
 
trigger UpdateAccountLookup on Opportunity (before insert,before update){

Set<id> setSiteids=new set<id>();
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();

list<account> acclst = new list<account>();

    For(Opportunity o : trigger.new){  
       
        setSiteids.add(o.Site_Location__c);
        mapsiteIdToopptId.put(o.Site_Location__c, o.Id);
    }
    
    for(Site__c objSite : [Select Id , Parent_Account__c from Site__c where Id IN :setSiteids ])
    {
        mapOpptIdToAccId.put(mapsiteIdToopptId.get(objSite.Id), objSite.Parent_Account__c);
    }
    
    for(Opportunity o : trigger.new){
    
        if(o.New_Site__c == false  || o.Takeover__c == false){
        
        o.AccountId = mapOpptIdToAccId.get(o.Id);
    }
}
}

Any help would be greatfully appreciated.

Thanks,

Snita

 
Best Answer chosen by Snita Lal
G10G10
Hi,
 
check ,whether you want to perform this functionality while update transaction or insert transaction, Based on the that you can use Trigger context variable like Trigger.insert or Trigger.isUpdate.

Please check below code ,

trigger UpdateAccountLookup on Opportunity (before insert,before update){
Set<id> setSiteids=new set<id>();
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();

    For(Opportunity o : trigger.new){  
        if(o.New_Site__c == false  || o.Takeover__c == false){
        setSiteids.add(o.Site_Location__c);
        mapsiteIdToopptId.put(o.Site_Location__c, o.Id);
        }
    }
    for(Site__c objSite : [Select Id , Parent_Account__c from Site__c where Id IN :setSiteids ])
    {
        mapOpptIdToAccId.put(mapsiteIdToopptId.get(objSite.Id), objSite.Parent_Account__c);
    }
    if(mapOpptIdToAccId != null && mapOpptIdToAccId.size()>0){
    for(Opportunity o : trigger.new){
       o.AccountId = mapOpptIdToAccId.get(o.Id);
    }
    }
}


You can even debug value of New_Site__c and Takeover__c field while executing the trigger using Syestem.debug() ;

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others 

All Answers

AbdelhakimAbdelhakim
Hi Snita,
What is the condition ?
Snita LalSnita Lal
Thanks for getting back to me Jiten, I tried your code but unfortunately couldn't get this to work, we are looking to get the OR(||) to work.

Abdelhakim - We are trying to get the trigger to only execute when either the New Site checkbox is unticked or when the checkbox Takeover is unticked.

Thanks,

Snita
Panduranga GollaPanduranga Golla
please check your declaration  --
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();

please check line --12   code --  mapsiteIdToopptId.put(o.Site_Location__c, o.Id);
                        line --12   consider the  mapsiteidToopptId<id,id>
but you have declared as map<id,string> 
please check this part .

i hope you should be declared as map<Id, id> mapsiteIdToopptId = new map<Id, id>();

then your problem will solve it
 
G10G10
Hi,
 
check ,whether you want to perform this functionality while update transaction or insert transaction, Based on the that you can use Trigger context variable like Trigger.insert or Trigger.isUpdate.

Please check below code ,

trigger UpdateAccountLookup on Opportunity (before insert,before update){
Set<id> setSiteids=new set<id>();
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();

    For(Opportunity o : trigger.new){  
        if(o.New_Site__c == false  || o.Takeover__c == false){
        setSiteids.add(o.Site_Location__c);
        mapsiteIdToopptId.put(o.Site_Location__c, o.Id);
        }
    }
    for(Site__c objSite : [Select Id , Parent_Account__c from Site__c where Id IN :setSiteids ])
    {
        mapOpptIdToAccId.put(mapsiteIdToopptId.get(objSite.Id), objSite.Parent_Account__c);
    }
    if(mapOpptIdToAccId != null && mapOpptIdToAccId.size()>0){
    for(Opportunity o : trigger.new){
       o.AccountId = mapOpptIdToAccId.get(o.Id);
    }
    }
}


You can even debug value of New_Site__c and Takeover__c field while executing the trigger using Syestem.debug() ;

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others 
This was selected as the best answer