• rajesh kumar 50
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 14
    Questions
  • 4
    Replies
Here Is my Trigger:

trigger opportunityinsertupdate on Opportunity (before insert,before update) {
    if(checkRecursive.runOnce()) {
    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 Id in:accountIds]);
        boolean flag=True;
        
     
 
            if(Trigger.isInsert){
                for(Opportunity opp: trigger.New){
                    system.debug('opp.name.===='+opp.name);
                    if(opp.name != null){
                        if(opp.name.startsWith('FO-')){
                            opp.CampaignId = '701U0000000QsAA';
                        }
                    }    
                 }
                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.Record_Type_Name__c == 'NC Nuclear')) &&(o.stagename == 'Closed Won') && (o.FS_Included__c == false) && accountMap.get(o.AccountId).Super_Region__c == 'Asia/India') {
                            o.Name = o.Name + ' - FS Opp';
                            o.RecordTypeId = '012U0000000UIoX';
                            o.stagename = 'Sales Lead';
                            o.amount = 1;
                            o.CurrencyIsoCode = 'USD';
                            o.Target_ShipDate__c = o.CloseDate.addmonths(3);
                            flag = false;
                            o.FS_Included__c = true;
                        }
                     
                    }
                }
                  
                                                
                              
                    
                   
                
           
    }   
         
     if(trigger.isUpdate && flag)  {
     
            for(opportunity o1:trigger.new) {
                if(accountMap.get(o1.AccountId) != null) {
               
                        if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')||(o1.Record_Type_Name__c == 'NC Nuclear')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.AccountId).Super_Region__c == 'Asia/India') {
                                              
                            Opportunity o2= new opportunity();
                            o2.name = o1.name+' - Fsopp';
                            o2.CloseDate = o1.CloseDate;
                            o2.RecordTypeId = '012U0000000UIoX';
                            o2.stagename = 'Sales Lead';
                            o2.amount = 1;
                            o2.CurrencyIsoCode = 'USD';
                        
                            o2.Target_ShipDate__c = o1.CloseDate.addmonths(3);
                          
                            o2.FS_Included__c = true;
                            insert o2;
                    
                        }
                 
                }
            }
        }
}

}
please help me writing the test class for this trigger

thanks in advance..
MY Trigger:

trigger opportunityinsertupdate on Opportunity (before insert,before update) {
    if(checkRecursive.runOnce()) {
    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 Id in:accountIds]);
        boolean flag=True;
        
       
 
            if(Trigger.isInsert){
                for(Opportunity opp: trigger.New){
                    system.debug('opp.name.===='+opp.name);
                    if(opp.name != null){
                        if(opp.name.startsWith('FO-')){
                            opp.CampaignId = '701U0000000QsAA';
                        }
                    }    
                 }
                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.Record_Type_Name__c == 'NC Nuclear')) &&(o.stagename == 'Closed Won') && (o.FS_Included__c == false) && accountMap.get(o.AccountId).Super_Region__c == 'Asia/India') {
                            o.Name = o.Name + '- FS Opp';
                            o.stagename = 'Sales Lead';
                            o.amount = 1;
                            o.CurrencyIsoCode = 'USD';
                            o.Target_ShipDate__c = o.CloseDate.addmonths(3);
                            flag = false;
                            o.FS_Included__c = true;
                        }
                  
                    }
                }
                  
                  
                
         
    }   
   
     if(trigger.isUpdate && flag)  {
      
            for(opportunity o1:trigger.new) {
                if(accountMap.get(o1.AccountId) != null) {
              
                        if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')||(o1.Record_Type_Name__c == 'NC Nuclear')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.AccountId).Super_Region__c == 'Asia/India' /*&& o1.Check__c == false*/) {
 
               
                            Opportunity o2= new opportunity();
                            o2.name = o1.name+'Fs-opp';
                            o2.CloseDate = o1.CloseDate;
                            o2.RecordTypeId = '012U0000000UIoX';
                            o2.stagename = 'Sales Lead';
                            o2.amount = 1;
                            o2.CurrencyIsoCode = 'USD';
                       
                            o2.Target_ShipDate__c = o1.CloseDate.addmonths(3);
                         
                            o2.FS_Included__c = true;
                            insert o2;
                
                        }
             
                }
            }
        }
}

}


and i have tried test class also but i think its completed mess..

My TEST class:

