You need to sign in to do that
Don't have an account?
rajesh kumar 50
Trigger On Opportunity before insert and before update
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
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
Shashank (Salesforce Developers)
This is porbably not working because you are not updating trigger.new directly, but you are retrieving records via a SOQL query using the IDs in trigger.new. In this case, you may need an explicit DML statement. Or you can use maps to retrieve the field from accounts and iterate through trigger.new without using a DML.