function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DoctorCodeDoctorCode 

Variable does not exist: c.id

hello all

 

im trying to update a lookup field in the acct with the case id but i keep getting the error variable does not exist, ay help will be appreciate 

 

by the way im new to this apex code and im kind rusty after 10 years i didn’t to programming at all

 

trigger updateAccountWithCase on Case (after insert) {
    set <ID> acc_ids = new set <id> ();
        for (case c : trigger.new)acc_ids.add(c.accountid);
        list<account> acct_to_update = [select id, case__c from account where id in :acc_ids];
            {
            for (account acc: acct_to_update) acc.case__c = caseid;
    update acct_to_update;
    }
}

DharmeshDharmesh

try this

trigger updateAccountWithCase on Case (after insert) {
        set<id>  acc_ids = new set<id>();
        for (case c : trigger.new){
            acc_ids.add(c.accountid);
        }    
        list acct_to_update = [select id,case__c from account where id in :acc_ids];
        for (account acc: acct_to_update){
            acc.case__c = caseid;
        }        
        update acct_to_update;    
}
DoctorCodeDoctorCode

Dharmesh

 

thanks a lot for your quick reply

 

i copy and past what you post and im getting this


Error: Compile Error: unexpected token: 'List' at line 6 column 8

 

 

thanks again for your help

DharmeshDharmesh

try this

trigger updateAccountWithCase on Case (after insert) {
        set<Id>  acc_ids = new set<Id>();
        for (case c : trigger.new){
            acc_ids.add(c.accountid);
        }    
        list<Account> acct_to_update = [select id,case__c from account where id in :acc_ids];
        for (account acc: acct_to_update){
            acc.case__c = caseid;
        }        
        update acct_to_update;    
}
DoctorCodeDoctorCode


Error: Compile Error: Variable does not exist: caseid at line 8 column 27

 

i got this one this time

rmehrmeh

Hi,

 

Try this out:

 

 

trigger Trigger1 on Case (after insert) 
{
set<id> acc_ids = new set<id>();
public String caseid{get;set;}
for (case c : trigger.new){
acc_ids.add(c.accountid);
caseid = c.Id;
}
list<Account> acct_to_update = [select id,case__c from account where id in :acc_ids];
for (account acc: acct_to_update){
acc.case__c = caseid;
}
update acct_to_update;
}

 But I wanted to clarify, is this a bulk enabled trigger?

If yes, whout should happen if the same accountId is associated with various cases???

 

Can you explain the scenario and purpose of your trigger??

 

I just hope, this is what you were looking out for.

 

 

sravusravu

Correct me If I am wrong in understanding your Scenario. From my understanding if you are trying to update the field on the Account object after the case is inserted try the following code

 

trigger updateAccountWithCase on Case (after insert) {   
    public Id caseId;
    public Id aId;
    for(Case c : Trigger.new){
        caseId = c.Id; //get the case Id of the new inserted record
    }
    List<Case> cList = [select Account__c from case where Id =:caseId];
    for(Case accCase : cList){
        aId = accCase.Account__c; //get the account Id of the new case
    }
    List<Account> acc = [select Id from Account where Id=:aId];
    for(Account a : acc){
       
//update the field you wish to update on the Account object for example a.<fieldname> = <some value>
        update a;
    }
}

 

 

Hope this help you..........