How to create a vendor or customer contact from a csv file

In this blog we are going to show how to create a contact for a customer or vendor using x++ code.

1

2

This is the code for the creation of the contact, we will be using a job.

 X++ |  copy code |? 
001
//Emilio Iza Paucar
002
//2014-06-11
003
//EI_LoadCustTable: Declaración de variables
004
class EI_GELoadCustContactPerson Extends Runbase
005
{
006
    DialogRunbase                               dialog;
007
//variables globales
008
    DialogField                 dlgfiledata;
009
    DialogField                 dlgfiledata2;
010
    FileNameOpen                filedata;
011
    FileNameOpen                fileErrors;
012
    TransactionLog              transactionLog;
013
    asciiIo                     trans;
014
    asciiIo                     error;
015
    str                         errorTxt;
016
    container                   transLine;
017
    container                   errors;
018
    boolean                     validateDate;
019
    boolean                     exist;
020
    str                         Lineread;
021
    str                         Line;
022
    CustParameters              custParameters;
023
    boolean                     errorfileopen;
024
    AsciiIo                     fileError;
025
    FileNameOpen                filedataError;
026
 
027
    CustTable   custTable;
028
 
029
    // Declare variables: views.
030
    DirPartyContactInfoView contactInfo;
031
    DirPartyPostalAddressView postalAddress;
032
 
033
 
034
    // Declare variables: classes.
035
    ContactPersonEntity contactPersonEntity;
036
    LogisticsLocationEntity entity;
037
 
038
 
039
    ContactPerson contactperson;
040
    DirPartyTable partyTable;
041
    ContactPersonId ContactPersonId;
042
 
043
 
044
 
045
    #DEFINE.CurrentVersion(1)
046
    #localmacro.CurrentList
047
        filedata,
048
        filedataError
049
    #endmacro
050
 
051
}
052
 
053
 
054
 
055
//Emilio Iza Paucar
056
//2014-06-11
057
//EI_LoadCustTable: Método principal
058
static void main(Args _args)
059
{
060
    EI_GELoadCustContactPerson     GELoadCustContactPerson;
061
    ;
062
 
063
    GELoadCustContactPerson = new EI_GELoadCustContactPerson();
064
 
065
    if(GELoadCustContactPerson.prompt())
066
        GELoadCustContactPerson.run();
067
}
068
 
069
 
070
public Object dialog()
071
{
072
    Object ret;
073
    ;
074
 
075
    dialog = new DialogRunBase('Carga de ContactPerson', this);
076
 
077
    dlgfiledata     = dialog.addField(extendedTypeStr(FileNameOpen), 'Fichero de carga de datos');
078
    dlgfiledata2    = dialog.addField(extendedTypeStr(FilePath),'Fichero de errores');
079
 
080
 
081
 
082
    return dialog;
083
}
084
 
085
 
086
 
087
 
088
public boolean getFromDialog()
089
{
090
    boolean ret;
091
    ;
092
 
093
    ret = super();
094
 
095
    filedata        = dlgfiledata.value();
096
 
097
    filedataError   = dlgfiledata2.value();
098
 
099
 
100
    return ret;
101
}
102
 
103
 
104
//Emilio Iza Paucar
105
//2014-06-11
106
//EI_LoadCustTable: Ejecuta la logica del proceso
107
public void run()
108
{
109
 
110
    try
111
    {
112
 
113
        if (!fileData)
114
            throw error ("@SYS18678",fileData);
115
 
116
        super();
117
        info(strFmt("inicio %1", time2str(timeNow(),1,1)));
118
 
119
        validateDate    = true;
120
        trans           = new AsciiIo(filedata,"R");
121
        trans.inFieldDelimiter(";");
122
        Transline       = trans.read(); //Leemos la 1ª linea y la insertamos en un contenedor
123
 
124
        ttsBegin;
125
        while (trans.status() == IO_Status::Ok)
126
        {
127
            //proceso
128
            custTable = custTable::find(strLRTrim(conpeek(transline,15)));
129
 
130
            if(custTable)
131
                this.create();
132
            //
133
            transLine    = trans.read();
134
        }
135
 
136
        info(strFmt("final %1", time2str(timeNow(),1,1)));
137
        if(errorTxt)
138
        {
139
            this.writeerror();
140
            throw(Exception::Error);
141
        }
142
        else
143
        {
144
            info("Proceso finalizado");
145
        }
146
        ttscommit;
147
    }
148
    catch (exception::Error)
149
    {
150
        error("Proceso cancelado");
151
        ttsabort;
152
    }
153
}
154
 
