Scenario: Cases has status "New, Sales, Closed". Cases is not related to opp but related to Accounts object.[Account and Opportunity are related]
I have created a field on Opportunity and I want that whathever is the status of Case, it should populate on the Opportuity object in the field i created.
Could you please tell me how to write a trigger based on the above scenario.
I tried writing a trigger but am stuck that how to match the account ID to the account Id on case to get the case status.
But I need to update the filed on Opportunity and Opportunity laos has account name.
this is the relation
Case-> Account
Account-> Opp
trigger Updateopp on Opportunity (after insert,after update) { list<Cases> ca = new list<Cases>(); for(Cases c:trigger.new) { list<Cases> caa = new list<Cases>(); ca = [select Status from Cases where id =: c.AccountId];
For this problem, I see you needing 2 triggers. The first one would be for when a cases is inserted/updated :
trigger UpdateOppFromCase on Case (after insert,after update)
{
Map<Id, Case> cases = new Map<Id, Case>();
for(Case c:trigger.new)
{
if (c.Status == 'Closed' || c.Status == 'Sales' || c.Status == 'New')
{
cases.put(c.AccountId, c);
}
}
if (!cases.isEmpty())
{
List<Opportunity> oppsToUpdate = [SELECT AccountId, fieldToUpdate__c FROM Opportunity WHERE AccountId IN :cases.keySet()];
for (Opportunity o : oppsToUpdate)
{
o.fieldToUpdate__c = cases.get(o.AccountId).Status;
}
update oppsToUpdate;
}
}
and another trigger for when an opportunity is inserted, for situations where there is already a case related to its account:
trigger UpdateOppFromOpp on Opportunity(before insert)
{
Map<Id, Opportunity> opps = new Map<Id, Opportunity>();
for(Opportunity o:trigger.new)
{
if (o.AccountId != null)
{
opps.put(o.AccountId, o);
}
}
if (!opps.isEmpty())
{
List<Case> casesToCheck = [SELECT AccountId, Status FROM Case WHERE AccountId IN :opps.keySet() AND (Status = 'New' OR Status = 'Closed' OR Status = 'Sales')];
for (Case c : casesToCheck)
{
opps.get(c.AccountId).fieldToUpdate__c = c.Status;
}
}
}
For this problem, I see you needing 2 triggers. The first one would be for when a cases is inserted/updated :
trigger UpdateOppFromCase on Case (after insert,after update)
{
Map<Id, Case> cases = new Map<Id, Case>();
for(Case c:trigger.new)
{
if (c.Status == 'Closed' || c.Status == 'Sales' || c.Status == 'New')
{
cases.put(c.AccountId, c);
}
}
if (!cases.isEmpty())
{
List<Opportunity> oppsToUpdate = [SELECT AccountId, fieldToUpdate__c FROM Opportunity WHERE AccountId IN :cases.keySet()];
for (Opportunity o : oppsToUpdate)
{
o.fieldToUpdate__c = cases.get(o.AccountId).Status;
}
update oppsToUpdate;
}
}
and another trigger for when an opportunity is inserted, for situations where there is already a case related to its account:
trigger UpdateOppFromOpp on Opportunity(before insert)
{
Map<Id, Opportunity> opps = new Map<Id, Opportunity>();
for(Opportunity o:trigger.new)
{
if (o.AccountId != null)
{
opps.put(o.AccountId, o);
}
}
if (!opps.isEmpty())
{
List<Case> casesToCheck = [SELECT AccountId, Status FROM Case WHERE AccountId IN :opps.keySet() AND (Status = 'New' OR Status = 'Closed' OR Status = 'Sales')];
for (Case c : casesToCheck)
{
opps.get(c.AccountId).fieldToUpdate__c = c.Status;
}
}
}
I only did that because I thought you only wanted to execute the logic for those 3 conditions. If you don't care what the status of the case is then you can remove those checks completely.
For this problem, I see you needing 2 triggers. The first one would be for when a cases is inserted/updated :
and another trigger for when an opportunity is inserted, for situations where there is already a case related to its account:
I hope these help you.
All Answers
For now, please consider that Account has one case and one opp only.i.e. it will be aone to one relstionship.
For this problem, I see you needing 2 triggers. The first one would be for when a cases is inserted/updated :
and another trigger for when an opportunity is inserted, for situations where there is already a case related to its account:
I hope these help you.
Hi,
I tried these two triggers but I just have a one question:
Like you have used
if (c.Status == 'Closed' || c.Status == 'Sales' || c.Status == 'New').
But I have around 20 status, so giving 20 status like this is not good. So can it be dynamic?
I only did that because I thought you only wanted to execute the logic for those 3 conditions. If you don't care what the status of the case is then you can remove those checks completely.
Aweome :)
Thanks a ton.
Hi,
I know you can help me with this query.
Now as you mentioned I created two triggers.I have one more object oppcase which has Opporunity field.So relation is like
Account-> Opp
Account-> Case
Case-> oppcase
Opp-> oppcase
I want now that only update those Opp's which exists on Oppcase object.
ExamplE: Account ABCD has 6 opps and 1 case and that case has 4 opps.So i need to update only 4 opps not the all 6.
Hope you understand my question.Please let me know how to create trigger or modify the above two triggers.
Thanks a lot