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
rajesh kumar 50rajesh kumar 50 

Facing problem with trigger on opportunity

when i insert or update a record some fields have to be automatically updated and when we update only once it should be updated based on the condition.
we have to do it in trigger only because in the conditon one field in related to account object i.e Super_region__c field so no way to do from workflow 
i have a written a trigger but it is not taking a relation field can any one suggest me to correct my issue 
below is my trigger 

trigger opportunityinsertupdate on opportunity(before insert,before update) {
boolean flag=True;
    if(trigger.isInsert) {
        //list<account> lacc = new list<account>();
        //lacc = [select id,super_region__c,name from account ];
        for(opportunity o : trigger.new) {
            if(((o.Record_Type_Name__c == 'NC Power')||(o.Record_Type_Name__c == 'NC Oil & Gas')) && (o.FS_Included__c == false) && (o.account.Super_Region__c == 'Asia/India')) {
                o.stagename = 'Sales Lead';
                o.amount = 1;
                o.CurrencyIsoCode = 'USD';
                o.Target_ShipDate__c = o.Target_ShipDate__c.addmonths(3);
                flag = false;
            }
        }
    }
   
    if(trigger.isUpdate && flag)  {
        for(opportunity o1:trigger.new){
            if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')) && (o1.FS_Included__c == false) && o1.account.Super_Region__c == 'Asia/India' && o1.Check__c == false) {
                o1.stagename = 'Sales Lead';
                o1.amount = 1;
                o1.CurrencyIsoCode = 'USD';
                o1.Target_ShipDate__c = o1.Target_ShipDate__c.addmonths(3);
                o1.Check__c = true;
            }
       
        }
    }
}
thanks in advance
Anoop yadavAnoop yadav
Hi,

Try the below code.
trigger opportunityinsertupdate on opportunity(before insert,before update) {
boolean flag=True;
    if(trigger.isInsert) {
        //list<account> lacc = new list<account>();
        //lacc = [select id,super_region__c,name from account ];
        for(opportunity o :[Select Id, Name, Record_Type_Name__c, FS_Included__c, account.Super_Region__c, Check__c, 
							stagename, amount, CurrencyIsoCode, Target_ShipDate__c
							From Opportunity Where Id IN :trigger.new) {
            if(((o.Record_Type_Name__c == 'NC Power')||(o.Record_Type_Name__c == 'NC Oil & Gas')) && (o.FS_Included__c == false) && (o.account.Super_Region__c == 'Asia/India')) {
                o.stagename = 'Sales Lead';
                o.amount = 1;
                o.CurrencyIsoCode = 'USD';
                o.Target_ShipDate__c = o.Target_ShipDate__c.addmonths(3);
                flag = false;
            }
        }
    }
   
    if(trigger.isUpdate && flag)  {
        for(opportunity o1 ::[Select Id, Name, Record_Type_Name__c, FS_Included__c, account.Super_Region__c, Check__c, 
							stagename, amount, CurrencyIsoCode, Target_ShipDate__c
							From Opportunity Where Id IN :trigger.new){
            if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')) && (o1.FS_Included__c == false) && o1.account.Super_Region__c == 'Asia/India' && o1.Check__c == false) {
                o1.stagename = 'Sales Lead';
                o1.amount = 1;
                o1.CurrencyIsoCode = 'USD';
                o1.Target_ShipDate__c = o1.Target_ShipDate__c.addmonths(3);
                o1.Check__c = true;
            }
       
        }
    }
}