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
Karthikeyan ChandranKarthikeyan Chandran 

Apex Class help Needed - Urgent

Hi Team,

I am new to developement, i need a script to query error records(Field - description) from an object(Object A) and store in a new object (Object B).

Object A Fields,

Name
Description(This field contains ERROR)
Details
Request Time

Obect B Fields.

B_Name
B_Description
B_Details
B_Request Time

Can anyone help me to fix tis ASAP?

Thanks & Regards
Karthi
Medhya MahajanMedhya Mahajan
Hi Karthikeyan, 

Assuming there are less than 50000 records that you need to query, 
ObjectA and ObjectB are the API names of your Objects,
Name ,Description, Details, Request_Time are the APi names of your fields.

Here is the Script you can use. 

You need to put the WHERE condition to find the error records. 
 
List<ObjectA> ObjAList = [SELECT Name ,Description, Details, Request_Time FROM ObjectA WHERE Description =: ERROR LIMIT 50000];
List<ObjectB> ObjBList = new List<ObjectB>();

for (ObjectA objA: ObjAList){
    ObjectB objB = new ObjectB();
    objB.Name = objA.Name ;
    objB.Description = objA.Description;
    objB.Details = objA.Details;
    objB.Request_Time = objA.Request_Time;
    ObjBList.add(objb);
}
System.debug('++++++' + ObjBList);
if(!ObjBList.isEmpty()){
insert ObjBList;
​}

You can first run the script without the insert statement and check if the records about to be inserted are correct in the debug statement. 
If so , you can go ahead with the insertion.

Let me know if this helps.

Regards
Medhya Mahajan
Manohar kumarManohar kumar

Hi Karthi,I dont know what you you mean by error records.I assume its a field.

List<ObjectA> objAList =[select name,Description,Details,RequestTime from ObjectA];
List<ObjectB> bObjList = new List<ObjectB>();
if(objAList.size()>0){
	for(ObjectA o:objAList){
		ObjectB ob = new ObjectB();
		ob.B_Name = o.Name;
		ob.B_Description = o.Description;
		ob.B_Details = o.Details;
		ob.B_RequestTime = o.B_RequestTime;
		bObjList.add(ob);
	} 

}
if(bObjList.size()>0)
	insert bObjList;


Hope this helps.Please let me know if you are looking for something else.

Thanks,

Manohar

Aniket Malvankar 10Aniket Malvankar 10
Hello Karthi,

You can create a trigger on Object A. On create of new record you can copy the text from Description(This field contains ERROR) to B_Description.

I have done on Account and Contact objects.
 
​trigger ForumTrigger1 on Account (after insert) {
    List<Contact> con = new List<Contact>();
    for(Account aa:Trigger.New)
    {
        Contact cc = new Contact();
        cc.C_Description__c = aa.ErrorWithDescription__c;
        
        con.add(cc);
    }
    insert con;
}



Thanks
Aniket