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 

Doubt on Trigger

Below is my trigger:
trigger opportunityinsertupdate on opportunity(before insert,before update) {
boolean flag=True;
    if(trigger.isInsert) {     
        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.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.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;
            }
       
        }
    }
}

BUt the main problem is super_region__c field in on account object and based on that field also the trigger have to fired..
here in my code i have commented super_region__c field but i want to take that field from account object.
so can any one suggest me how to solve this problem.
thanks in advance.
Arunkumar RArunkumar R
Hi Rajesh,

Can you please try the below code,

trigger opportunityinsertupdate on opportunity(before insert,before update) {

Set<Id> accountIds = new Set<Id>();
for(Opportunity currentOpportunity: Trigger.New)
{
	accountIds.add(currentOpportunity.AccountId);
}

Map<Id, Account> accountMap = new Map<Id, Account>[Select Id, Super_Region__c from Account Where AccountId in:accountIds];
boolean flag=True;
    if(trigger.isInsert) {     
        for(opportunity o : trigger.new) {
		if(accountMap.get(o.AccountId) != null)
		{
            if(((o.Record_Type_Name__c == 'NC Power')||(o.Record_Type_Name__c == 'NC Oil & Gas')) && (o.FS_Included__c == false) && accountMap.get(o.AccountId).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(accountMap.get(o.AccountId) != null)
		{
            if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')) && (o1.FS_Included__c == false) && accountMap.get(o.AccountId).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;
            }
       
        }
		}
    }
}


ManjunathManjunath
Hi Rajesh,

I believe you are trying to get the associated Account record's field "super_region__c"   from the opportunity.
If that is the case you cannot get the lookup recods field through this.

1: You need to first get the account id associated with the opportunity
2: Next get the query the Account with that field "super_region__c"
3: Next do your existing logic what yoou have written.

Hope that helps you.

Regards