@isTest(SeeAllData=true)
public class TestOppInsertUpdate {
    Set<Id> accountIds = new Set<Id>();
    Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Super_Region__c from Account Where Id in:accountIds]);
       
    static testMethod Void OppInsert() {
        RecordType rtid = new RecordType();
        rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
        opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
                            stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
                            where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India' 
                            and FS_Included__c = false];
        Opportunity o = new Opportunity();                  
        o.stagename = 'Sales Lead';
        o.amount = 1;
        o.CurrencyIsoCode = 'USD';
        o.Target_ShipDate__c = o.CloseDate.addmonths(3);
        //flag = false;
        
        test.startTest();
        insert o;
        test.stopTest();
    }
    static testMethod Void Oppupdate() {
   Set<Id> accountIds = new Set<Id>();
    Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Super_Region__c from Account Where Id in:accountIds]);
        RecordType rtid = new RecordType();
        rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
        opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
                            stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
                            where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India' 
                            and FS_Included__c = false];
        if(trigger.isUpdate)  {
          //  for(opportunity o1:trigger.new) {
            if(accountMap.get(opp.AccountId) != null) {
                        if(((opp.Record_Type_Name__c == 'NC Power')||(opp.Record_Type_Name__c == 'NC Oil & Gas')||(opp.Record_Type_Name__c == 'Controls Field Service')) &&(opp.stagename == 'Closed Won') && (opp.FS_Included__c == false) && accountMap.get(opp.AccountId).Super_Region__c == 'Asia/India') {                   
        Opportunity o = new Opportunity();                  
        o.stagename = 'Sales Lead';
        o.amount = 1;
        o.CurrencyIsoCode = 'USD';
        o.Target_ShipDate__c = o.CloseDate.addmonths(3);
        //flag = false;
        //check__C = true;
        
        test.startTest();
        update o;
        test.stopTest();
    }
}
//}
}
}
}   
        
                            
             
please can any one help me out of this. 

thanks in advance               
                            
        
             
below is the  trigger that i was written:

trigger opportunityinsertupdate on Opportunity (before insert,before update) {
    if(checkRecursive.runOnce()) {
    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 Id in:accountIds]);
        boolean flag=True;
        
        List<Account> a = new List<Account>([SELECT T_Code__c FROM Account where Name =: accName ]);
 
            if(Trigger.isInsert){
                for(Opportunity opp: trigger.New){
                    system.debug('opp.name.===='+opp.name);
                    if(opp.name != null){
                        if(opp.name.startsWith('FO-')){
                            opp.CampaignId = '701U0000000QsAA';
                        }
                    }    
                 }
                for(opportunity o : trigger.new) {
                    if(accountMap.get(o.AccountId) != null)    {
                    if(!(a.isEmpty())){ 
                        if(((o.Record_Type_Name__c == 'NC Power')||(o.Record_Type_Name__c == 'NC Oil & Gas')||(o.Record_Type_Name__c == 'NC Nuclear')) &&(o.stagename == 'Closed Won') && (o.FS_Included__c == false) && accountMap.get(o.AccountId).Super_Region__c == 'Asia/India') {
                            o.Name = o.Name + '- FS Opp';
                            o.stagename = 'Sales Lead';
                            o.amount = 1;
                            o.CurrencyIsoCode = 'USD';
                            o.Target_ShipDate__c = o.CloseDate.addmonths(3);
                            flag = false;
                            o.FS_Included__c = true;
                        }
                    }    
                    }
                }
                   //insert opp;
                           /*campaign cam=new campaign();
                        cam.id=opp.campaignid;
                        insert cam;*/
                                                
                              
                    //}
                    /*else if(opp.name.startsWith('PL-')){
                        opp.CampaignId = '701U0000000Qu1b';
                        && (opp.Super_Region__c == 'ASIA/INDIA')
                        system.debug('---------CampaignId-------'+opp.CampaignId); 
                    }*/
                
            /*if(Trigger.isUpdate){
                if(opp.name.startsWith('FO-')){
                        opp.CampaignId = '701U0000000RJ4r';

                        system.debug('---------CampaignId-------'+opp.CampaignId); 
                                                                
                    }else if(opp.name.startsWith('PL-')){
                        opp.CampaignId = '701U0000000Qu1b';

                    }
                }*/
    }   
  //  }         
     if(trigger.isUpdate && flag)  {
            for(opportunity o1:trigger.new) {
                if(accountMap.get(o1.AccountId) != null) {
                if(!(a.isEmpty())){ 
                        if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')||(o1.Record_Type_Name__c == 'NC Nuclear')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.AccountId).Super_Region__c == 'Asia/India' && o1.Check__c == false) {
                           /* o1.Name = o1.Name + '- FS Opp';
                            o1.stagename = 'Sales Lead';
                            o1.amount = 1;
                            o1.CurrencyIsoCode = 'USD';
                            o1.Target_ShipDate__c = o1.CloseDate.addmonths(3);
                            o1.Check__c = true;
                            o1.FS_Included__c = true;*/
                            
                            Opportunity o2= new opportunity();
                            o2.Record_Type_Name__c = 'Controls field service';
                            o2.stagename = 'Sales Lead';
                            o2.amount = 1;
                            o2.CurrencyIsoCode = 'USD';
                            o2.Target_ShipDate__c = o1.CloseDate.addmonths(3);
                            o2.Check__c = true;
                            o2.FS_Included__c = true;
                            
                        }
                }        
                }
            }
        }
}
//}
}

