Add / Update Checkbox field using PnP

Good Afternoon,

We have a button on a form that creates a new version of the item and we are utilising PnP to perform this and I am struggling to get the correct syntax to get the value from a Multiple Select / Checkbox field on the form and then create a new item utilising the data. My code is below: -

The internal name of the multiple select field is - Contract_x0020_Systems

// Function to Raise Renewal for the Contract
function raiseRenewalContract () {
	if (confirm('Are you sure you wish to raise a Renewal for this Contract?')) {
		doNotRedirect = true;
		//fd.save();
		var refParts = fd.field('Title').value.split('-');
		var newVersion = parseFloat(refParts[2]) + 1;
		var newReference = refParts[0] + '-' + refParts[1] + '-' + newVersion;
		var correspondenceCompanyNameId = fd.field('Correspondence_x0020_Company_x00').value.LookupId;
		var correspondenceAddressNameId = fd.field('Correspondence_x0020_Address_x00').value.LookupId;
		var correspondenceContactNameId = fd.field('Correspondence_x0020_Contact_x00').value.LookupId;
		var siteCompanyNameId = fd.field('Company_x0020_Name').value.LookupId;
		var siteAddressNameId = fd.field('Address_x0020_Name').value.LookupId;
		var siteContactNameId = fd.field('Contact_x0020_Name').value.LookupId;
		var wrsCompanyId = fd.field('WRS_x0020_Company').value.LookupId;
		var wrsCompanyName = fd.field('WRS_x0020_Company').value.LookupValue;
		var responsibility = retrieveResponisibilityPersonOrGroupFieldByFilter(correspondenceCompanyNameId,wrsCompanyName);
		var customerAccountManagerId = responsibility.UserId;
		var dateRaised = new Date();
		var quotedContractLength = parseFloat(fd.field('Quoted_x0020_Contract_x0020_Leng').value);
		var parts = fd.field('Renewal_x0020_Due_x0020_Date').value.split('/');
		var string = parts[1] + '/' + parts[0] + '/' + parts[2];
		var proposedStartDate = new Date(string);
		var renewalDate = new Date(string);
		renewalDate.setMonth(renewalDate.getMonth() + quotedContractLength);
		var endDate = new Date(string);
		endDate.setMonth(endDate.getMonth() + quotedContractLength);
		endDate.setDate(endDate.getDate() - 1);
		debugger;
		var contractOption = fd.field('Contract_x0020_Option').value;
		var contractSystems = fd.field('Contract_x0020_Systems').value;
        var cctvDetails = fd.field('CCTV_x0020__x002d__x0020_Detail').value;
		var list = pnp.sp.web.lists.getByTitle("Solutions%20Maintenance%20Contracts");
		list.items.add({
			Title: newReference,
			Correspondence_x0020_Company_x00Id: correspondenceCompanyNameId,
			Correspondence_x0020_Address_x00Id: correspondenceAddressNameId,
			Correspondence_x0020_Contact_x00Id: correspondenceContactNameId,
			Company_x0020_NameId: siteCompanyNameId,
			Address_x0020_NameId: siteAddressNameId,
			Contact_x0020_NameId: siteContactNameId,
			WRS_x0020_CompanyId: wrsCompanyId,
			Customer_x0020_Account_x0020_ManId: customerAccountManagerId,
			Date_x0020_Raised: dateRaised,
			Contract_x0020_Issue_x0020_Type: 'Renewal',
			Quoted_x0020_Contract_x0020_Leng: quotedContractLength,
			Contract_x0020_Start_x0020_Date: proposedStartDate,
			Contract_x0020_End_x0020_Date: endDate,
			Renewal_x0020_Due_x0020_Date: renewalDate,
			Forecast_x0020_Date: proposedStartDate,
			Forecast_x0020_Probability: '3 - Ongoing',
			Contract_x0020_Option: contractOption,
			Contract_x0020_Systems: contractSystems,
			CCTV_x0020__x002d__x0020_Detail: cctvDetails
		}).then(function(){
		    console.log("Created!");
		});
	}
}

Thanks

Hello @Tony_Duke,

The trick is to set the value in the new item. The syntax should be as following.

ChoiceField: {
      results: [ 'Choice #2', 'Choice #3' ]
    }

So your code will be as below.

