I'm trying to delete an attachment from a case after the case is closed. The trigger should only fire when a custom check box on the case object is checked.
You can write an After Update trigger on the Case object, and check inside the trigger the two conditions (Case.isclosed and CustomCheck = true ), then query on the attachments whose parentid match with the id of this case. Then Database.delete the attachment list which you queried above. Hope this helps !
EDIT : Ofcourse you must bulkify your triggers as a best practice.
In trigger you should get id records who have selected check box.
When you have id's yours records run query like this:
SELECT Id ParentId FROM Attachment WHERE ParentId in: youIdList
and delete result of query.
Related
I created a Power Automate flow to create 9 tasks (Dev, Testing, and BA tasks) as a child for a new user story when it is assigned.
It works fine but it is getting duplicated every time when the story is assigned back from Unassigned to a resource.
I tried to get the count of related tasks for the user story before creating them but couldn't find any default conditions to get that.
Is there any other condition to set to avoid this duplicate task creations?
Solution for the query to set a condition to check for the existing workitems before creating them has been provided by yashag2255 on the Power Automate forums:
To get the work items related to the user story, you will have to send a HTTP request for that. But before sending this request -> Go to Azure DevOps -> Queries -> My Queries -> Create a new blank query and save it. (In this example let us say TestNew).
After you have done this, in the flow under the trigger create an action as shown. Very carefully populate all the fields. (here 'yashTest' is my project, replace it with yours)
Body : {
"name" : "TestNew",
"queryType" : "tree",
"wiql" : "SELECT [System.Id],[System.Title],[System.State] FROM workitemLinks WHERE([Source].[System.WorkItemType] = 'Task' AND [Source].[System.Id] = )AND ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward')AND ([Target].[System.WorkItemType] = 'Task')MODE (Recursive)"
}
in the "wiql" near the [System.Id] = select the ID of the work item retrived from the trigger action (the original trigger request that has the id of the assigned work item)
Now, create a new string variable to get the query ID and put the foloowing expression in the expression editor and add it to the string value.
body('Send_an_HTTP_request_to_Azure_DevOps')['id']
Now add another action: Get Query Results with the query ID in the previous step. This will fetch you a list of the work items related to the assigned work item. You can iterate thorough this to check if the items exist and if not create them with Create Work Item Action.
Source: https://powerusers.microsoft.com/t5/Building-Flows/How-to-set-conditions-to-avoid-duplicate-Tasks-creation-for-a/td-p/289629
I need help on how to create the following workflow please. I added a new body field in the NetSuite sand box called “sow.” It is a check box field and I added it to the sales order transaction page. Is it possible to make a formula that says: If “contract type” field that exists on opportunity equals “sow”, check box, if not don’t check box? So the field contract type is on the opportunity and once this is contract type is set to SOW the checkbox SOW on the opportunity should be checked.
Your workflow will need a single State. The State will have a single Set Field Value Action, which will mark the checkbox accordingly. This Action will have a Condition that looks at the value of the Contract Type field.
New Workflow
Name the Workflow
Give it an ID
Record Type: Transaction
Sub Types: Sales Order
Trigger Ttype: Before Record Load
WorkFlow Condition: Condition
Field: SOW -- Compare Type: checked
Workflow Condition
I was trying out a simple trigger on Case Object.There is a field Hiring_Manager__c (looks up to User) in Case .On update or insert of a case, this field has to be populated with the Case Owner's Manager.I created the trigger as follows.It is not bulkified as I was just trying out for a single record.
I could see the value getting populated correctly in debug statements.But it is not updated on the record.
trigger HiringManagerupdate_case on Case (before insert,before update) {
Case updatedCaseRec= [select id,ownerId,Hiring_Manager__c,CaseNumber from case where id in :Trigger.new];
system.debug('Case Number'+updatedCaseRec.CaseNumber);
system.debug('Manager before updation'+updatedCaseRec.Hiring_Manager__c);
try{
User caseOwner = [Select Id,Name, ManagerId, Manager.Email From User Where Id = :updatedCaseRec.ownerId];
updatedCaseRec.Hiring_Manager__c=caseOwner.ManagerId;
}
Catch(DMLException de){
system.debug('Could not find Manager');
}
system.debug('Manager'+updatedCaseRec.Hiring_Manager__c);
}
I got a reply for this in developer's forum with a code that works fine.
trigger HiringManagerupdate_case on Case (before insert,before update) {
for(Case cas :Trigger.new){
try{
User caseOwner = [Select Id,Name, ManagerId, Manager.Email From User Where Id = :cas.ownerId];
cas.Hiring_Manager__c=caseOwner.ManagerId;
}
Catch(DMLException de){
system.debug('Could not find Manager');
}
system.debug('Manager'+cas.Hiring_Manager__c);
}
I would like to know
1)Difference in logic in both the approaches if I am trying to update only a single record?Mine is populating the value in debug statement but not on screen.
2)Will the select statement inside the loop (of second approach)affect governor limit?
3)Do we need to explicitly call update for before triggers? My understanding is the save and commit happens after the field update for before triggers and hence the changes will be saved automatically without calling update statement.?
Thank in advance.
Nisha
1) If you are trying to access the record(s) for the object which fired a before trigger, you must iterate over Trigger.new. You cannot query for it because the record(s) may not be in the database yet.
2) Yes it could. The query executes for every Case record being inserted or updated. If you have 1,000 such Cases, the query executes a 1,000 times.
3) No, we do not need to call update for before triggers. Your understanding is correct.
Suppose I have two objects
1.Account- standard object[it has a field name Status_c which is a picklist having value inprogress and closed]
2.Client_c - custom object[it also have same field name Status__c which is a picklist having value inprogress and closed]
and Client__c has lookup to Account name which means Account has a related list of client object .
My question is :
I want to write a trigger where if I put account status to "closed" I can not put client status to "closed",it should throw an error message on client object or if I put client status to closed I can not put account status to closed vice versa.
Can any one please help me to write a trigger on this??
Conceptually, I think what you are looking to do is set up Validation Rules on both of those objects. Your validation rule on Client_c should be pretty simple: TEXT(Status_c) == 'Closed' && TEXT(Account_c.Status_c) == 'Closed'
The more interesting piece is how you handle making sure none of your related items are Closed when you move the Account to Closed. I tend to prefer creating a field on the Account that keeps track of the status of the related items (checkbox) that basically tells me whether it is valid for me to change my status or not. In this case, the validation rule becomes pretty simple. In order to set that boolean value, I end up using a Trigger on Client__c that basically just grabs all the Accounts when a Client is being modified in the batch (taking into account both inserts, upserts, and deletes):
SELECT Account__c.Id FROM Client__c WHERE Id =: Trigger.new OR Id =: Trigger.old
Then create a Set of all the Account Ids (in this example, named accounts), and run a query to retrieve ALL Clients related to those Ids (in a single query to ensure you don't hit SOQL limits).
SELECT Account__c.Id, Status__c FROM Client__c WHERE Account__c.Id =: accounts
From the results of this, you will iterate over all of the entries, tossing them into a Map keyed by the Account Id where the value is a List of Clients. When you are done, run a query to get all accounts based on the "accounts" list from earlier (which was just a list of strings, not actual Accounts), subsequently iterate over all the Clients associated with that Account, and if a Client is marked as Closed, you will update the metadata of that Account accordingly. If no Clients are closed, the Account will be marked as such. Once you are finished, run an update statement to update the list of Accounts that you have modified.
I would really appreciate if someone can guide me to check if a particular field is included in update call inside a before/after update trigger. Many thanks.
All fields are always present in the trigger regardless of whether they are dirty or not, to ascertain if a specific field has been modified you have to retrieve a previous version of the row using oldMap map which is a Map<ID, sObject> and compare the values in old and new. For example
trigger CaseOnParticularFieldUpdate on Case (before update) {
for (Case c: Trigger.new) {
Case oldCase = Trigger.oldMap.get(c.ID);
if (c.Field != oldCase.Field) {
// field was updated, do some magic here
}
}
}
Trigger will include all fields of that sobject for which it is invoked. You can check previous(old) value and current(new) value of any field in that object and can compare it and can do the operation accordingly.
Hope it helps you.