+ Start a Discussion
JustinWilliams2381JustinWilliams2381 

Missing something in my IF statement.

I have some code that was written by someone before me.  It checks to see if a field is a certain value and if so executes the trigger.  I am trying to modify it so that it checks to see if two fields are true before executing it.  We expanded our opportunities to include multiple record types and this old trigger only needs to fire with one specific record type. 

 

 

Here is the original If statement that works.

 

// Add to the list all opps associated with an agreement in the trigger that has been signed
for (echosign_dev1__SIGN_Agreement__c form: Trigger.New) {
if (form.echosign_dev1__Status__c == 'Signed') {
list_forms.add(form.echosign_dev1__Opportunity__c);
}
}

 

I try and update the If statement to validate two field before fireing.  The trigger is referencing an EchoSign object called "echosign_dev1__SIGN_Agreement__c".  The status field is a picklist that Adobe updates when a document is eSigned.  Company_Division__c is a formula field referencing a picklist on the related Opportunity object.  I have also tried making it a text field instead of a formula field in case the trigger didn't like referencing a formula field.

 

// Add to the list all opps associated with an agreement in the trigger that has been signed
for (echosign_dev1__SIGN_Agreement__c form: Trigger.New) {
if ((form.echosign_dev1__Status__c == 'Signed') && (form.company_Division__c == 'GradeBeam')){
list_forms.add(form.echosign_dev1__Opportunity__c);
}
}

bob_buzzardbob_buzzard

Formula fields are populated by the database when a record is retrieved, so I wouldn't expect that to work in a trigger unless you retrieved the record from the database again.

 

When you say you made it a text field - how did you populate that?  Also, what type of trigger is this (before insert, after upate?)

JustinWilliams2381JustinWilliams2381

I populated the text field with a simple field update. The value "GradeBeam" was in the text field when the trigger was suposed to check and fire.

 

The trigger is set AfterUpdate on the Agreement object.

bob_buzzardbob_buzzard

According to the order of execution, the workflow field update will happen after your trigger has completed, although this should cause triggers to fire again. Do you have anything that stops triggers firing more than once in a single transaction?

JustinWilliams2381JustinWilliams2381

Still new at Apex and this was written by a different person that I am trying to catch up to.

 

 

Here is the original code in its entirety

 

