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 i am new to this and facing problem with trigger i have tried writting trigger but i am confused can any help me out of this please..

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) 

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+
}

}
}
}


can any one help me solving this problem thanks in advance..

Mani PenumarthiMani Penumarthi
Rajesh,

Go throught this link http://www.cloudforce4u.com/2013/07/compare-old-and-new-values-in-trigger.html

You need to compare the old and new values while updating so that it won't fire every time.
Naveen Rahul 3Naveen Rahul 3
Hi rajesh , 

the error with above codes were error with list assignment,boolean comparion,wrong conditional closure bracket.

below code will work, like what you expecting.

trigger opportunityinsertupdate on opportunity(after insert,after update) {
    if(trigger.isInsert) {
        list<Opportunity> olist = new list<Opportunity>();
        olist=[select RecordType__c,id,FS_Included__c,Region__c from Opportunity];
        for(opportunity o:trigger.new) {
            if((o.Record_Type_Name__c == 'NC Power'||'NC Oil & Gas') && (o.FS_Included__c == false) && (o.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((o1.Record_Type_Name__c == 'NC Power'||'NC Oil & Gas') && (o1.FS_Included__c == false) && (o1.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;
             
            }
        }
    }
  
}
rajesh kumar 50rajesh kumar 50
Hi naveen rahul i dont think it works well bcs the code wat i written was completly mess with the condition what i want and u have modified my code withput reading the condition so could you you please that and thanks for being modified my code.
Naveen Rahul 3Naveen Rahul 3

Hi rajesh how about this

to get 3 month from now you can use the below code.

Date today = System.today();
Date monthPlus = today.addMonths(3);
and 

trigger opportunityinsertupdate on opportunity(after insert,before update) {
    if(trigger.isInsert) {
        list<Opportunity> olist = new list<Opportunity>();
  list<Opportunity> newname = new list<Opportunity>();
        olist=[select RecordType__c,id,FS_Included__c,Region__c from Opportunity];
        for(opportunity o:trigger.new) {
            if((o.Record_Type_Name__c == 'NC Power'||'NC Oil & Gas') && (o.FS_Included__c == false) && (o.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);
    o.updated_field__c=1;
   
   
  }
            
  }
}
  
  
    if(trigger.isupdate){
        for(Opportunity o1:trigger.new){
            if((o1.Record_Type_Name__c == 'NC Power'||'NC Oil & Gas') && (o1.FS_Included__c == false) && (o1.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;
             newname = [select updated_field__c from Opportunity id:o1];
    if(newname.updated_field__c==1){
    o1.Target_ShipDate__c = o1.Target_ShipDate__c.addMonths(3);
    o1.updated_field__c = 2;
    }else{
    o1.Target_ShipDate__c = o1.Target_ShipDate__c;
    }
            }
        }
    }
 
}

i have added an field(updated_field__c) in the update trigger  ,1 will be added to that field at very first time insert and 2 will be updated for first time update and it will not update every time.


Thanks
D Naveen rahul.