Call existing trigger on Opportunity Update - triggers

Probably an easy answer I cannot find. I'm trying to call an existing trigger when the Opportunity Stage is set to 'Closed Won'. Not sure how to execute it. The existing trigger executes a Visualforce component.
trigger create_cc_project on Opportunity (after insert,after update)
{
if(Opportunity.stagename=='Closed Won')
then(cc.ccUtility.asyncInitiateProject('Opportunity', Opportunity.Id));
}

Related

Is there a way to do this inside Memgraph with triggers?

When calling a query module from a trigger statement, what I expected to get within a function was a state of the graph before the query changes occurred. However, this is not the case, and made changes are already visible in the graph state. I’ll write a dummy example of such behavior:
CREATE TRIGGER my_trigger ON CREATE
BEFORE COMMIT EXECUTE
CALL my_module.function(createdVertices, createdEdges) YIELD node, property
SET node.property = property;
Function my_module.function should execute after following statement:
MATCH (n {id: 19}), (m {id: 10}), (u {id: 14})
CREATE (n)-[new_edge_1:RELATION]->(m), (n)-[new_edge_2:RELATION]->(u)
In my_module.function, we’ll see a graph state with just created new_edge_1 and new_edge_2. If our calculation depends on having incremental changes, where we need the state of graph before any changes occur, is there a way to do it inside Memgraph with triggers?
Unfortunately, there is no way to do that with the triggers. During a single transaction, the changes of each query are visible in the next query. Triggers basically execute their statements as a query that is part of that transaction (if the BEFORE COMMIT is used).
You could probably use the changed values and ignore edges that are created in that transaction.

Trigger is running even if I create a new lead

As per the below code, this meant to update the first and last name only if I update a new lead but it is updating the first and last name of new lead as well
Can someone please suggest. Thank you!
trigger HelloWorld on Lead (before update) {
for(Lead l : Trigger.new){
l.FirstName = 'Hello';
l.LastName = 'World';
}
}
Do you have a workflow or process builder field update action on the lead object? If so it would trigger an update on the record that was newly inserted thus would fire the trigger.
So in lightning there is s default Lead assignment rule that is active, if you disable it, the update doesn't happen. But it is weird that the rule causes an update in Lightning but not in classic, so that might still be a bug that should be reported to Salesforce.

Apex Trigger on Case Object

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.

How to Maintain the quartz triggers history in data base after firing in java

i am usign SimpleTriggerBean and CronTriggerBean after executing that triggers i need to maintain the history of triggers for that i create table name is quartz_fired_triggred. How to store the detail of fired triggers in that particular table.
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobExecutionContext jobExeContext = new JobExecutionContext(scheduler, firedBundle, job);
Set the property JobDetail.Durable = true - which instructs Quartz not to delete the Job when it becomes an “orphan” (when the Job not longer has a Trigger referencing it).
Please note that the triggers are being deleted.
Reference : http://www.quartz-scheduler.net/documentation/faq.html

Salesforce- Trigger - Deleting object

Hello fellow code lovers, I am attempting to learn apex code and stuck on a problem. Since most of you guys here are avid code lovers and excited about problem solving, I figured I could ran by problem by.
I am attempting to Create a Trigger on an object called Book that does the following:
On Delete, all associated Chapters are also deleted
There is also an object named Chapter that is has a lookup to book.
Here is my attempt. This is my first ever attempt at apex so please be patient. Is anyone willing to dabble in this piece of code?
trigger DeleteChaptersTrigger on Book__c (before delete) {
List<Book__c> book = Trigger.old;
List<Chapter__c> chapters = new List<Chapter__c>();
Set set = new Set();
for (Book__c b :books){
if ()
}
}
You need to write all trigger with consideration that the trigger might be processing many records at any one time so you need to bulkify your trigger code.
Here are the variables that available on the trigger object.
You want to get all the record ids that will be deleted. Use the keyset method on the oldmap to get this info without looping and creating your own collection. Then you can just delete the records returned from the query.
trigger DeleteChaptersTrigger on Book__c (before delete) {
Set<string> bookids = Trigger.oldMap.keyset();
delete [SELECT Id FROM Chapter__c WHERE Book__c IN :bookids];
}
Apex code is unlike other languages where it gets confused with reuse of used words, like set as a variable name. Apex is also case insensitive.
Since you have full control, I recommend changing the way your custom objects are related to each other.
A chapter has no meaning/value without a book so we want to change the relationship between the two.
Remove the lookup on the Chapter object and replace it with a master-detail. When a master record gets deleted, Salesforce automatically deletes the detail related records. This is what you want, and without coding.