You need to sign in to do that
Don't have an account?
vanessa
before insert or after insert
hi.
I make a trigger to update a field on Opportunity already created when it was created a custom object "proposta__c" (related list on Opportunity)
but isnt obrigatory have one "proposta__c" associated to the opportunity.
When the trigger not find the "proposta__c" i want to skip. I tried with List<> but not works..
here's my code - with the list commented and the other.
Please help me
trigger preencherNumeroOportunidade on Opportunity (after insert, before update) {
for (Opportunity o : Trigger.new)
{
Proposta__c p = [select Proposta_N__c, Validade_em_dias__c from Proposta__c
where
Oportunidade__c =:o.id AND
Valida__c ='Sim' LIMIT 1
];
//if(p.Proposta_N__c !=null ){
o.N_proposta_background__c = p.Proposta_N__c;
o.validade_proposta_background__c = p.Validade_em_dias__c;
// }
}}
/*trigger preencherNumeroOportunidade on Opportunity (after insert, before update, after update) {
Opportunity o = new Opportunity ();
List <Proposta__c> p = new List<Proposta__c>();
p = [select Proposta_N__c, Validade_em_dias__c from Proposta__c
where
Oportunidade__c =:o.id LIMIT 1
];
if(p.size()>0)
{
o.N_proposta_background__c = p[0].Proposta_N__c;
o.validade_proposta_background__c = p[0].Validade_em_dias__c;
update o;
}
}*/
use trigger.new[0].addError("Child record is already exists for the opportunity"); in your else condn
All Answers
if I understand you correctly, you want to update a field on opportunity when you add/update the child record.
if this is the case, your trigger has to be on the childobject
trigger mytrigger on childobject(after insert, after update)
{
Opportunity op = new Opportunity(Trigger.new[0].OpportunityId__c);
op.yourfieldname = Trigger.new[0].Necessaryfieldname__c;
update op;
//You definitely have to bulk enable this. Check documentation how to bulk enable a trigger.
}
Thanks
hi!
Thanks u very much. Yes u understand me :)
yesterday i had done that! and works.
:) i have a other problem if u can help me.
i have 2 objects, Opportunity and Proposta. Proposta is a child object of opportunity.
When i add a proposta record i want to see if there are already a proposta record associated to the opportunity and accepted i dont want to save the data and return a error to the user. can u help me?
my code:
trigger updateNumeroProposta on Proposta__c (before insert, after insert) {
for(Proposta__c prop : Trigger.new){
List<Proposta__c> proposta = new List<Proposta__c>();
//Integer count = [select count() from Proposta__c
//where oportunidade__c = : prop.Oportunidade__c order by IsPrimary Desc limit 1];
//];
proposta = [ select proposta_n__c from Proposta__c where oportunidade__c = :prop.Oportunidade__c
AND Valida__c ='Sim'
limit 1
];
if(proposta.size()<=1)
{
// if (count <1){
//update the field on opportunity and add the proposta record.
Opportunity op = [select name from Opportunity
where
id = :prop.Oportunidade__c];
op.N_proposta_background__c = prop.Proposta_N__c;
op.validade_proposta_background__c = prop.Validade_em_dias__c;
update op;
//}else {
//addError - Skip of saving data
//}
}else {
}
}
}
use trigger.new[0].addError("Child record is already exists for the opportunity"); in your else condn
thanks :)