But my condition is when i update a exixting record and if the given condition met the criteria then it should create a new record with 
recordtype="controls field service" and the existing recoed wat we try to update should me remain same..
 i have written a above trigger but i dont know whether it cerates a new record when i update but i was facing
a error when i try to save it
The Error is RecordTypeName field is not writtable..

can any help in out of this please..
 
Below is my trigger: 

trigger opportunityinsertupdate on Opportunity (before insert,before update) {
    if(checkRecursive.runOnce()) {
    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 Id in:accountIds]);
        boolean flag=True;
        
      
 
            if(Trigger.isInsert){
                for(Opportunity opp: trigger.New){
                    system.debug('opp.name.===='+opp.name);
                    if(opp.name != null){
                        if(opp.name.startsWith('FO-')){
                            opp.CampaignId = '701U0000000QsAA';
                        }
                    }    
                 }
                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.Record_Type_Name__c == 'NC Nuclear')) &&(o.stagename == 'Closed Won') && (o.FS_Included__c == false) && accountMap.get(o.AccountId).Super_Region__c == 'Asia/India') {
                            o.Name = o.Name + '- FS Opp';
                            o.stagename = 'Sales Lead';
                            o.amount = 1;
                            o.CurrencyIsoCode = 'USD';
                            o.Target_ShipDate__c = o.CloseDate.addmonths(3);
                            flag = false;
                            o.FS_Included__c = true;
                        }
                    }    
                    }
                }
                
      }   
         
     if(trigger.isUpdate && flag)  {
            for(opportunity o1:trigger.new) {
                if(accountMap.get(o1.AccountId) != null) {
    
                        if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')||(o1.Record_Type_Name__c == 'NC Nuclear')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.AccountId).Super_Region__c == 'Asia/India' && o1.Check__c == false) {
                            o1.Name = o1.Name + '- FS Opp';
                            o1.stagename = 'Sales Lead';
                            o1.amount = 1;
                            o1.CurrencyIsoCode = 'USD';
                            o1.Target_ShipDate__c = o1.CloseDate.addmonths(3);
                            o1.Check__c = true;
                            o1.FS_Included__c = true;
                        }
                }        
                }
            }
        }
}

}
Below is my trigger:

trigger opportunityinsertupdate on Opportunity (before insert,before update) {
    if(checkRecursive.runOnce()) {
    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 Id in:accountIds]);
        boolean flag=True;
 
            if(Trigger.isInsert){
                for(Opportunity opp: trigger.New){
                    system.debug('opp.name.===='+opp.name);
                    if(opp.name != null){
                        if(opp.name.startsWith('FO-')){
                            opp.CampaignId = '701U0000000QsAA';
                        }
                    }    
                 }
                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.Record_Type_Name__c == 'Controls Field Service')) &&(o.stagename == 'Closed Won') && (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.CloseDate.addmonths(3);
                            flag = false;
                        }
                    }
                }
                  
                        opp.CampaignId = '701U0000000Qu1b';
                        && (opp.Super_Region__c == 'ASIA/INDIA')
                        system.debug('---------CampaignId-------'+opp.CampaignId); 
                  
                                                                
                  
    }   
  
     if(trigger.isUpdate && flag)  {
            for(opportunity o1:trigger.new) {
                if(accountMap.get(o1.AccountId) != null) {
                        if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')||(o1.Record_Type_Name__c == 'Controls Field Service')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.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.CloseDate.addmonths(3);
                            o1.Check__c = true;
                        }
                }
            }
        }
}
}


and below is my test calss written for my trigger:

