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
ronycronyc 

Error: Compile Error: Incompatible key type SOBJECT:Visit__c for MAP<Id,Visit__c>

Hi team,

 

I am writing a trigger to update a field in Account object(PVF_value__c) . Account is having a Detail called Visit__c which has 2 fields Location__c (Acount) and Primary_visit_focus__c .

 

So whenever a visit is created on a account it should update the field in Account.

Below is the code and the error i am getting.

 

trigger Acct_pvf_value on Account (after insert,after update) {
 for(Account acc :Trigger.new()){
List<String> IDpvf = new List<String>();
IDpvf.add(acc.ID);
Map<ID,Visit__c> VisMap =  new Map<ID,Visit__c>([Select Location__c,Primary_Visit_Focus__c from Visit__c where Location__c in :IDpvf ]);
for(Visit__c visit : VisMap.keyset())
{
if(acc.id == VisMap.get(visit).Location__c){
acc.PVF_value__c= VisMap.get(visit).Primary_Visit_Focus__c;
}
update IDpvf;
}
}
}

Error: Compile Error: Incompatible key type SOBJECT:Visit__c for MAP<Id,Visit__c> at line 9 column 19

Any help on this is highly appreciated

Avidev9Avidev9

May be you wanted to do

 

acc.PVF_value__c = VisMap.get(visit.id).Primary_Visit_Focus__c

 Earlier you just passed SObject instead of the Id 

ronycronyc

Error: Compile Error: Loop variable must be of type Id at line 6 column 14

 

trigger Acct_pvf_value on Account (after insert,after update) {
 for(Account acc :Trigger.new()){
List<String> IDpvf = new List<String>();
IDpvf.add(acc.ID);
Map<ID,Visit__c> VisMap =  new Map<ID,Visit__c>([Select Location__c,Primary_Visit_Focus__c from Visit__c where Location__c in :IDpvf ]);
for(Visit__c visit : VisMap.keyset())
{
if(acc.id == VisMap.get(visit.id).Location__c){
acc.PVF_value__c= VisMap.get(visit.id).Primary_Visit_Focus__c;
}
}
update IDpvf;
}
}

 

 

Avidev9Avidev9

so you wont say anything will just post the code ? hmmmm ?

 

This should be working

 

rigger Acct_pvf_value on Account(after insert, after update) {
    for (Account acc: Trigger.new()) {
        List < String > IDpvf = new List < String > ();
        IDpvf.add(acc.ID);
        Map < ID, Visit__c > VisMap = new Map < ID, Visit__c > ([Select Location__c, Primary_Visit_Focus__c from Visit__c where Location__c in : IDpvf]);
        for (Id visit: VisMap.keyset()) {
            if (acc.id == VisMap.get(visit.id).Location__c) {
                acc.PVF_value__c = VisMap.get(visit).Primary_Visit_Focus__c;
            }
        }
        update IDpvf;
    }
}

 

AshlekhAshlekh

 

 

Hi,

 

I think

 update IDpvf;

 is not updateable as this is list of string not a list of sobject.

 

And this trigger will fire after update then we cant able to update the accounts list if end user want this. Because in trigger we are updating the value of Account record .

 

Thanks.