trigger oppUpdateWon on echosign_dev1__SIGN_Agreement__c (before insert) {

try {

// List of opportunity IDs
List<Id> list_forms = new List<Id>();

// Add to the list all opps associated with an agreement in the trigger that has been signed
for (echosign_dev1__SIGN_Agreement__c form: Trigger.New) {
if (form.echosign_dev1__Status__c == 'Signed') {
list_forms.add(form.echosign_dev1__Opportunity__c);
}
}

// Loop through all opportunities who were added to the list previously
for (Opportunity opp:[SELECT Id, Name, OwnerId, Bid_Date__c, Start_Date__c, Construction_Value__c, AccountId, Contact__c, Subscriber_Account__c, Subscriber_Contact__c, Scanning_Allowance_Included__c, Scanning_Provider__c, Project_City__c, Project_State__c, Project_Zip_Code__c, Certification__c, Instructions_for_Support__c, Subscription_Type__c,Annual_Sub_Start_Date__c,Annual_Sub_End_Date__c,Annual_Sub_Design_Projects__c,Annual_Sub_Construction_Projects__c FROM Opportunity WHERE Id in :list_forms]) {

// If annual subscription, copy annual subscription information to the account

// Else if not annual subscription, create project and copy opportunity information to the project

// Check to see if a project already exists that is related to the opportunity
List<SE_Project__c> list_projects = new List<SE_Project__c>();
for (SE_Project__c project:[SELECT Id FROM SE_Project__c WHERE Opportunity__c = :opp.id]) {
list_projects.add(project);
}
if (!(list_projects.size() > 0)) {

// If project does not already exist, create one
SE_Project__c proj = new SE_Project__c();
proj.Name = opp.Name;
proj.Bid_Date__c = opp.Bid_Date__c;
proj.Construction_Value__c = opp.Construction_Value__c;
proj.Opportunity__c = opp.id;
proj.Certification__c = opp.Certification__c;
proj.Project_City__c = opp.Project_City__c;
proj.Project_State__c = opp.Project_State__c;
proj.Project_Zip_Code__c = opp.Project_Zip_Code__c;
proj.Scanning_Included__c = opp.Scanning_Allowance_Included__c;
proj.Scanning_Service__c = opp.Scanning_Provider__c;
proj.Special_Instructions__c = opp.Instructions_for_Support__c;
proj.Sales_Rep__c = opp.OwnerId;
proj.Subscription_Type__c = opp.Subscription_Type__c;
proj.Activity_Status__c = 'Needs Project Coordinator';

// If opportunity does not have a start date already defined, then set start date = 7 days from today
if (opp.Start_Date__c <> null) {
proj.Start_Date__c = opp.Start_Date__c;
} else {
proj.Start_Date__c = System.today().addDays(7);
}

// If opportunity lists a subscriber, then add them to the project in the right location
if (opp.Subscriber_Account__c <> null) {
proj.Subscriber__c = opp.Subscriber_Account__c;
Account subscriber = [SELECT Id,Type FROM Account WHERE Id = :opp.Subscriber_Account__c];
if (subscriber.Type.contains('Architect')) {
proj.AE_Account__c = opp.Subscriber_Account__c;
proj.AE_Contact__c = opp.Subscriber_Contact__c;
} else if ((subscriber.Type.contains('General Contractor')) || (subscriber.Type.contains('Construction Manager'))) {
proj.GC_CM_Account__c = opp.Subscriber_Account__c;
proj.GC_Contact__c = opp.Subscriber_Contact__c;
} else if (subscriber.Type.contains('Owner')) {
proj.Owner_Account__c = opp.Subscriber_Account__c;
proj.Owner_Contact__c = opp.Subscriber_Contact__c;
}
}

// Add driver account from opportunity to the right location in the project
Account acct = [SELECT Id,Type FROM Account WHERE Id = :opp.AccountId];
if (acct.Type.contains('Architect')) {
proj.AE_Account__c = opp.AccountId;
proj.AE_Contact__c = opp.Contact__c;
proj.Who_Is_In_Charge__c = 'A/E';
} else if ((acct.Type.contains('General Contractor')) || (acct.Type.contains('Construction Manager'))) {
proj.GC_CM_Account__c = opp.AccountId;
proj.GC_Contact__c = opp.Contact__c;
proj.Who_Is_In_Charge__c = 'GC/CM';
} else if (acct.Type.contains('Owner')) {
proj.Owner_Account__c = opp.AccountId;
proj.Owner_Contact__c = opp.Contact__c;
proj.Who_Is_In_Charge__c = 'Owner';
}

// Create the new project
insert proj;

// Obtain email address for billing from the agreement object
opp.Email_Address_for_Billing__c = [SELECT Email_Address_for_Billing__c FROM echosign_dev1__SIGN_Agreement__c WHERE echosign_dev1__Opportunity__c = :opp.id].Email_Address_for_Billing__c;

// Obtain project number and add it to the opportunity
opp.Project_Number__c = [SELECT Project_Number__c FROM SE_Project__c WHERE Opportunity__c = :opp.id].Project_Number__c;

}

// Update opportunity to closed won
opp.StageName = 'Closed Won';
opp.CloseDate = System.today();
update opp;
}
} catch (Exception e) {Trigger.new[0].addError(e.getMessage());}
}

bob_buzzardbob_buzzard

This is a before insert trigger though, as shown in the signature on the first line.  This will run before the workflow has a chance to update the field from the related record.  I'd suggest you query back all the related records and look at the actual field value on that record as part of your if statement (or copy the value into the agreement record if you need it in the future).