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 

HI all Having problem with trigger on opportunity can u please assest me to solve this problem.

Hi i facing problem with trigger that on opportunity i.e in opportunity there are fields like fs_include__c , target_ship_date,amount,stage etc..
if any one inserts a new opportunity record then if the field record _type_name== 'nc power' ,super_region == 'asia' and fs_include__c is "false" then automatically some fields have to be filled like 'amount' should be '1' and target_ship_date__c should be existing target_ship_date plus 3 months etc. and shuold be followed for the update also means when the record is updated then the same process should be followed(not every time updated only once it is updated that means when the record is updated every time it should not be updated every time only once it should be updated)

Below is my trigger :

trigger opportunityinsertupdate on opportunity(after insert,after update) {
if(trigger.isInsert) {
list<oppurtunity>() olist = new list<apportunity>();
olist=[select RecordType,id,FS_Included__c,Region__c from opportunity]
for(opportunity o : trigger.new) {
if(Record_Type_Name__c == 'NC Power'||'NC Oil & Gas' && o.FS_Included__c == false && Super_Region__c == 'Asia/India') {
  o.stagename = 'Sales Lead';
  o.amount = 1;
  o.CurrencyIsoCode = USD - U.S.Dollar;
  o.Target_ShipDate__c = o.Target_ShipDate__c+
}
}
}
if(trigger.isupdate)  {
for(opportunity o1:trigger.new)
  if(Record_Type_Name__c == 'NC Power'||'NC Oil & Gas' && o.FS_Included__c == false && Super_Region__c == 'Asia/India') {
  o1stagename = 'Sales Lead';
  o1.amount = 1;
  o1.CurrencyIsoCode = USD - U.S.Dollar;
  o1.Target_ShipDate__c = o1.Target_ShipDate__c+
}

}
}
}

In this trigger i made many mistakes and i didnt add 3 months to the target ship date
can any one please sugest me to solve the trigger please
thanks in advance...

Virendra ChouhanVirendra Chouhan
Hello Rajesh,

firstly i want to tell you that why don't you use Workflow instead of trigger you can update fields from workflow.

And about y our trigger there is some mistakes which is i understood is--
  • firstly if  you want to modify the record in insert event you only done in Before Insert not in after.
  • Is Record_Type_Name__c is field ?? if yes then you forget to write o.Record_type_name__c. (In both side insert and update)
  • I'm not sure about it but i think you have to use this condition-- if((o.Record_Type_Name__c == 'NC Power' || o.Record_Type_Name__c =='NC Oil & Gas' ) && o.FS_Included__c == false && Super_Region__c == 'Asia')
  • In update code add this 'o1' into the olist(Opportunity list) nut make sure the list is accessable in this block if not then instansiat it on the top.  olist.add(o1);
  • and after for loop update this list
hope it'll help you.

regards
Virendra
RamuRamu (Salesforce Developers) 
Please add a new checkbox in opportunities with the name Check (API Name : Check__c) and use the below trigger code

trigger opportunityinsertupdate on opportunity(before insert,before update) {
	if(trigger.isInsert) {		
		for(opportunity o : trigger.new) {
			if(Record_Type_Name__c == 'NC Power'||'NC Oil & Gas' && o.FS_Included__c == false && Super_Region__c == 'Asia/India') {
				o.stagename = 'Sales Lead';
				o.amount = 1;
				o.CurrencyIsoCode = USD - U.S.Dollar;
				o.Target_ShipDate__c = o.Target_ShipDate__c.addmonths(3);
			}
		}
	}
	if(trigger.isupdate)  {
		for(opportunity o1:trigger.new){
			if(Record_Type_Name__c == 'NC Power'||'NC Oil & Gas' && o.FS_Included__c == false && Super_Region__c == 'Asia/India'&&check__c==false) {
				o1.stagename = 'Sales Lead';
				o1.amount = 1;
				o1.CurrencyIsoCode = USD - U.S.Dollar;
				o1.Target_ShipDate__c = o1.Target_ShipDate__c.addmonths(3);
				check__c=true;
			}
		
		}
	}
}