@isTest(SeeAllData=true)
public class TestOppInsertUpdate {
    static testMethod Void OppInsert() {
        RecordType rtid = new RecordType();
        rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
        opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
                            stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
                            where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India' 
                            and FS_Included__c = false];
        Opportunity o = new Opportunity();                    
        o.stagename = 'Sales Lead';
        o.amount = 1;
        o.CurrencyIsoCode = 'USD';
        o.Target_ShipDate__c = o.CloseDate.addmonths(3);
        flag = false;
        
        test.startTest();
        insert o;
        test.stopTest();
    }
    static testMethod Void Oppupdate() {
        RecordType rtid = new RecordType();
        rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
        opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
                            stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
                            where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India' 
                            and FS_Included__c = false];
        Opportunity o = new Opportunity();                    
        o.stagename = 'Sales Lead';
        o.amount = 1;
        o.CurrencyIsoCode = 'USD';
        o.Target_ShipDate__c = o.CloseDate.addmonths(3);
        flag = false;
        check__C = true;
        
        test.startTest();
        update o;
        test.stopTest();
    }
}

I was not able to cover my trigger so can any one help me writting the test class for the above trigger.
Can any one help me to write a test class for the below trigger:

trigger opportunityinsertupdate on opportunity(before insert,before update) {
    if(checkRecursive.runOnce())  {

        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 Id 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(o1.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(o1.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;
                        }
                }
            }
        }
    }
}

i was trying with inserting a record but i want to try it with
@isTest(SeeAllData=true)
can any suggest me to slove this please
thanks in advance..
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 :[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;
            }
       
        }
    }
}

But by my code nothing is updating in the record and i want to write a query outside the for loop.
can any one suggest me to solve my problem please.

thanks in advance
i was facing problem with the test class for the following trigger.

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

can any one tell how to caver this trigger
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
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.
Below is my trigger:
trigger opportunityinsertupdate on opportunity(before insert,before update) {
    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);
            }
        }
    }
   
    if(trigger.isUpdate)  {
        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.Check__c == false) {
                o.stagename = 'Sales Lead';
                o.amount = 1;
                o.CurrencyIsoCode = 'USD';
                o.Target_ShipDate__c = o.Target_ShipDate__c.addmonths(3);
                o.Check__c = true;
            }
       
        }
    }
}

But i was facing a problem that when i insert a record the shipdate date is adding 6 months
and check__c field is setting to true..
But my requirement is a record is inserted the shipdate should add 3 months only with exiting shopdate
and when any record is updated then shipdate should add 3 months and check box is set to true..
but with my trigger when i am inserting also the check box is seeting to true and shipdate is adding to 6 months..
can any suggest me to solve my trigger please
thanks in advance..

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...

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)...
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..

below is the  trigger that i was written:

trigger opportunityinsertupdate on Opportunity (before insert,before update) {
    if(checkRecursive.runOnce()) {
    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 Id in:accountIds]);
        boolean flag=True;
        
        List<Account> a = new List<Account>([SELECT T_Code__c FROM Account where Name =: accName ]);
 
            if(Trigger.isInsert){
                for(Opportunity opp: trigger.New){
                    system.debug('opp.name.===='+opp.name);
                    if(opp.name != null){
                        if(opp.name.startsWith('FO-')){
                            opp.CampaignId = '701U0000000QsAA';
                        }
                    }    
                 }
                for(opportunity o : trigger.new) {
                    if(accountMap.get(o.AccountId) != null)    {
                    if(!(a.isEmpty())){ 
                        if(((o.Record_Type_Name__c == 'NC Power')||(o.Record_Type_Name__c == 'NC Oil & Gas')||(o.Record_Type_Name__c == 'NC Nuclear')) &&(o.stagename == 'Closed Won') && (o.FS_Included__c == false) && accountMap.get(o.AccountId).Super_Region__c == 'Asia/India') {
                            o.Name = o.Name + '- FS Opp';
                            o.stagename = 'Sales Lead';
                            o.amount = 1;
                            o.CurrencyIsoCode = 'USD';
                            o.Target_ShipDate__c = o.CloseDate.addmonths(3);
                            flag = false;
                            o.FS_Included__c = true;
                        }
                    }    
                    }
                }
                   //insert opp;
                           /*campaign cam=new campaign();
                        cam.id=opp.campaignid;
                        insert cam;*/
                                                
                              
                    //}
                    /*else if(opp.name.startsWith('PL-')){
                        opp.CampaignId = '701U0000000Qu1b';
                        && (opp.Super_Region__c == 'ASIA/INDIA')
                        system.debug('---------CampaignId-------'+opp.CampaignId); 
                    }*/
                
            /*if(Trigger.isUpdate){
                if(opp.name.startsWith('FO-')){
                        opp.CampaignId = '701U0000000RJ4r';

                        system.debug('---------CampaignId-------'+opp.CampaignId); 
                                                                
                    }else if(opp.name.startsWith('PL-')){
                        opp.CampaignId = '701U0000000Qu1b';

                    }
                }*/
    }   
  //  }         
     if(trigger.isUpdate && flag)  {
            for(opportunity o1:trigger.new) {
                if(accountMap.get(o1.AccountId) != null) {
                if(!(a.isEmpty())){ 
                        if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')||(o1.Record_Type_Name__c == 'NC Nuclear')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.AccountId).Super_Region__c == 'Asia/India' && o1.Check__c == false) {
                           /* o1.Name = o1.Name + '- FS Opp';
                            o1.stagename = 'Sales Lead';
                            o1.amount = 1;
                            o1.CurrencyIsoCode = 'USD';
                            o1.Target_ShipDate__c = o1.CloseDate.addmonths(3);
                            o1.Check__c = true;
                            o1.FS_Included__c = true;*/
                            
                            Opportunity o2= new opportunity();
                            o2.Record_Type_Name__c = 'Controls field service';
                            o2.stagename = 'Sales Lead';
                            o2.amount = 1;
                            o2.CurrencyIsoCode = 'USD';
                            o2.Target_ShipDate__c = o1.CloseDate.addmonths(3);
                            o2.Check__c = true;
                            o2.FS_Included__c = true;
                            
                        }
                }        
                }
            }
        }
}
//}
}

