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
D ROKAD ROKA 

Help with trigger

Below is the code for the trigger but i am getting error 

"Error: Compile Error: expecting a semi-colon, found 'Idval' at line 2 column 8"

 

trigger onUpdateOfObjectB on Object_B__c (Before insert,before update) {
set Idval=new set();
set Ids=new set();

for (Object_B__c ct: trigger.new)
{
idval.add(ct.Lookup_Object_A__c);
ids.add(ct.Id);
}

List Aclist=[select Id,Priority__c from Object_A__c where Id =:Idval];

List Bclist=new List();
List Cclist=[select Lookup_Object_B__c,Status__c from Object_C__c where Lookup_Object_B__c=:ids];

for(Object_A__c ac: Aclist)
{
for(Object_C__c cc: Cclist)
{
if(ac.Priority__c=='P1' && cc.Status__c=='Closed')
{
System.debug('SDFGsdf sdfsdf');
cc.addError('Your custom error message');
}
}
}
}

 

Can anybody help me its urgent...

liron169liron169
Hello,

you need to define the object inside the set. e.g.:

Set<String> strSet=new Set<String>();
Set<Account> accSet=new Set<Account>();
qraishqraish

How about trying key value pair from Map instead?

 

trigger onUpdateOfObjectB on Object_B__c (Before insert,before update) {
map<id,string> newMap = newMap<id, string>;

for (Object_B__c ct: trigger.new)
{
newMap.put(ct.id,ct.lookup_Object_A__c);
}
List Aclist=[select Id,Priority__c from Object_A__c where Id =:newMap.keyset()];
List Cclist=[select Lookup_Object_B__c,Status__c from Object_C__c where Lookup_Object_B__c=:newMap.values()];

for(Object_A__c ac: Aclist)
{
for(Object_C__c cc: Cclist)
{
if(ac.Priority__c=='P1' && cc.Status__c=='Closed')
{
System.debug('SDFGsdf sdfsdf');
cc.addError('Your custom error message');
}
}
}
}

sfdcfoxsfdcfox

You didn't specify a type for your Set objects or List objects. Collections require a data specialization in order to operate. Regardless, your code is still not optimized, as you're using an unfiltered loop inside an unfiltered loop. Instead, you're looking to use maps:

 

trigger onUpdateOfObjectB on Object_B__c (before update) {
    Map<Id, Object_A__c> objectAMap = new Map<Id, Object_A__c>();
    
    for(Object_B__c record: Trigger.new) {
        objectAMap.put(record.Lookup_Object_A__c, null);
    }
    
    objectAMap.remove(null);
    objectAMap.putAll([SELECT Id, Priority__c FROM Object_A__c WHERE Id IN :objectAMap.keySet()]);

    for(Object_C__c record:[SELECT Id, Lookup_Object_B__c, Status__c FROM Object_C__c WHERE Lookup_Object_B__c IN :Trigger.new]) {
        if( record.Status__c=='Closed' &&
            objectAMap.containsKey(Trigger.newMap.get(record.Lookup_Object_B__c).Lookup_Object_A__c) &&
            objectAMap.get(Trigger.newMap.get(record.Lookup_Object_B__c).Lookup_Object_A__c).Priority__c=='A1') {
            Trigger.newMap.get(record.Lookup_Object_B__c).addError('Your custom error message.');
        }
    }
}

Some other observations I found while fixing the code:

 

  • Since you're looking for an Object_C__c status, it makes no sense to run this trigger "before insert", because Object_C__c can't possibly have a lookup to Object_B__c at this point in time due to the fact that Object_B__c records wouldn't yet have an ID to lookup.
  • The Set<Id> variables you defined were unnecessary, and in the case of the Set<ID> for Object_B__c, redundant, since you already have this set: Trigger.newMap.keySet().