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
Rohit Saxena 17Rohit Saxena 17 

can someone assist me on how to proceed withe the below requirements :

Main Object : Case
Child Object : custom_object__c

Conditions:

Create custom_object__c record If ,

1) Case.Survey_Star_Rating__c != null for the cases created last week. create a child record immediately (no other conditions needed).
                   
2) Case.STS__C = true. Create child records per owner 1 in a day and maximum 2 per week if no above records is found for the same owner.
3) Case.CS_Deal__c = true. Create child records per owner 1 in a day and maximum 2 per week if no above records is found for the same owner.

Hint - if 1 survey record is found in last week then this week only 3 more records to be created for points 2 & 3

       if 2 survey record is found     in last week then this week only 2 more records to be created for points 2 & 3
       
       if a record is created for point 2 or 3, no other records should be created today for the same owner.

Child records should be updated with:

1) custom_object__c.Owner = queueid
2) custom_object__c.Case No. = Lookup of case
3) custom_object__c.Agent__c = Case.owner
Saravana Bharathi 1Saravana Bharathi 1
For 1st Point, We can achieve through process Builder.

Create a process on Case, 
Check the condition Survey_Star_Rating !=NULL and CreatedDate in:last Week
Create Child Record for custom object with specific value.

For 2nd Point, in-order to check the logic for existing record, ownership count, we need to go through trigger.

As I suggest, If you are already having a trigger for achieving 2 & 3.
You can do 1st point also in a trigger. since, its going to be one more line in Trigger, and you have all the desired functionality handled in one place.

Hope. This Helps.

Thanks
 
Rohit Saxena 17Rohit Saxena 17
Hi Saravana,

Thank You for loking into it. I am sure this functionality can be achieved by triggers, however the main roadblock for me is counting ownership and limite the no. of records created. I want only 1 record is created in a day for each owner when the criteria is met and overall 4 records in a week. I am new in Apex so have no idea how to start. Please can you assist me with the trigger.

Thanks
Saravana Bharathi 1Saravana Bharathi 1
I can give you the code, but that wont help you in learning apex.
Ask as many useful links or any documentation which helps you in learning apex. Probably, even If I dont have, others can help.
Go through the below links, try with simple examples mentioned in the document.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm
Try to achieve your functionality. Still you face some issue/error in implementing your functionality, you can share your questions with your code.

Thanks
Rohit Saxena 17Rohit Saxena 17
I understand its not going to help me in learning apex if you can give the code, but the requirements here are so complex it would take more than a Month for me to create this. I have to meet a deadline in 4 days. I do have prior experience in Apex but not as much as you have.

Appreciate it !!

Thanks
Rohit Saxena 17Rohit Saxena 17
I have written this till now not sure how to proceed with the ownercount thing.


global class qaCreateNewRecords implements Database.Batchable<SObject>, Database.Stateful {

    global Database.QueryLocator start(Database.BatchableContext bc) {
        //first, you gather all the records you want to create a new record with
        String query = 'Select Id,Ownerid,STS__c,Origin,CreatedDate,Status from Case WHERE STS__c= true';// AND Origin = Phone AND Status = Closed'; //&& CreatedDate >Last_Week' ;
        //below checks to see if the batch is being called from a test class. If the record count
        //returned by the query above is over 200, the test will fail
        
       if(Test.isRunningTest())
        query  += ' limit 200';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<Case> originalrecords){
        //now we want to loop through the records from the query above and create the new records
        //and store them in a list
        List<QA_Scorecard__c> newrecords = new List<QA_Scorecard__c>();
        
     //   Group queue = [SELECT Id FROM Group WHERE Name = 'QA_Scorecards' and Type = 'Queue'];
      
        for(Case yo1 :originalrecords){ // this is the loop
            QA_Scorecard__c yo2 = new QA_Scorecard__c(); //this sets up a new record to be created
            yo2.Agent__c= yo1.Ownerid;
            yo2.Case_1__c= yo1.id;
            yo2.Ownerid = '00Gc0000001YLhX';
            //add any other fields on the new record that you want to set
            newrecords.add(yo2); //add to list to insert into SFDC later
        }

        //this checks to see if there are any records to insert. If there are, then it will 
        //create them
        if(!newrecords.isEmpty())
            database.insert(newrecords,false); 
        //newrecords is the list of records you put in during the for loop. The "false" is saying,
        //if one records fails to insert, still insert the rest (All or Nothing)
    }
    
    global void finish(Database.BatchableContext bc) {}
    
    }