Reply
Regular Contributor
Developement
Posts: 31
0
Accepted Solution

Help with Triggers

I have 4 objects and their relatinoship is like this:

Case-> Account

Opportunity->Account

Case->Caseopp

Caseopp-> Opportunity

 

I have created a new field on Opportunity as  " Test". 

what am trying to do is I need to copy the status of Case on this field "Test" only on those Opportunity which exists on Caseopp by trigger.

 

Can anyone help me with this?

 

Trusted Contributor
Madhan Raja M
Posts: 206
0

Re: Help with Triggers

Try this code:

 

trigger UpdateStatus on Case(before insert, before update) 
{
  for ( Case c : Trigger.new )  
     {     
caseopp__c co= [select opportunity__c from Caseopp__c where id = :c.Caseopp__c];             
        opportunity o= [select test__c from opportunity where id = :co.opportunity__c];
        o.test__c=c.status; 
        update o;
     }  
}

 

Madhan Raja M

Regular Contributor
Developement
Posts: 31
0

Re: Help with Triggers

Hi,

This trigger gives me an error:

 

Error: Compile Error: Invalid field CaseOpp__c for SObject Case at line 5 column 97

 

 

FYI: CaseOpp is an junction object between Case and Opp

Regular Contributor
Developement
Posts: 31
0

Re: Help with Triggers

And also I need to populate the Case status on Opportunity [Test field], so why are we creating trigger on Case?

Trusted Contributor
Madhan Raja M
Posts: 206
0

Re: Help with Triggers

[ Edited ]

Caseopp is a junction object and the API Name for Caseopp is CaseOpp__c.

 

Replace CaseOpp__c with your junction Object API Name.

 

If this doesn't solve your problem, I have a question for you.

Have you created Two lookups one for Case and the other for Opportunity in CaseOpp junction object? 

 

Madhan Raja M

Trusted Contributor
Madhan Raja M
Posts: 206
0

Re: Help with Triggers

[ Edited ]

Once you create/update the status in case record then the trigger will update the Test fields in opportunity with Case status.

 

Madhan Raja M

Regular Contributor
Developement
Posts: 31
0

Re: Help with Triggers

Yes the API name for Caseopp [Juntion object] is CaseOpp__c and I laready replaced it and I got the same error.

Yes I have created 2 lookups one for case and other for opportunity in CaseOpp junction object.

Trusted Contributor
Madhan Raja M
Posts: 206
0

Re: Help with Triggers

Try this out:

 

trigger UpdateStatus on Case(before insert, before update) 
{
    for ( Case c : Trigger.new )  
    {     
       Caseopp__c co= [select case__c, opportunity__c from Caseopp__c where case__c= :c.id limit 1];             
       opportunity o= [select test__c from opportunity where id = :co.opportunity__c];
       o.test__c=c.status; 
       update o;
    }  
}
Regular Contributor
Developement
Posts: 31
0

Re: Help with Triggers

I am still getting this error:

 


Error: Compile Error: Invalid field CaseOppAssociation__c for SObject Case at line 5 column 97

 

Trigger used is:

 

trigger UpdateStatus on Case(before insert, before update)
{
for ( Case c : Trigger.new )
{
CaseOppAssociation__c co= [select opportunity__c from CaseOppAssociation__c where id = :c.CaseOppAssociation__c];
opportunity o= [select Trigger__c from opportunity where id = :co.opportunity__c];
o.Trigger__c=c.status;
update o;
}
}

Trusted Contributor
Madhan Raja M
Posts: 206
0

Re: Help with Triggers

Paste this code and try, it should work:

 

trigger UpdateStatus on Case(before insert, before update)
{
    for ( Case c : Trigger.new )  
    {     
       CaseOppAssociation__c co= [select case__c, opportunity__c from CaseOppAssociation__c where case__c= :c.id limit 1];             
       opportunity o= [select test__c from opportunity where id = :co.opportunity__c];
       o.test__c=c.status;
       update o;
    }  
}