0

I have Two objects. In both objects I have Holdback field whose datatype is check-box. Whenever I check or uncheck field of Object 1, field of Object 2 should check or uncheck respectively. But field from Object 2 should be read-only. I want a trigger for same.

trigger CheckHoldBack on dealer__Sales_Up__c (after insert,after update) {

    List<dealer__Vehicle_Inventory__c> objSalesUp =[SELECT Id, Name, dealer__Holdback__c FROM dealer__Vehicle_Inventory__c ];
    for(dealer__Sales_Up__c s: Trigger.new){
        System.debug('***********HoldBack********'+s.Holdback__c);    
        if(s.Holdback__c == true){
            System.debug('********HoldBack********'+s.Holdback__c);
            s.dealer__Desired_Stock_Num__r.Holdback__c = true;
        }
        else{
            s.dealer__Desired_Stock_Num__r.Holdback__c = false;
        }

    }
}
2
  • Ritesh you are nowhere updating the record, try adding the Trigger.New instance in the list of dealer_sales_up__c and update it outside for loop. Only question is you want to make 2nd object's field readonly at profile or page layout level? Dec 30, 2015 at 8:30
  • Hey @Ritesh. I think logic will be like if all the child Holdback__c false then only update parent Holdback__c to false. ? let me know if I am correct?
    – Ratan Paul
    Dec 30, 2015 at 9:17

1 Answer 1

1

Based on my understanding of your problem, you want to check the Holdback field on the parent if the same field on the child is checked? So basically you just need to add the parent record with the updated value in a List or Set and make an update on it.

trigger CheckHoldBack on dealer__Sales_Up__c (after insert,after update) {

    List<dealer__Vehicle_Inventory__c> vehicleInventorySet= 
                  new List<dealer__Vehicle_Inventory__c>();

    for(dealer__Sales_Up__c s: Trigger.new){

    dealer__Vehicle_Inventory__c temp = 
            new dealer__Vehicle_Inventory__c(id=s.dealer__Desired_Stock_Num__c,
                                             Holdback__c=s.Holdback__c);        
    vehicleInventorySet.add(temp);  

    }


   update vehicleInventorySet;
}

Edit

For making the field readonly, you would have to do it on a PageLayout level, since basically you need the user to have profile level permissions to edit it.

3
  • 1
    DML requires SObject or SObject list and also if(vehicleInventorySet.size()>0) not needed.
    – Ratan Paul
    Dec 30, 2015 at 9:05
  • Thanks @Ratan. Changed the set to List. But not sure how his logic would handle multiple child records with different checkbox values. Anyway that is for him to work out.
    – AslamK
    Dec 30, 2015 at 9:14
  • Yes @AslamK. I think logic will be like if all the child Holdback__c false then only update parent Holdback__c to false.
    – Ratan Paul
    Dec 30, 2015 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .