1

I have 3 custom object "Project"(Parent), "Block"(Child), "Flat"(child of block). In Flat object i have a field called "Status" which is picklist values('Available', 'Block', 'Sold').

I have one more custom object called "Allocate" here I will be selecting "Flat" whose status is "Available" for that i have given a look-up filter to show only available flats.

there is one more field called "Status" in allocation Object(with same values of Flat__c.Status__c, field values) if i select status has "Block" it should change status in that "Flat" Object for that i have written trigger

trigger Field_update on Flat_Allocation__c (after update) {

    set<id> st= new set<id>();

    for(Flat_Allocation__c  fa:trigger.new){

        st.add(fa.Flat__c);

        for(Flat__c F: [Select id, Flat_status__c, Super_Built_Up_Area__c 
                          From Flat__c 
                         Where id= :st]){

            // for(Flat_Allocation__c  Fa1:[Select id, Flat_status__c, Flat__c 
                                              From Flat_Allocation__c 
                                             Where Flat__c = :st]){
                if(fa.Flat_status__c != trigger.oldmap.get(Fa.id).Flat_status__c){
                    f.Flat_status__c = fa.Flat_status__c;
                // }
                update f;
            }
        }
    }
}

If I remove Look up filter Trigger works fine, if filter is there it shows error Error:Value does not exist or does not match filter criteria. (Related field: Flat)

2 Answers 2

2

According to your criteria you cannot do this, Why:

  1. Your lookup filter is to only in include Flats that have a status of available.
  2. In your trigger you are changing the Flat Status to Block (per your example)
  3. Thus when you update the flat status it is no longer meeting your lookup field criteria and the after update to allocation throws an error.
0

Solution is easy - I suspect you get also objects in wrong status. Try to change you query.

Additionaly : This code need serious update. I am not totally sure what you want to achieve... Please tell us more about relation beetwene flat and
But mayby this will help you

trigger Field_update on Flat_Allocation__c (after update) {

    set<id> st= new set<id>();
    Flat_status__c[] flatStatusToUpdate = new Flat_status__c[]{};

    for(Flat_Allocation__c  fa:trigger.new){
       st.add(fa.Flat__c);
    }

    for(Flat__c flat:[Select id, Flat_status__c,Super_Built_Up_Area__c from Flat__c where id=:st AND Status__c = 'Avalible']){
        /** here put your actions for flat and add object you want update to list **/
    }

    if(!flatStatusToUpdate.isEmpty()){
        update flatStatusToUpdate;
    }
}

As you homework please read about code best practice :)

1
  • Can you provide any links to your recommended reading?
    – Adrian Larson
    Feb 15, 2016 at 21:25

You must log in to answer this question.

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