Need help - Synchronize names of company & persons

I woud appreciate it very much if someone could help me with a good suggestion or comment on the next code which is in the OnAfterGetRecord section of a report. First, I describe what I am trying to do: We have many person-type contacts with a company name which is different from the company-type contact they belong to. So I try to filter out the contacts with type ‘company’. Second, filter out the matching contacts with type ‘person’ (contact with the same company no. as the filtered company). Third, modify the name of the person-type contact in the name of the ‘parent’ company. The problem is that I can’t get the loop working correctly. Can somebody give me a good advise? <OnAfterGetRecord()> CLEARALL; Counter := 0; IF FIND(’-’) THEN BEGIN REPEAT SETFILTER(Type, ‘Company’); gCompName := “Company Name”; //copy value company name to global gCompareNo := “Company No.”; //copy value company no. to global SETFILTER(Type, ‘Person’); SETFILTER(“Company No.”, gCompareNo); //person contact with matching company no. IF FIND(’-’) THEN BEGIN REPEAT “Company Name” := gCompName; MODIFY; Counter := Counter + 1; UNTIL NEXT =0; END; UNTIL NEXT =0; END; MESSAGE('Changed records: %1 ', Counter);

Hi auchhaben, You should not do the MODIFY on the Rec variable, do this on an extra variable: REPEAT ModifyVar := Rec; ModifyVar."Company Name" := gCompName; ModifyVar.MODIFY; Counter := Counter + 1; UNTIL NEXT =0; br Josef Metz

Thanks jm. Modifying the records is no problem. The problem is that it reads no new records. It stil keeps looping around the first record. So what’s the wrong thing in this code?

Hi auchhaben, you need a second variable for the inner loop. And you can exclude all conacts with the correct “Company Name”. CLEARALL; Counter := 0; IF Rec.FIND('-') THEN REPEAT Rec.SETFILTER(Type, 'Company'); gCompName := Rec."Company Name"; //copy value company name to global gCompareNo := Rec."Company No."; //copy value company no. to global Var2.SETFILTER(Type, 'Person'); Var2.SETRANGE("Company No.", gCompareNo); //person contact with matching company no. Var2.SETFILTER("Company Name",'<>%1' gCompName); IF Var2.FIND('-') THEN REPEAT Var2."Company Name" := gCompName; Var2.MODIFY; Counter := Counter + 1; UNTIL Var2.NEXT =0; UNTIL Rec.NEXT =0; br Josef Metz

Try something like this… SETFILTER(Type, 'Company'); IF FIND('-') THEN REPEAT grecContact.SETFILTER(Type, 'Person'); grecContact.SETFILTER("Company No.", "Company No."); IF grecContact.FIND('-') THEN REPEAT grecContact."Company Name" := "Company No."; grecContact.MODIFY; Counter := Counter + 1; grecContact.UNTIL NEXT =0; UNTIL NEXT =0;

Your first loop is handled by the report design itself. OnAfterGetRecord should be the first Loop with any applied filters you need, then have a variable that is a copy of the Dataitem you are looping through - getting all related records as per your criteria and MODIFY the Variable. Think you can just loose your first loop really.

He uses a report and I missed that. [:o][:I]

Thanks for you answers so far. I used your answers to modify the code. I removed the first loop and the program code looks like this: OnAfterGetRecord() IF Rec.FIND ('-') THEN BEGIN Rec.SETFILTER(Type, 'Company'); gCompName := Rec."Company Name"; //copy value company name to global gCompareNo := Rec."Company No."; //copy value company no. to global Var2.SETFILTER(Type, 'Person'); Var2.SETRANGE("Company No.", gCompareNo); //person contact with matching company no. IF Var2.FIND('-') THEN REPEAT MESSAGE('%1 %2 %3 %4', Var2.Name, gCompareNo, Var2."Company No.", Var2."Company Name"); Var2."Company Name" := gCompName; Var2.MODIFY; UNTIL Var2.NEXT=0; RecCounter := RecCounter+1; IF RecCounter >= 5 THEN BEGIN MESSAGE('RecCounter records: %1 ', Counter); CurrReport.QUIT; END; END; Unfortunately, the report still returns to the first record. The only thing I get is 5 messages for the same record. So The RecCounter variable is needed to stop the report from looping. What is the reason the report doesn’t skip to the next record?

This is because you are telling the report to go to the first record every time you go in to OnAfterGetRecord. I would set any filters you need to set on PreDataItem Then declare a Variable - to look at the records you actually want to modify. Use the Values of Rec - (new record each time OnAfterGetRecord is called) to set filters upon your New Varible - then if needed ModifyALL - or Loop through the related records you get from your new variable. Hope this helps

Thanks Andrew! That worked. My final code is: OnPreDataItem() SETFILTER(Type, 'Company'); OnAfterGetRecord() gCompName := "Company Name"; //copy value company name to global gCompareNo := "Company No."; //copy value company no. to global Var2.SETFILTER(Type, 'Person'); Var2.SETRANGE("Company No.", gCompareNo); //person contact with matching company no. IF Var2.FIND('-') THEN REPEAT Var2."Company Name" := gCompName; Var2.MODIFY; UNTIL Var2.NEXT=0;