0

I am new to Scaleforce. I created a new app, added three custom objects (Client, Property, Client_Property) and created tabs for them. So now CRUD is working correctly but I want to change Available field in the Property object to false (type of this field is boolean) when adding a new record to the Client_Property object using a trigger in the Client_Property object.

Design of trigger seems to me like that:

  1. Get the Property Id field from the Property field in Client_Property object and save in some variable (id_clpr).
  2. Make SQL request like this: SELECT Available FROM Property WHERE id_clpr = Propery Id.
  3. And then change the field to false and somehow commit it.
trigger ChangeAvailable on Client_Property__c (before insert) {
    //initialize variable to store Property Id

    //find Property field
    for(Client_Property__c cp : Trigger.New){
        //get Property Id from Property field
    }

    //Make SQL request to Property object, get appropriate record, change value of
    //Available field to false
}

Could you suggest solution, please?

enter image description here

  • 1
    So what's the question? – Pranay Jaiswal Nov 28 '19 at 21:21
  • 1
    You seem to be on the right track with this solution. Have you completed the Apex Triggers module on Trailhead (particularly the critical second module on bulk triggers)? I think that will get you started on the actual implementation. – David Reed Nov 28 '19 at 21:24
  • In the Client_Property object the field is not an actual Property Id like in the Property object. I think I need to get Id so that I would be able to execute SQL request to find specific record. And also I don't now how to commit update in the Property object in the trigger. David, thx for the link. – bossonog Nov 28 '19 at 21:33
1

I think there's an easier way to do this, if you're interested in avoiding Apex code.

Client_Property__c is what's called a junction object; it has master-detail relationships to two other objects. Because it is the child, you have declarative tools that can help you traverse the relationship to the parent and make updates, or automatically roll-up child details to the parent.

If I can rephrase your objective as "Any Property which has at least one Client Property child object should have the Available field set to False", you can achieve it elegantly like this:

  1. Create a Roll-Up Summary Field on Property. Call it, say, Count_Client_Properties__c. Configure it to COUNT the Client Property objects, with no criteria. At your option, you may choose not to display this field on the page layout - it may or may not be relevant to users.
  2. Replace the Available checkbox with a formula field whose data type is Checkbox. Use the formula Count_Client_Properties__c > 0.

Then, the system will take care of showing the correct true/false value for you - no code required.

You can write an Apex trigger to do this if you want to, but it's a much heavier-weight solution. You'll need to complete some core Trailhead modules, like

to learn how to write SOQL queries and DML data manipulation statements. Additionally, you will be required to write and pass an Apex unit test for the trigger providing at least 75% code coverage.

|improve this answer|||||
  • Available field is not displayed now. – bossonog Nov 28 '19 at 23:09
  • @bossonog I don't know what you mean by that. Did you create a new field? Did you add it to the page layout? – David Reed Nov 29 '19 at 17:37
  • I created the trigger, so problem is solved. Thank you! – bossonog Nov 29 '19 at 19:41

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.