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
Samadhan Sakhale 3Samadhan Sakhale 3 

Writing Trigger Logic in Apex Class

Hi All,
I’m new in salesforce but now I can write triggers but in salesforce forum I read that all logic should be in apex class so anyone help me improve my trigger? And how to call that apex in Trigger by passing trigger.new argument to that apex Class?
My Trigger is…
 
trigger UpdateprdctonAcc on Subscription__c (before insert)
{
                //Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'User Group Membership'].Id;
                Id recordTypeId = Schema.SObjectType.Subscription__c.getRecordTypeInfosByName().get('User Group Membership').getRecordTypeId();
    List<Subscription__c> subscList = new List<Subscription__c>();
 
    for (Subscription__c s : Trigger.new){
        if(s.RecordTypeId == recordTypeId)
            subscList.add(s);
    }
    String str;
   
    Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
    List<Id> listIds = new List<Id>();
    set<ID> cObjectID = new set<ID>();   //Making a set of Product ID's
    Map<ID, Account> updateMap = new Map<ID, Account>();
 
    for (Subscription__c s : subscList)
    {
        listIds.add(s.Company_Name__c);
     
        if(s.Membership_Type__c!= null)
        {
            cObjectID.add(s.Membership_Type__c);//takes the Lookup Record & Add that ID's in cObjectID set
        }
    }
    if(!cObjectID.isEmpty())
    {
        Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
        Acc = new Map<Id, Account>([SELECT id,Product__c,(SELECT ID,Membership_Type__c FROM Subscriptions__r) FROM Account WHERE ID IN :listIds]);
       
        for(Subscription__c s : subscList)
        {  
            if(cObjectMap.get(s.Membership_Type__c).Name != Null)
            {
                // fill the country name on Opportunity with Country Name on Country_Object__c
                String pro= cObjectMap.get(s.Membership_Type__c).Name;
                Account myacc = acc.get(s.Company_Name__c);
                if(myacc != null){ //always check for nulls to avoid null pointer exceptions
                    myacc.Product__c=pro;
                    updateMap.put(myacc.Id,myacc);
                }
            }
        }
        update updateMap.values();
    }
}
 
 Regards,
Sam
Best Answer chosen by Samadhan Sakhale 3
PratikPratik (Salesforce Developers) 
Hi Sam,

Try to send id of the record and then have a SOQL in the class to retrieve the other fields of that records by using the id.

Thanks,
Pratik

All Answers

PratikPratik (Salesforce Developers) 
Hi Sam,

You can pass the id of record you will get from Trigger.new and rest of the logic , you can write it in class.

Here is the smaple code:

Trigger:

trigger contactonuser on User(after update,after insert) {

    for (user u:trigger.new) {
    
    createcononuser.conuser(u.id);
    
    }

}

Class:

global class createcononuser {

@future

    public static void conuser(id userid) {
    
    user u=[select id, firstname,lastname,email,username,phone from user where id=:userid];
       contact c= new contact();
       c.firstname=u.firstname;
       c.lastname=u.lastname;
       c.email=u.email;
       c.description=u.username;
       c.phone=u.phone;
       c.accountid='0019000000Uyk7P';
       insert c;
    
    }

}

Hope this will help you to get started.

Thanks,
Pratik
 
Samadhan Sakhale 3Samadhan Sakhale 3
hi pratik,
     Thanks for reply but i have quetions of sending all recrords in apex so i uwrite trigger like this
trigger SubscriptionTrigger on Subscription__c (After insert,after update) 
{
    SubscriptionTriggerHandler handler = new SubscriptionTriggerHandler();
    handler.OnAfterInsert(trigger.new);
}

but it gives compile time error...
Method does not exist or incorrect signature: [SubscriptionTriggerHandler].OnAfterInsert(LIST&lt;Subscription__c&gt;)

Thanks,
Sam
PratikPratik (Salesforce Developers) 
Hi Sam,

Try to send id of the record and then have a SOQL in the class to retrieve the other fields of that records by using the id.

Thanks,
Pratik
This was selected as the best answer
Samadhan Sakhale 3Samadhan Sakhale 3
thank a lot pratik 
PratikPratik (Salesforce Developers) 
Glad it helped!

Thanks,
Pratik