But my condition is when i update a exixting record and if the given condition met the criteria then it should create a new record with 
recordtype="controls field service" and the existing recoed wat we try to update should me remain same..
 i have written a above trigger but i dont know whether it cerates a new record when i update but i was facing
a error when i try to save it
The Error is RecordTypeName field is not writtable..

can any help in out of this please..
 
Below is my trigger:

trigger opportunityinsertupdate on Opportunity (before insert,before update) {
    if(checkRecursive.runOnce()) {
    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 Id in:accountIds]);
        boolean flag=True;
 
            if(Trigger.isInsert){
                for(Opportunity opp: trigger.New){
                    system.debug('opp.name.===='+opp.name);
                    if(opp.name != null){
                        if(opp.name.startsWith('FO-')){
                            opp.CampaignId = '701U0000000QsAA';
                        }
                    }    
                 }
                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.Record_Type_Name__c == 'Controls Field Service')) &&(o.stagename == 'Closed Won') && (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.CloseDate.addmonths(3);
                            flag = false;
                        }
                    }
                }
                  
                        opp.CampaignId = '701U0000000Qu1b';
                        && (opp.Super_Region__c == 'ASIA/INDIA')
                        system.debug('---------CampaignId-------'+opp.CampaignId); 
                  
                                                                
                  
    }   
  
     if(trigger.isUpdate && flag)  {
            for(opportunity o1:trigger.new) {
                if(accountMap.get(o1.AccountId) != null) {
                        if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')||(o1.Record_Type_Name__c == 'Controls Field Service')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.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.CloseDate.addmonths(3);
                            o1.Check__c = true;
                        }
                }
            }
        }
}
}


and below is my test calss written for my trigger:

@isTest(SeeAllData=true)
public class TestOppInsertUpdate {
    static testMethod Void OppInsert() {
        RecordType rtid = new RecordType();
        rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
        opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
                            stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
                            where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India' 
                            and FS_Included__c = false];
        Opportunity o = new Opportunity();                    
        o.stagename = 'Sales Lead';
        o.amount = 1;
        o.CurrencyIsoCode = 'USD';
        o.Target_ShipDate__c = o.CloseDate.addmonths(3);
        flag = false;
        
        test.startTest();
        insert o;
        test.stopTest();
    }
    static testMethod Void Oppupdate() {
        RecordType rtid = new RecordType();
        rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
        opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
                            stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
                            where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India' 
                            and FS_Included__c = false];
        Opportunity o = new Opportunity();                    
        o.stagename = 'Sales Lead';
        o.amount = 1;
        o.CurrencyIsoCode = 'USD';
        o.Target_ShipDate__c = o.CloseDate.addmonths(3);
        flag = false;
        check__C = true;
        
        test.startTest();
        update o;
        test.stopTest();
    }
}

I was not able to cover my trigger so can any one help me writting the test class for the above trigger.
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)...
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..