0

I have 4 fields on my custom setting

  1. record type name,
  2. record type id
  3. status
  4. send email
  5. notification type

Now i have to create a new record based on the case status and status combination like RecordTypeName   SobjectType Status  RecordtypeId Send Email

Currently problem is when i am creating a case record for record type R1 with open status it is creating 3 new record for open status .same for waiting it is creating 2 and for closed it is creating one record . it is not matching with record type name and status.

i want to create one record for R1-open i Send_Email_to_email__c want this to consider R2 status. Also Send_Email_to_email__c is getting checked in all the condition

trigger NewtestcaseRecord on Case(after inseRecTY, after update) {
    Map < string,
    string > caseRecordTypeMap = new Map < string, string > ();
    set < string > caserectype__c = new set < string > ();

    List < Case > casel = new List < Case > ();
    string rec_type;
    RecordClass RectyCS = new RecordtypeCSClass();
    RectyCS.CaseFetchrectype__c();

    Map < string, Custom_Setting__c > allRecordTypeMap = RecordtypeCSClass.CaseRecordtypeMap; //Get all Custom setting records
    system.debug('allRecordTypeMap :NewServicenetrecord:' + allRecordTypeMap);
    for (Custom_Setting__c RecTY: allRecordTypeMap.values()) {
        for (Case c1: trigger.new) {
            if (RecTY.RecordTypeId__c == c1.RecordTypeId && RecTY.Status__c == c1.Status) {
                string status = RecTY.Status__c;
                system.debug('status' + status);
                system.debug('RecTY.RecordtypeName__c' + RecTY.RecordtypeName__c);
                caseRecordTypeMap.put(RecTY.Name, RecTY.RecordtypeId__c.substring(0, 15));
                caserectype__cet.add(RecTY.RecordtypeId__c.substring(0, 15));
                rec_type = c1.RecordTypeId;
                system.debug('rectyoe' + rec_type);
                if (caserectype__cet.contains(rec_type.substring(0, 15)) && c1.AD_Private__c != true) {
                    System.debug('Condition satisfied');
                    // if (c1.Status == status )                      {
                    system.debug('Statis' + c1.Status);
                    system.debug('Statis' + status);
                    casel.add(c1);
                    system.debug('casel' + casel);
                    system.debug('casel size' + casel.size());
                    // }
                    //}
                }
            }
        }

        //  }// for loop
        system.debug('caseRecordTypeMap : ' + caseRecordTypeMap);
        system.debug('caserectype__cet : ' + caserectype__cet);

        List < test_Case__c > testcaseList = new List < test_Case__c > ();
        if (!casel.isempty()) {
            for (case c: casel) {
                rec_type = c.RecordTypeId;
                Custom_Setting__c CRecTY = new Custom_Setting__c();
                test_Case__c testcase = new test_Case__c();
                testcase.Case__c = c.ID;
                testcase.Send_Email__c = true;

                if (caserectype__cet.contains(rec_type.substring(0, 15))) {
                    if (c.Status == RecTY.Status__c) {
                        testcase.Notification_Type__c = RecTY.Notification_Type__c;
                        system.debug('test Notification' + testcase.Notification_Type__c);
                    }

                    if (RecTY.Send_Email_to_email__c = true) {
                        testcase.Send_Email_to_email__c = true;
                    } else {
                        testcase.Send_Email_to_email__c = false;
                    }
                }

                if (testcase.Notification_Type__c != null && testcase.Notification_Type__c != '') {
                    testcaseList.add(testcase);
                }
            }
        }

        Chatter_helper.setIstestcaseCreated();
        system.debug('**in inseRecTY testcaseList.size()' + testcaseList.size());
        if (testcaseList.size() > 0) {
            inseRecTY testcaseList;
        }
        system.debug('inseRecTY testcaseList :' + testcaseList);
    }
}
  • Inconsistent indentation and brace style can make it difficult for others (and you!) to follow your code. I've used a formatter to try to clean up the code style. – David Reed Apr 11 at 8:39
  • I have rolled back an edit to this question to undo destruction of the content. Please do not erase question content. – David Reed Apr 21 at 11:19
  • Divya, if you continue to destroy question content I will lock this post. Please do not do this. – David Reed Apr 23 at 11:58
1

You are iterating all the custom settings values, which also has inner for loop for all the cases inserted/updated . It's something like-

for(Custom_Setting__c RecTY : allRecordTypeMap.values()){
        for(Case c1:trigger.new){

         if(RecTY.RecordTypeId__c == c1.RecordTypeId && RecTY.Status__c == c1.Status){           
       casel.add(c1);
             }
        }
    }

So when you create a case let's say with record type R1 and Status as -Open ,then condition

if(RecTY.RecordTypeId__c == c1.RecordTypeId && RecTY.Status__c == c1.Status)

will be true 3 times based on the custom settings above, as you have outer 'for loop' iterating over all the records of the custom settings and you are adding c1 to case1, 3 times. Similarly for other statuses as well. Instead of List of cases as Case1 , you can have set of cases as Case1 and before adding any new case c1 to case1 , you can check if that case already exists (based on record id), if it doesn't then only add c1 to Case1. Though I am not clear on what you want to achieve, but using set will avoid creation of duplicate records. Also, I see lot of scope of improvement in your code. You can avoid nested for loop, using map etc

  • Thanks for the quick reply. by using set i can reslove this but .cantains method is not supporting in my code Error: Compile Error: Method does not exist or incorrect signature: void contains(String) from the type Set<Case> at line 43 column 37 if(caselist1.contains(rec_type.substring(0,15))){ case1.add(c1); } – Rajni Bhagat Apr 11 at 10:16
  • Did you mean contains method in the set in not working as expected? Can you paste where and how are you using it? – Manish Anand Apr 11 at 10:18

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.