You need to sign in to do that
Don't have an account?

Before insert want to update one of its field
if(trigger.isInsert){
For(contract__c con:trigger.new){
datetime s;
Try{
if(con.Contract_Status__c == 'Renewal'){
Contract__c Cold = [Select Contract_Expiry_Date__c From Contract__c where ID=:con.Contract_Old__c];
s = cold.Contract_Expiry_Date__c;
datetime e = date.Today();
date startDate = Date.newInstance(s.year(),s.month(),s.day());
date enddate = date.newInstance(e.year(),e.month(),e.day());
integer numberDaysDue = startDate.daysBetween(enddate);
if(numberDaysDue < 7){
con.Renewal_Duration__c = 'Less than 1 week';
}
if(numberDaysDue >= 7 && numberDaysDue <= 30 ){
con.Renewal_Duration__c = '2 to 4 weeks';
}
if(numberDaysDue > 30 ){
con.Renewal_Duration__c = 'More than 1 Month';
}
}
}
catch(Exception e){}
}
}
i want to update the contract it self , when its getting inserted, from above code i want remove query from for loop, i did using set<id>. as below code, as im checking for ID's its give null bez its before insert, How to remove query from for loop.
if(trigger.isInsert){
For(contract__c con:trigger.new){
if(con.Contract_Status__c == 'Renewal'){
contracts_oldids.add(con.Contract_Old__c);
}
}
if(contracts_oldids.size()>0){
contract_oldque = [Select Contract_Old__c,Contract_Expiry_Date__c From Contract__c where ID IN:contracts_oldids];
}
For(contract__c con:trigger.new){
for(Opportunity o:oppall){
if(Con.Opportunity__c == o.ID ){
if(o.Probability != 100){
trigger.new[0].addError('Opportunity Percentage 100%');
}
}
}
datetime s;
Try{
if(con.Contract_Status__c == 'Renewal'){
for(Contract__c Cold:contract_oldque ){
if(Cold.Contract_Old__c==con.id){
this id getting empty
I think you have your comparison logic flipped around. Shouldn't you be comparing the Cold.Id to the con.Contract_Old__c instead?
Like so:
All Answers
I think you have your comparison logic flipped around. Shouldn't you be comparing the Cold.Id to the con.Contract_Old__c instead?
Like so:
Thanks. Yes i messed up the comparision..
Now its working fine.