155
 
156
//Emilio Iza Paucar
157
//2014-06-11
158
//EI_LoadCustTable: Crea los contactos
159
private void create()
160
{
161
    // Declare variables: tables.
162
 
163
    LogisticsPostalAddress logisticsPostalAddressInfo;
164
 
165
    // Declare variables: extended data types.
166
    Phone phone;
167
 
168
    // Declare variables: primitives.
169
    str firstName;
170
    str middleName;
171
    str lastName;
172
 
173
    ContactPersonId = strLRTrim(conpeek(transline,2));
174
 
175
    if(ContactPersonId)
176
    {
177
        // Create and populate the contact person.
178
        this.createContact();
179
 
180
        // Populate the postal address information by using the view.
181
        this.createAddress();
182
 
183
         //Update the contact information.
184
        this.contactinfo();
185
    }
186
}
187
 
188
 
189
private void createContact()
190
{
191
 
192
    contactperson = contactperson::find(ContactPersonId);
193
 
194
    // Create and populate the contact person.
195
    contactPersonEntity = ContactPersonEntity::construct(contactPerson);
196
 
197
    if(!contactperson)
198
    {
199
        contactPersonEntity.parmContactPersonId(ContactPersonId);
200
    }
201
 
202
    partyTable = DirPartyTable::findRec(CustTable.Party);
203
 
204
    //if(!partyTable)
205
      //  return;
206
 
207
    contactPersonEntity.parmName                            (strLRTrim(conpeek(transline,1)));
208
    contactPersonEntity.parmFirstName                       (strLRTrim(conpeek(transline,77)));//'Contact');
209
    contactPersonEntity.parmMiddleName                      (strLRTrim(conpeek(transline,78)));//'M.');
210
    contactPersonEntity.parmLastName                        (strLRTrim(conpeek(transline,79)));//'Person');
211
    contactPersonEntity.parmAssistantName                   (strLRTrim(conpeek(transline,70)));//'AssistantName');
212
    contactPersonEntity.parmBillingInformation              (strLRTrim(conpeek(transline,22)));//'Billing info');
213
    contactPersonEntity.parmCharacter                       (strLRTrim(conpeek(transline,26)));//'Character description');
214
    contactPersonEntity.parmComputerNetworkName             (strLRTrim(conpeek(transline,58)));//'Computer network name');
215
    contactPersonEntity.parmContactMemo                     (strLRTrim(conpeek(transline,29)));//'Memo');
216
    contactPersonEntity.parmLoyalty                         (strLRTrim(conpeek(transline,23)));//'Loyalty');
217
    contactPersonEntity.parmMileage                         (strLRTrim(conpeek(transline,48)));//'Mileage');
218
    contactPersonEntity.parmOfficeLocation                  (strLRTrim(conpeek(transline,51)));//'Office location');
219
    //contactPersonEntity.parmOutlookCategories('Outlook categories');
220
 
221
    contactPersonEntity.parmProfession                      (strLRTrim(conpeek(transline,61)));//'Profession');
222
    contactPersonEntity.parmSensitivity                     (conpeek(transline,56));
223
    contactPersonEntity.parmSpouse                          (strLRTrim(conpeek(transline,69)));//;'Spouse');
224
    //contactPersonEntity.parmProfessionalTitle               (strLRTrim(conpeek(transline,14)));
225
    //contactPersonEntity.parmPersonalTitle                   (strLRTrim(conpeek(transline,14)));
226
    contactPersonEntity.parmTitle                           (strLRTrim(conpeek(transline,14)));
227
 
228
    if(str2time(conpeek(transline,37)))
229
        contactPersonEntity.parmTimeAvailableFrom               (str2time(conpeek(transline,37)));//1000);
230
    else
231
        contactPersonEntity.parmTimeAvailableFrom               (0);//1000);
232
 
233
    if(str2time(conpeek(transline,38)))
234
        contactPersonEntity.parmTimeAvailableTo                 (str2time(conpeek(transline,38)));//2000);
235
    else
236
        contactPersonEntity.parmTimeAvailableTo                 (0);
237
 
238
    contactPersonEntity.parmGender                          (conpeek(transline,72));
239
    contactpersonentity.parmLanguageId                      (strLRTrim(conpeek(transline,40)));
240
    contactPersonEntity.parmPersonalSuffix                  (strLRTrim(conpeek(transline,73)));
241
 
242
    contactPersonEntity.parmContactForParty(partyTable.RecId);
243
    contactPersonEntity.write();
244
}
245
 
