+ Start a Discussion
Danny HartleyDanny Hartley 

Help with cross-object trigger

Hello Everyone,

I am trying to create a trigger that updates a picklist field on the Account object whenever that account has a Par_Level__Line__c created for a particular family of products.  The custom object, Par_Level_Lines__c, has a lookup relationship to the Account object.  The custom field on account is named Allocation_Review_Week__c.  Here is the trigger code I have written so far:

 
trigger AllocationReviewWeek on Par_Level_Lines__c (after insert){
    List <ID> AccountsID = new List<ID>();
    for(Par_Level_Lines__c o : Trigger.new){
        if(o.Product_Family__c = 'Our Products' && Par_Level_Lines__c.Account__c.Allocation_Review_Week__c == null){
            AccountsID.add(Par_Level_Lines__c.Account__c.ID);
        }
    }
    List<Accounts> AcctUpdate = [Select id, Par_Level_Lines__c.Account__c FROM Account Where id in : AccountsID];
        for(integer i = 0 ; i < AcctUpdate.size(); i++){
            AcctUpdate[i].Par_Level_Lines__c.Account__c.Allocation_Review_Week__c = "1";
        }
        update AcctUpdate;
}

I keep hitting a compiler error on the 8th line of code.  Thanks in advance for any help.
Best Answer chosen by Danny Hartley
rajat Maheshwari 6rajat Maheshwari 6
Hi Danny,

Please use simple code snippet as below, It would definately help you :) 
 
trigger AllocationReviewWeek on Par_Level_Lines__c (before insert){
    Set <ID> AccountsID = new Set<ID>();
   
 for(Par_Level_Lines__c o : Trigger.new)
  {
        if(o.Product_Family__c = 'Our Products' )
        {
            AccountsID.add(o.Account__c);
        }
    }


    Map<Id,Account> AcctUpdate = new Map<Id,Account>([Select id, Allocation_Review_Week__c FROM Account Where id in : AccountsID And Allocation_Review_Week__c = null ]);
       
for(Par_Level_Lines__c o : Trigger.new)
   {
       if(AcctUpdate!=null && AcctUpdate.containsKey(o.Account__c))
          {
             AcctUpdate.get(o.Account__c).Allocation_Review_Week__c = "1";
          }
    }

if(AcctUpdate!=null && AcctUpdate.values()!=null)
   update AcctUpdate.values();
        
}


I am assuming "Allocation_Review_Week__c" is text field, that's why we have given value : - "1" 

If it is number field then please remove quote, i.e 1

Mark as best answer, if it works
Thanks

All Answers

Nishad KNishad K
Hi Danny.
try the below code :
 
trigger AllocationReviewWeek on Par_Level_Lines__c (after insert){
    List <ID> AccountsID = new List<ID>();
    for(Par_Level_Lines__c o : Trigger.new){
        if(o.Product_Family__c = 'Our Products' && Par_Level_Lines__c.Account__c.Allocation_Review_Week__c == null){
            AccountsID.add(Par_Level_Lines__c.Account__c.ID);
        }
    }
    List<Accounts> AcctUpdate = [Select id, Allocation_Review_Week__c FROM Account Where id in : AccountsID];
        for(integer i = 0 ; i < AcctUpdate.size(); i++){
            AcctUpdate[i].Allocation_Review_Week__c = "1";
        }
        update AcctUpdate;
}
let me know the outcomes .
Regards,
rajat Maheshwari 6rajat Maheshwari 6
Hi Danny,

Please use simple code snippet as below, It would definately help you :) 
 
trigger AllocationReviewWeek on Par_Level_Lines__c (before insert){
    Set <ID> AccountsID = new Set<ID>();
   
 for(Par_Level_Lines__c o : Trigger.new)
  {
        if(o.Product_Family__c = 'Our Products' )
        {
            AccountsID.add(o.Account__c);
        }
    }


    Map<Id,Account> AcctUpdate = new Map<Id,Account>([Select id, Allocation_Review_Week__c FROM Account Where id in : AccountsID And Allocation_Review_Week__c = null ]);
       
for(Par_Level_Lines__c o : Trigger.new)
   {
       if(AcctUpdate!=null && AcctUpdate.containsKey(o.Account__c))
          {
             AcctUpdate.get(o.Account__c).Allocation_Review_Week__c = "1";
          }
    }

if(AcctUpdate!=null && AcctUpdate.values()!=null)
   update AcctUpdate.values();
        
}


I am assuming "Allocation_Review_Week__c" is text field, that's why we have given value : - "1" 

If it is number field then please remove quote, i.e 1

Mark as best answer, if it works
Thanks

This was selected as the best answer
rajat Maheshwari 6rajat Maheshwari 6
Hi Danny,

Does your issue get solved ?  please let me know

Thanks
Danny HartleyDanny Hartley
This code resulted in this error:

trigger AllocationReviewWeek on Par_Level_Lines__c (after insert){
    List <ID> AccountsID = new List<ID>();
        for(Par_Level_Lines__c o : Trigger.new){ 
            if(o.Product_Family__c = 'Our Products' && Par_Level_Lines__c.Account__c.Allocation_Review_Week__c == null){
                AccountsID.add(Par_Level_Lines__c.Account__c.ID);
            }
        }
    List<Accounts> AcctUpdate = [Select id, Allocation_Review_Week__c FROM Account Where id in : AccountsID];
        for(integer i = 0 ; i < AcctUpdate.size(); i++){
            AcctUpdate[i].Allocation_Review_Week__c = "1";
        }
        update AcctUpdate;
}

Error: Compile Error: line 10:54 no viable alternative at character '"' at line 10 column 54

After changing the quotations to apostrophes I received this error:


Error: Compile Error: line 10:54 no viable alternative at character '"' at line 10 column 54


 
Danny HartleyDanny Hartley
I apologize, the second error from my comment above is:

Error: Compile Error: Invalid type: Accounts at line 8 column 10
Danny HartleyDanny Hartley
@rajat

Thanks, that did what I needed!
 
rajat Maheshwari 6rajat Maheshwari 6

@Danny, Please feel free to contact me in case of any issue on gmail : - rajatzmaheshwari@gmail.com

 

Thanks