// Function to Raise Renewal for the Contract
function raiseRenewalContract () {
	if (confirm('Are you sure you wish to raise a Renewal for this Contract?')) {
		doNotRedirect = true;
		//fd.save();
		var refParts = fd.field('Title').value.split('-');
		var newVersion = parseFloat(refParts[2]) + 1;
		var newReference = refParts[0] + '-' + refParts[1] + '-' + newVersion;
		var correspondenceCompanyNameId = fd.field('Correspondence_x0020_Company_x00').value.LookupId;
		var correspondenceAddressNameId = fd.field('Correspondence_x0020_Address_x00').value.LookupId;
		var correspondenceContactNameId = fd.field('Correspondence_x0020_Contact_x00').value.LookupId;
		var siteCompanyNameId = fd.field('Company_x0020_Name').value.LookupId;
		var siteAddressNameId = fd.field('Address_x0020_Name').value.LookupId;
		var siteContactNameId = fd.field('Contact_x0020_Name').value.LookupId;
		var wrsCompanyId = fd.field('WRS_x0020_Company').value.LookupId;
		var wrsCompanyName = fd.field('WRS_x0020_Company').value.LookupValue;
		var responsibility = retrieveResponisibilityPersonOrGroupFieldByFilter(correspondenceCompanyNameId,wrsCompanyName);
		var customerAccountManagerId = responsibility.UserId;
		var dateRaised = new Date();
		var quotedContractLength = parseFloat(fd.field('Quoted_x0020_Contract_x0020_Leng').value);
		var parts = fd.field('Renewal_x0020_Due_x0020_Date').value.split('/');
		var string = parts[1] + '/' + parts[0] + '/' + parts[2];
		var proposedStartDate = new Date(string);
		var renewalDate = new Date(string);
		renewalDate.setMonth(renewalDate.getMonth() + quotedContractLength);
		var endDate = new Date(string);
		endDate.setMonth(endDate.getMonth() + quotedContractLength);
		endDate.setDate(endDate.getDate() - 1);
		debugger;
		var contractOption = fd.field('Contract_x0020_Option').value;
		var contractSystems = fd.field('Contract_x0020_Systems').value;
        var cctvDetails = fd.field('CCTV_x0020__x002d__x0020_Detail').value;
		var list = pnp.sp.web.lists.getByTitle("Solutions%20Maintenance%20Contracts");
		list.items.add({
			Title: newReference,
			Correspondence_x0020_Company_x00Id: correspondenceCompanyNameId,
			Correspondence_x0020_Address_x00Id: correspondenceAddressNameId,
			Correspondence_x0020_Contact_x00Id: correspondenceContactNameId,
			Company_x0020_NameId: siteCompanyNameId,
			Address_x0020_NameId: siteAddressNameId,
			Contact_x0020_NameId: siteContactNameId,
			WRS_x0020_CompanyId: wrsCompanyId,
			Customer_x0020_Account_x0020_ManId: customerAccountManagerId,
			Date_x0020_Raised: dateRaised,
			Contract_x0020_Issue_x0020_Type: 'Renewal',
			Quoted_x0020_Contract_x0020_Leng: quotedContractLength,
			Contract_x0020_Start_x0020_Date: proposedStartDate,
			Contract_x0020_End_x0020_Date: endDate,
			Renewal_x0020_Due_x0020_Date: renewalDate,
			Forecast_x0020_Date: proposedStartDate,
			Forecast_x0020_Probability: '3 - Ongoing',
			Contract_x0020_Option: contractOption,
			Contract_x0020_Systems:{
			     results: contractSystems			
			},
			CCTV_x0020__x002d__x0020_Detail: cctvDetails
		}).then(function(){
		    console.log("Created!");
		});
	}
}

Thank you Margarita that work perfectly.

1 Like

I am essentially trying to do the same thing... but it's not working. Since I need to use the ID created by the parent form (this is a creation form) do I need to build in a pause so it has time to save? I looked in the console and it does not report any errors.

Thanks as always for your help!!

fd.spSaved(function (result) {var ContractorNumber = fd.field('Contractor_x0020_Number');
var LastName = fd.field('Last_x0020_Name').value;
var FirstName = fd.field('First_x0020_Name:').value;
var PhoneNumber = fd.field('Phone_x0020_Number').value;
var EmailAddress = fd.field('Email_x0020_Address').value;
var AddressOne = fd.field('Address_x0020_Line_x0020_1').value;
var Address2 = fd.field('Address_x0020_Line_x0020_2').value;
var City = fd.field('City').value;
var State = fd.field('State').value;
var ZipCode = fd.field('ZipCode');
var LicenseNumber = fd.field('License_x0020_Number').value;
var LicenseState = fd.field('License_x0020_State').value;
var DateOfBirth = fd.field('Date_x0020_of_x0020_Birth').value;

	var list = pnp.sp.web.lists.getByTitle("Drivers");
	list.items.add({
		ContractorID: ContractorNumber,
		Last_x0020_Name: LastName,
		First_x0020_Name: FirstName,
		Phone_x0020_Number: PhoneNumber,
		Email_x0020_Address: EmailAddress,
		Address_x0020_Line_x0020_1: AddressOne,
		Address_x0020_Line_x0020_2: AddressTwo,
		City: City,
		State: State,
		Zip_x0020_Code: ZipCode,
		License_x0020_Number: LicenseNumber,
		License_x0020_State: LicenseState,
		Date_x0020_of_x0020_Birth: DateOfBirth })
});

Hi djbeaudin,

I could be wrong here but I think the only values you can access in the saved handler is the ID of the item you just created, so you'd need to do something like this:

sp.web.lists.getByTitle().items.getById(result.ID).get().then(function(item){

   var list = pnp.sp.web.lists.getByTitle("Drivers");
   list.items.add({ 
           City: item.City
           // etc....
   });

});

Hope that makes sense, you essentially need to get the item you just created, and use the item.value of each field to set the values in the new item in the Drivers list.

Not sure if my syntax is the best there, I think maybe you can do it by chaining .then, my syntax nests the second call inside the first. Works for me but maybe not the best pattern to use.

Kind regards
Andy

1 Like

That makes sense. I will give it a try.

Forgot to indicate, you obviously need the name of the current list in the GetByTitle. Whoops.

I got it to work (mostly) but the list I am adding to has a lookup on it. I am passing the ID for the lookup, but it's not accepting it... any help is appreciated... Is there something different I should be doing with the lookup? This code is fired by a button...

this is the code:

var firstName = fd.field('First_x0020_Name').value;
var lastName = fd.field('Last_x0020_Name').value;
var email = fd.field('Email_x0020_Address').value;
var id = fd.field('ID').value;
var list = pnp.sp.web.lists.getByTitle("Drivers");
list.items.add({First_x0020_Name: firstName,
Last_x0020_Name: lastName,
Email_x0020_Address: email,
ContractorID: id
});
fd.control('SPDataTable1').refresh();

If the name of the Lookup field is 'ContractorID', then in a PnP call to update the field you should append an 'Id' on the end.

So the field would be called 'ContractorIDId' - if you pass that field an integer value it should update.

Regards,
Andy

1 Like

That was it!!! Thank you so much. It works perfectly!