function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SunadminSunadmin 

Proposed trigger for credit application

Hello,

 

I am working on an idea for a trigger and thought I would post this to the community before I get too far and realize it is not a worthwhile pursuit. Any feedback would be appreciated.

I am proposing a trigger that fires on a custom field to query email and two other custom fields “credit_line_request_Amount_c” and “credit_analysis_c” from Opportunity.

There are different stages to the trigger depending on the dollar amount in “credit_line_request_Amount_c” and if the potential customer was denied or approved per “credit_analysis_c”. Depending on the stage and status an email message will be sent out to the proper user.

I can say my first problem is that I am trying to query the email field from Opportunity. I am trying to talk the credit people into querying from Lead.

Here is my code so far, please excuse some of the pseudo code I have used and any syntax/compile errors, I am still in the planning stages.  

//APEX TRIGGER AND SOQL QUERY TO AUTOMATE PROCESS FROM CREDIT PERSON //DENY/APPROVE AND SEND EMAIL . MUST INCLUDE DATE/TIME OF APPROVE/DENY. 

Trigger denyapprove on Opportunity(after insert, before update){

    //resets trigger after each check
    Set Trigger<> = new Trigger{};
    for (Integer i=0; i<Trigger.new.size();i++) {

     //date time stamp for each check 
    for (credit_line_request_Email_c i : Trigger.new) {
                DateTime now = System.now();         
       
//SOQL Query for data
if(credit_line_request_Amount_c.size()!=0)
    List<Opportunity>=[SELECT credit_line_request_Amount_c, credit_analysis_c, Email    
    FROM Opportunity WHERE Record Type = Credit Approval);

//if stage 2 and credit denied  
if (credit_line_request_Amount_c()!<75,000) && (credit_analysis_c = 'deny';)
//this will need to be picklist field
    {
    //send email message to director
    Messaging.Email Message mail= new Messaging.Email Message();
    mail.setTemplate.(Credit Application Declined[0],Email);
    mail.setReplyTo.(director[0],Email);
    Messaging.sendEmail(new Messaging.Email Message[] {mail});
}
//if stage 2 and credit approved
if (credit_line_request_Amount_c()!<75,000) && (credit_analysis_c = 'approved';)
    {
    //send email message to applicant
    Messaging.Email Message mail= new Messaging.Email Message();
    mail.setTemplate.(approved[0],Email);
    mail.setReplyTo.(applicant[0],Email);
}   Messaging.sendEmail(new Messaging.Email Message[] {mail});
}

//if stage 4 and credit denied  
if (credit_line_request_Amount_c()!>=75,000 && <125,000) && (credit_analysis_c = 'deny';)
    {
    //send email message to director
    Messaging.Email Message mail= new Messaging.Email Message();
    mail.setTemplate.(Credit Application Declined[0],Email);
    mail.setReplyTo.(director[0],Email);
}   Messaging.sendEmail(new Messaging.Email Message[] {mail});
}
//if stage 4 and credit approved
if (credit_line_request_Amount_c()!>=75,000 && <125,000) && (credit_analysis_c = 'approved';)
    {
    //send email message to applicant
    Messaging.Email Message mail= new Messaging.Email Message();
    mail.setTemplate.(approved[0],Email);
    mail.setReplyTo.(applicant[0],Email);
}   Messaging.sendEmail(new Messaging.Email Message[] {mail});
}

//if stage 5 and credit denied  
if (credit_line_request_Amount_c()!>=125,000 && <200,000) && (credit_analysis_c = 'deny';)
    {
    //send email message to director
    Messaging.Email Message mail= new Messaging.Email Message();
    mail.setTemplate.(Credit Application Declined[0],Email);
    mail.setReplyTo.(director[0],Email);
}   Messaging.sendEmail(new Messaging.Email Message[] {mail});
}
//if stage 5 and credit approved
if (credit_line_request_Amount_c()!>=125,000 && <200,000) && (credit_analysis_c = 'approved';)
    {
    //send email message to applicant
    Messaging.Email Message mail= new Messaging.Email Message();
    mail.setTemplate.(approved[0],Email);
    mail.setReplyTo.(applicant[0],Email);
}   Messaging.sendEmail(new Messaging.Email Message[] {mail});
}

//if stage 6 and credit denied  
if (credit_line_request_Amount_c()!>=200,000 && <300,000) && (credit_analysis_c = 'deny';)
    {
    //send email message to director
    Messaging.Email Message mail= new Messaging.Email Message();
    mail.setTemplate.(Credit Application Declined[0],Email);
    mail.setReplyTo.(director[0],Email);
}   Messaging.sendEmail(new Messaging.Email Message[] {mail});
}
//if stage 5 and credit approved
if (credit_line_request_Amount_c()!>=200,000 && <300,000) && (credit_analysis_c = 'approved';)
    {
    //send email message to applicant
    Messaging.Email Message mail= new Messaging.Email Message();
    mail.setTemplate.(approved [0],Email);
    mail.setReplyTo.(applicant[0],Email);
}   Messaging.sendEmail(new Messaging.Email Message[] {mail});
}

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Personally, I'd scrap this and go with an Approval Process or use Workflow Rules. Faster to set up, easier to configure. Regardless, it looks like a reasonable train of thought code-wise. Minimize the code you need to write; if you need to do complex decisions where formulas and workflow fails you, then use Apex Code. Definitely leave the email notifications as workflow rules. You want the ability to turn on/off the emails, as necessary (i.e. testing).

All Answers

sfdcfoxsfdcfox

Personally, I'd scrap this and go with an Approval Process or use Workflow Rules. Faster to set up, easier to configure. Regardless, it looks like a reasonable train of thought code-wise. Minimize the code you need to write; if you need to do complex decisions where formulas and workflow fails you, then use Apex Code. Definitely leave the email notifications as workflow rules. You want the ability to turn on/off the emails, as necessary (i.e. testing).

This was selected as the best answer
SunadminSunadmin

Thanks sfdcfox, that makes more sense to me. This is actually one half of a process I am working on. I have already automated the other half with workflow rules, I was trying to see if there was a way to set this up using the trigger.