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
RbnRbn 

How to retrieve Group ID of newly created Group

 

 

I have a Custom Object IA Team and when a IA Team is being created same IA Team gets Sync with The public Group.So,for that i am writing a Trigger and its working it fine.

 

But my req is after the Group is created ,i require the group Id to get populated in a IA Team field called(Unique Group Id).

below is my Trigger::

Trigger GroupCreation on IA_Team__c (after insert)
{
    Group grp = new Group();
    list<Group> gplist=new list<Group>();
    list<IA_Team__c> ialist=new list<IA_Team__c>();
    if(trigger.isinsert)
    {
        set<id> memids = new set<id>();
        for(IA_Team__c ev:Trigger.New)
        memids.add(ev.id);
        for(IA_Team__c ia:[select Name,Public_Group_Name__c from IA_Team__c where id in:memids])
        {
            grp.Name = ia.Name;
            System.debug('@@@@@@@@@@@@@'+ grp.id);
            grp.DeveloperName = ia.Public_Group_Name__c;
            ia.Team_Linked_To_Group__c=True;
            //ia.Unique_Group_Id__c=grp.Id;
            ialist.add(ia);
            gplist.add(grp);
        }
        if(!gplist.isempty())
        {
            insert gplist;
            update  ialist;
        }
    }

 Thanks in Advance

bob_buzzardbob_buzzard

As have two lists where the ordering matches (i.e. gplist[0] contains the group for IA_Team__c ialist[0]), you can simply insert the groups, iterate them to extract the id and then populate the field in the ialist elements - this will work as the id of the group is populated in the local copy of the record after the insert completes.

 

It should be as simple as:

 

if(!gplist.isempty())
{
    insert gplist;
    for (Integer idx=0; idx<gplist.size(); idx++)
    {
       IA_Team__c iat=ialist[idx];
       Group gp = gplist[idx];
       iat.Unique_Group_Id__c=gp.id
    }
    update  ialist;
}