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
sashamsasham 

Pls anyone help on this

I have all participant and states object (two records registered and assessed ). for some participant is not assesed inthe table evethough it is actually assessed. need to insert those records to states as assesesd 

i have list of patticipant ids and list of  states as assessed 

 if(recs.size()>0 && participantsatates.size()>0) 
         {
              for(participant__c p:recs)
                 {
             
                     for(States__c pstate :participantsatates)
                         
                         {
                            IF (P.Id = pstate.Participant__c)
                            {
                                
                                
                            }
                             
                 
                         }
                 }
Best Answer chosen by sasham
Gokula KrishnanGokula Krishnan
Hi Sasham,

Try this,
 
//Storing all the State with Assessd picklist value in Set type
Set<id> PartSet = new Set<id>();
For(State__c st : [Select id,states,Participantt_c from State__c where Participantt_c != null]){
	if(st.states == 'assesesd')
		PartSet.add(st.Participantt_c);
}

//Checking participant have State as Assessed picklist value or not
List<State__c> insertList = new List<State__c>();
For(Participantt_c pt : [Select id from Participantt_c]){
	if(!PartSet.contains(pt.id))
		State__c stObj = new State__c();
		stObj.Participantt_c = pt.id;
        stObj.states = 'assesesd';
		insertList.add(stObj);
}


//Insert state records
if(insertList.size() > 0)
	insert insertList;

Thanks,

If it helps you, please mark is as best answer, so it will be helpful for other developers.

All Answers

Gokula KrishnanGokula Krishnan
Hi Sasham,

Your code is not clear, could you answer for the below:

1.  what is the assessed record id?
2. what is the API name for the states in participant object? Did you create lookup field in Participant?
3. For all the participant  records need to update states as assessed ?

Thanks,
sashamsasham
Hi Thank you for the reply.
Participant - Participantt_c
State - state (picklist as registered and assesesd) ,, Participantt_c look up , 

 I have all the participant ids  which is matched form other table 
List < Participantt_c > par = ​List < Participantt_c >()
List <id> pid = List <id>()
need  to insert state table if there is no state like assesed 
sashamsasham
two objects - participant and State
Participant  -  Participantt_c 
State - Participantt_c (look up field), states( picklist as egistered and assesesd)
queriyng all participants whch match the od form other object and see if there is  a state  as assesed for ecah participant , if not need to insert one 
Gokula KrishnanGokula Krishnan
Hi Sasham,

Try this,
 
//Storing all the State with Assessd picklist value in Set type
Set<id> PartSet = new Set<id>();
For(State__c st : [Select id,states,Participantt_c from State__c where Participantt_c != null]){
	if(st.states == 'assesesd')
		PartSet.add(st.Participantt_c);
}

//Checking participant have State as Assessed picklist value or not
List<State__c> insertList = new List<State__c>();
For(Participantt_c pt : [Select id from Participantt_c]){
	if(!PartSet.contains(pt.id))
		State__c stObj = new State__c();
		stObj.Participantt_c = pt.id;
        stObj.states = 'assesesd';
		insertList.add(stObj);
}


//Insert state records
if(insertList.size() > 0)
	insert insertList;

Thanks,

If it helps you, please mark is as best answer, so it will be helpful for other developers.
This was selected as the best answer
sashamsasham
Hi Gokula , thank you for the reply. i look at your code and made using map 

Pls have a look at the code .
I have some questions and clarification.
This code is used using Vf page and button . when i click on the button it will update the states 
We have huge participants in the system . i  am querying all the participants , states and Reviews in my code. pls let me know it will not throw an error in regards to governor limit 
if that so , how do i change and what to do for that ( i used map for that ) still confusing 
Pls answer if it is possible 
 public PageReference insertstatesforparticipant(){

String statesRec = assessed;
String quiz= '123'
PsRecsById = new Map<id, States__c>();
recsById =  new Map<id, (participant__c> ();
         for(States__c st : [select Id,participant__c,state_c
                                         from  States__c
                                           where participant__c!=null and State__c=:statesRec])
             {
                PsRecsById .put(st.participant__c, st);
                 
             }
         stateIds = new Set<ID>();
         stateIds= PsRecsById .keyset();
         
        
         reviewtRecsById.put= new Map<Decimal, Review_C>(); 
         for(Review_C  assrecs : [select Id, RL_C,Quiz_C,CreatedDate from  Review_C where  Quiz_C=:quiz])
             {
                reviewtRecsById.put((Decimal)assrecs.RL_C, assrecs);
                 
             } 
         
          List<States__c> insertList = new List <States__c>();
         
         for(participant__c par :[select id,RL_C from participant__c where RL_C reviewtRecsById.keySet()])
             {
               // recsById.put(par.id, par);
                if(!stateIds.contains(par.ID))
                {
                     States__c stateObj = new States__c();
                     stateObj.participant__c = par.Id;
                     stateObj.State__c =statesRec.id;                  
                     stateObj.ReceivedAt_C=reviewRecsById.get(par.RL_C ).CreatedDate;
                     insertList.add(stateObj);
                }
             }
         if(insertList.size()>0)
         {
             insert insertList;
         }
return null;
    ]

 
Gokula KrishnanGokula Krishnan
Hi Sasham,

The code is fine, it does not throws any governor limits.

In line 42, correct as stateObj.State__c =statesRec;   //statesRec is a string field, you are using as statesRec.id

If you not using PsRecsById other than in line 23. you can remove it and directly add the ids in line
as below: replace line 15 - 23.
stateIds = new Set<ID>();
for(States__c st : [select Id,participant__c,state_c
                                         from  States__c
                                           where participant__c!=null and State__c=:statesRec])
             {
                stateIds.add(st.participant__c);

             }

Thanks,
sashamsasham
Thank you for lookig at that..Yes i will change that . My only concern is .We have huge participant (Ex more than 10000,) . i am using 3 queries form participant , Review ( also morethan  10000) and State objects .if  total no of records form 3 queries will  be exceeded mor than 50000 records., what will happen ?  and alos it is everthing  in one class . pls suggest any ways to modify if you  think that is the problem.
Gokula KrishnanGokula Krishnan
Hi Sasham,

I hope you will not get any limit exception,  since you're using where condition in your SOQL Query statement.

If you got  any issue, post it and will check on it
(or)
Need to go with Batch class
Batch Class Referencehttps://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

Thanks,

If it helps you, please mark is as best answer, so it will be helpful for other developers.
sashamsasham
Thank you Gokula . Yes i am  using where condition.but our all particpant have that review id . it is basically retiving all particpant in objectt.
Just one question .I am yf page and the button. From the controller is it possible to run bacth . I mean i cn craete a batch class and intaiting form the controller calss when clicking on the button, or ineed to craete batch class 
and other question. if i create a helper calss for this controller , . so that i can reduce the governer limit.
 
Gokula KrishnanGokula Krishnan
Hi Sasham,

If you need do bulk operation, then you can go with Batch Class.

VF Question:
You can call the batch class from the controller.
BatchClass obj = BatchClass();
Database.executeBatch(obj);

Referencehttps://success.salesforce.com/answers?id=90630000000hkxlAAA

Thanks..