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
MattMet86MattMet86 

@invocablemethod - Pass variables to apex

I keep getting an error message when trying to save this code. 

Error: Compile Error: Illegal modifier on local variable at line 7 column 19

Code:
public class PackageLicenseDelete
{
    @InvocableMethod
    public static void licenseDelete()
    {
        @InvocableVariable(required=true)
		public ID UserId;
		@InvocableVariable(required=true)
		public String PackageID;
		
		//Use the UserId and PackageID to delete record from UserPackageLicense
   }
}

I need to pass two variables to the apex plugin so I can delete UserPackageLicense records. 

Thanks for any help,
Matt
 
MattMet86MattMet86
I got it working but I am not sure this is the best code as I have a SOQL statement in a loop. 
 
public class PackageLicenseDelete{
                 
    public class inputValues{
        @InvocableVariable(label='User ID' required=true)
        public string userId;
        @InvocableVariable(label='Package ID' required=true)
        public String packageId;
    }
     
    @InvocableMethod(label='Delete package license')
    
    public static void deletePackageLicense(List<inputValues> inputs) {
        for (inputValues i : inputs) {
            List<UserPackageLicense>  UPL = new List<UserPackageLicense>(); 
                UPL =[select Id FROM UserPackageLicense WHERE UserID = :i.userId AND PackageLicenseId = :i.packageId];                 
            delete UPL;
        }
    }
}

 
JeffreyStevensJeffreyStevens

 
I think this will get you close....
 
public static void deletePackageLicense(list<inputValues> inputs) {
    // Collect user and Package Ids first
    set<id> packageIds = new set<id>();
    set<id> userIds = new set<id>();
    for(intputValues i :inputs) {
        packageIds.add(i.packageId);
        userIds.add(i.UserId);
    }

    list<UserPackageLicense> UPLsToDelete = [SELECT id 
                                               FROM UserPackageLicense 
                                               WHERE userId IN :UserIds 
                                                  AND PackageLicenseId IN :packageIds];

    delete UPLsToDelete;
}


 
farukh sk hdfarukh sk hd
This will help you on this,
https://www.sfdc-lightning.com/2018/11/call-apex-code-from-process-builder-in.html,

To avoid soql query inside for loop you can get id's using for loop and than can operate on list of id's outside for loop.