2

I have what seems to be a simple cross object field update. When the Operational Status on the Parent (Account) is updated, I want the value of the Contract Status field on the Child (Master Contract) to be updated to a corresponding value.

I need to match the Parent to the Child on Acc.Long_Account_Id__c = MC.Account_Id__c (it's a one-to-one relationship)

Here is what I've come up with thus far:

trigger UpdateStatus on Account (after update) {  
 List<Master_Contract__c> mcs = new List<Master_Contract__c>();  
 Set<Master_Contract__c> mcSet = new Set<Master_Contract__c>();  
 Map<ID, Master_Contract__c> mcMap = new Map<ID,Master_Contract__c>(); 
     for (Account acc: Trigger.new)
     {
      if(acc.Operational_Status__c =='Operational') 
      {
      mcSet.add(acc.Long_Account_Id__c);
      }
     }    mcs = [Select Account_Id__c, Contract_Status__c from Master_Contract__c where Account_Id__c n :mcSet];
     for(Account acc: Trigger.new)  
      {
        for(Master_Contract__c mc:mcs)
        {
         if(acc.Long_Account_Id__c==mc.Account_ID__c && acc.Operational_Status__c =='Operational')
           mc.Contract_Status__c = 'Active';
         }
        } 
       if(mcs.size() >0)
       update mcs;  }

I am currently getting this error: Error: Compile Error: Incompatible element type String for collection of SOBJECT:Master_Contract__c at line 9 column 6

Suggestions?

1

Here is the sort of code I would write (updated after added comment):

trigger UpdateStatus on Account (after update) {
     Map<Id, String> requireds = new Map<Id, String>();
     for (Account acc: Trigger.new) {
         Account old = Trigger.oldMap.get(acc.Id);
         if (old.Operational_Status__c != acc.Operational_Status__c) {
             String s;
             if (acc.Operational_Status__c == 'Operational') {
                 s = 'Active';
             } else if (acc.Operational_Status__c == 'Cancelled') {
                 s = 'Terminated';
             } else {
                 s = 'Pending';
             }
             requireds.put(acc.Long_Account_Id__c, s);
         }
     }
     if (m.size() > 0) {
         List<Master_Contract__c> updates = new List<Master_Contract__c>();
         for (Master_Contract__c mc : [
                 Select Id, Contract_Status__c, Account_Id__c
                 from Master_Contract__c
                 where Account_Id__c in :requireds.keySet()
                 ]) {
             String required = requireds.get(mc.Account_Id__c);
             if (mc.Contract_Status__c != required) {
                 mc.Contract_Status__c = required;
             }
             updates.add(mc);
         }
         update mcs;
    }
}

The above code assumes that Long_Account_Id__c is an ID-based reference; if it is a text field make the collection data type String instead.

  • The second solution seems to work best. But now that I've changed the structure of the query, I'm not sure how to add multiple parameters. For example, if the Operational status = Operational, The Contract Status = Active; if the Operational Status = Cancelled, the Contract Status = Terminated. All other Operational Statuses = Contract Status = Pending. – Robin L Dec 1 '14 at 18:02
  • @RobinL I've updated the answer to create a map of the account id and the required contract status. The query then can't be as specific so more has to be done in Apex code. – Keith C Dec 1 '14 at 19:59
0

It looks as though the error is with the line
mcSet.add(acc.Long_Account_Id__c).

Your "mcSet" is a set of Master_Contract__c objects, however "acc.Long_Account_Id__c" which is of type Id, not a Master_Contract__c.

Since you don't use "mcSet" anywhere else I believe changing its type from
Set < Master_Contarct__c > to Set < Id > would fix the error you were receiving and still allow it to be used properly in the following piece of code:

mcs = [Select Account_Id__c, Contract_Status__c from Master_Contract__c where Account_Id__c IN :mcSet]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.