246
 
247
private void createAddress()
248
{
249
    postalAddress.clear();
250
    postalAddress.Street                = strLRTrim(conpeek(transline,17));//'One Microsoft Way';
251
    postalAddress.City                  = strLRTrim(conpeek(transline,16));//'Redmond';
252
    postalAddress.State                 = strLRTrim(conpeek(transline,10));//'WA';
253
    postalAddress.ZipCode               = strLRTrim(conpeek(transline,13));//'98052';
254
    postalAddress.CountryRegionId       = strLRTrim(conpeek(transline,12));//'US';
255
    postalAddress.County                = strLRTrim(conpeek(transline,11));
256
 
257
    // Update the postal address information.
258
    contactPersonEntity.createOrUpdatePostalAddress(postalAddress);
259
}
260
 
261
 
262
private void contactinfo()
263
{
264
    if(strLRTrim(conpeek(transline,3))) //EMail
265
    {
266
        // Update the contact information.
267
        this.contactInfoWrite(strLRTrim(conpeek(transline,3)), LogisticsElectronicAddressMethodType::Email);
268
        contactPersonEntity.createOrUpdateContactInfo(contactInfo);
269
    }
270
    if(strLRTrim(conpeek(transline,4))) //Telex
271
    {
272
        // Update the contact information.
273
        this.contactInfoWrite(strLRTrim(conpeek(transline,4)), LogisticsElectronicAddressMethodType::Telex);
274
        contactPersonEntity.createOrUpdateContactInfo(contactInfo);
275
    }
276
    if(strLRTrim(conpeek(transline,5))) //Celular
277
    {
278
        // Update the contact information.
279
        this.contactInfoWrite(strLRTrim(conpeek(transline,5)), LogisticsElectronicAddressMethodType::Phone, NoYes::Yes);
280
        contactPersonEntity.createOrUpdateContactInfo(contactInfo);
281
    }
282
    if(strLRTrim(conpeek(transline,6))) //PhoneLocal
283
    {
284
        // Update the contact information.
285
        this.contactInfoWrite(strLRTrim(conpeek(transline,6)), LogisticsElectronicAddressMethodType::Phone);
286
        contactPersonEntity.createOrUpdateContactInfo(contactInfo);
287
    }
288
    if(strLRTrim(conpeek(transline,7))) //Fax
289
    {
290
        // Update the contact information.
291
        this.contactInfoWrite(strLRTrim(conpeek(transline,7)), LogisticsElectronicAddressMethodType::Fax);
292
        contactPersonEntity.createOrUpdateContactInfo(contactInfo);
293
    }
294
    if(strLRTrim(conpeek(transline,8))) //Fax
295
    {
296
        // Update the contact information.
297
        this.contactInfoWrite(strLRTrim(conpeek(transline,8)), LogisticsElectronicAddressMethodType::URL);
298
        contactPersonEntity.createOrUpdateContactInfo(contactInfo);
299
    }
300
    if(strLRTrim(conpeek(transline,9))) //Fax
301
    {
302
        // Update the contact information.
303
        this.contactInfoWrite(strLRTrim(conpeek(transline,9)), LogisticsElectronicAddressMethodType::Phone);
304
        contactPersonEntity.createOrUpdateContactInfo(contactInfo);
305
    }
306
}
307
 
308
 
309
private void contactInfoWrite(str    locator, LogisticsElectronicAddressMethodType   type, Noyes IsMobilePhone = false)
310
{
311
    // Populate the contact information by using the view.
312
    contactInfo.clear();
313
    contactInfo.Locator             = locator;
314
    contactInfo.Type                = type;
315
    contactInfo.IsMobilePhone       = IsMobilePhone;
316
    contactInfo.IsPrimary           = true;
317
}
318

So when executing the class we will see the following dialog:

3

We choose our csv file

4

And the job will start the process creating the contacts as expected.

5