0

I have written one class and inside that there is method which calling one small method ,what i need is to call inside method based on Insert and update

If Operation is INSERT then call one method if Operation is UPDATE then call other method.

how to get the context from trigger to class-method and check if its insert or update

   public static void assignLicense(List<user> usr) {
    //find the PackageLicense Id

    if(pl.size()>0){
        System.assert(pl != null, 'PackageLicense cannot be null.');
        if(trigger.isinsert)
        List<User> usersToAssignLicenses = getUsers(usr);
        If(trigger.isupdate){
        }
          List<User> usersToAssignLicenses = getUsers2(usr);
        }

    static List<User> getUsers(list<user> usr){
    List<User> matching= new List<User>();
    Set<Id> Ids = new Set<Id>();
    for(user u:usr) {
        Ids.add(u.id);
    }
    matching = [SELECT Id FROM User WHERE id in:ids and profile.name='Internal User'];
    return matching;
}

static List<User> getUsers2(list<user> usr){
    List<User> matchingUsers = new List<User>();
    Set<Id> Ids = new Set<Id>();
    for(user u:usr) {
        Ids.add(u.id);
    }
    matching = [SELECT Id FROM User WHERE id in:ids and profile.name='Sales User'];
    return matching;
}
1

If you will be using this class as trigger handler, then you can access the context variables inside your handler class as well.

You can call your method based on the context variables

if(Trigger.isInsert){
    callMethodOne();
} else if (Trigger.isUpdate){
    callMethodTwo();
}

Likewise, you can use rest other context variables as well in your handler class.

If you don't want to use the context variables in handler class, then you can pass the values of context variables to your handler method from trigger.

MethodCalledFromTrigger(List<user> usr, boolean isInsert, boolean isUpdate){
    if(isInsert){
        CallMethodOne();
    } else if (isUpdate){
        CallMethodTwo();
    }
}

And from trigger, you can pass the context variables as parameters to your method

ClassName.MethodCalledFromTrigger(Trigger.New, Trigger.isInsert, Trigger.isUpdate);

You can go through context variables documents to get further information this.

  • Thanks very much,you solved my problem – Sandy May 30 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.