You need to sign in to do that
Don't have an account?
rajesh 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..
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..
thanks in advance