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 

Having problem with trigger on opportunity object ..

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..
 
RamuRamu (Salesforce Developers) 
Not sure why you are using a custom record type field with the name 'Record_Type_Name__c'. Basically to create a new record with a particularr record type, we should give its 15 or 18 digit id something as o2.Recordtype='xxxxxxxxxxxxxx'
rajesh kumar 50rajesh kumar 50
Hi Ramu thanks for suggesting me and ihave doubt that whether the given condition works fine or not i want to know can u tell me please

thanks in advance