1. Create a new lead whenever a new user (with profile Marketing User) is created, also add the user into following Public group and Queues:

    • General Announcements (public Group)
    • Marketing Hot Leads (Queue)
    • Newsletter Signup (Queue)
  2. Then auto assigns the following permission set:

    • View and Edit Converted Leads (Custom Permission Set)
    • Modify All Leads (Custom Permission Set)
    • View All Data (Custom Permission Set)

Below is what I tried to do ,but when I create user neither its added to Group ,Queue nor the permission sets are assigned. I am not able to figure out, where am I missing ? Is my code totally wrong or am I missing something very basic ? Please help me, I am a newbie :

trigger ExerciseTwo on User (after insert) 

    Group pG =  new GROUP(); 
    pG.Name  = 'General Announcements' ;
    pG.Type = 'Regular';
    Group Q  =  new GROUP();
    Q.Name  = 'Marketing Hot Leads';
    Q.Type = 'Queue';
    Group Qu =  new GROUP();
    qU.Name = 'Newsletter Signup';
    Qu.Type = 'Queue';

    permissionSet vC = new PermissionSet(); 
    vC.PermissionsAllowViewEditConvertedLeads = true; 

    PermissionSet mD = new PermissionSet();
    mD.PermissionsModifyAllData = true;

    permissionSet vL = new permissionSet();
    vl.PermissionsViewAllData= true;

    GroupMember gM = new GroupMember(); 
    gM.GroupId = pg.Id;
    gM.GroupId = Q.Id;
    gM.GroupId = Qu.Id;

    PermissionSetAssignment psa = new PermissionSetAssignment();
    psa.PermissionSetId = vC.id;
    psa.PermissionSetId = mD.Id;
    psa.PermissionSetId = vL.Id;

    for( User u : Trigger.new)
        if ( u.UserPermissionsMarketingUser == true)
            Lead Led = new Lead(); 
            gM.UserOrGroupId = u.Id;
            psa.AssigneeId = u.Id;
  • 2
    Welcome to SFSE! Please take the tour and read How to Ask. Your question is mostly complete, you've included your objective and the code you've tried so far, but it's still missing one piece. What is the problem you're encountering? Are you getting an error, or does the code you have not work the way you want it to? The more specific you are, the better the chance that your question will be well received and garner accurate, tailored answers. You should add this information to your question by making an edit to your question. – Derek F Sep 4 '19 at 2:07
  • Sure, will do that right away. Thanks ! – user56430 Sep 4 '19 at 2:19

To store objects you need to insert them, and to relate one object to another, you need to set the Id value of the "another" object in the foreign key field of the "one" object. The "another" must have been inserted first so that it has an Id value allocated. This also needs to be done "in bulk" in triggers.

Work through this Data Modeling module, and also see e.g. Inserting and Updating Records.

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.