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
krishna chaitanya 35krishna chaitanya 35 

Assign a value retrived from Map to a custom field in account

Hello All,
I have a requiremnt to get the delegated approver email on the account based on the account createdby value.
Map <ID,ID> accnuser =new Map<ID,ID>();
List<Account> acc = new List<Account>();
acc=[select id,createdbyid from account where id='0011x000014rEQO'];
for(Account accs :acc){
    accnuser.put(accs.id,accs.createdbyid);
}

Map <ID,ID> userndelegated =new Map<ID,ID>();
List<User> uss =[select id,DelegatedApproverId,createdbyid,isactive from user where DelegatedApproverId!= NULL and ID IN :accnuser.values()];

for(user us :uss){
   userndelegated.put(us.id,us.DelegatedApproverId);
    system.debug('set of delegated id '+userndelegated);
}
 List <user> us1 = [select id,email from user where id IN:userndelegated.values()];
 Map <Id,String> us3 = new Map <Id,String>();
for(user us2 :us1){
    us3.put(us2.id,us2.email);
    system.debug('list of emails2'+us3);
}

system.debug('list of emails'+us1);

//Account acce = new Account();
//acce.Delegated_Approver_Email__c=us3.values();
//update acc;

I am able to retrive the delegated approver email and able stored in map us3 , the next step i want to update the a custom field in the same account with email address, i am missing something here .

can you help me to complete the operation and also please suggest to if there are unneccessary lines in code(optimization) and i want to put this in helper class and call from a trigger .

THank you in Advance
krishna chaitanya 35krishna chaitanya 35
public class AccountUpdateWithDelegated {
public static void fillDelegatedApproverEmail(Set<id>  accn) {
List<ProcessInstance> pi = new List<ProcessInstance>();
Map <ID,ID> prinst =new Map<ID,ID>();
Map <ID,ID> Prsinswi =new Map<ID,ID>();
Map <ID,ID> userndelegated =new Map<ID,ID>();
List < Account > ac = new List < Account > ();
pi=[select id,Status,TargetObjectId ,LastActorId from  ProcessInstance where  TargetObjectId IN :accn];

for (ProcessInstance ps : pi){
prinst.put(ps.TargetObjectId,ps.id);
system.debug('list of id '+prinst);
}

List<ProcessInstanceWorkitem> piw = new List<ProcessInstanceWorkitem>();

piw =[select id,ActorId,ProcessInstanceId from ProcessInstanceWorkitem where ProcessInstanceId IN :prinst.values()];
for (ProcessInstanceWorkitem psiw :piw){
    Prsinswi.put(psiw.id,psiw.ActorId);
    system.debug('list of process instance items in piws'+prinst.values());
}
 
List<user> us = [select id,DelegatedApproverId,Email,createdbyid,isactive from user where DelegatedApproverId!= NULL and ID IN :Prsinswi.values()];

system.debug('Delegated approver '+us);
for(user usrs :us){
userndelegated.put(usrs.id,usrs.DelegatedApproverId);
    system.debug('set of delegated id '+userndelegated);
}
List <user> us1 = [select id,email from user where id IN:userndelegated.values()];
for (user usrss : us1) {
            Account acce = new Account();
            acce.Delegated_Approver_Email__c = usrss.email;
            //acce.id = prinst.get.ps.TargetObjectId;
            system.debug('Delegated_Approver_Email__c' + acce.Delegated_Approver_Email__c);
            ac.add(acce);
        }
    If (!ac.isEmpty()){
        update ac;
    }
}
}

can anyone help me with update operation in the end, how to get the actual id of the account to update