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
arosysarosys 

Upsert with Master object

I hav two object custom objects cart & product.

cart has two fields a master detail field related to product object and quantity.

what i want to do is when i insert a new cart , it will check if product already exist it will increment the value of quantity else

insert the cart.

This is what i am presently doing .

 

     String ProductId = System.currentPageReference().getParameters().get('ProductId');
        
        cart__c cart = new cart__c ();
        
        cart.Product__c = ProductId ;
        
        cart.Quantity__c= 1;
        
        insert cart ;

 

Can anyone please help me in this.

 

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Try this

 

String ProductId = System.currentPageReference().getParameters().get('ProductId');
List<Cart__c> cartList = new List<Cart__c>();
Product prod = [select id, name ,(select quantity, product__c from Carts__r) from Product where id =: ProductId];
if(prod.Carts__r != NULL && prod.Carts__r.size()>0){
for(Cart__c cartObj : prod.Carts__r){
if(cartObj.Quantity__c != NULL){
cartObj.Quantity__c += 1;
cartList.add(cartObj);
}
}
if(cartList != NULL && cartList.size()>0){
upsert cartList;
}
}
else{
cart__c cart = new cart__c ();

cart.Product__c = ProductId ;

cart.Quantity__c= 1;

insert cart ;
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

All Answers

Neha LundNeha Lund

If Cart is the master object and you want to count the child records, then you can use roll up summary field to calculate the count on your master object

arosysarosys

But cart is not master object , it is just having a look up field for product to get details of a particular product from product object.

Neha LundNeha Lund

Try this...

this will work fine, if Quantity field has default value to 0

String ProductId = System.currentPageReference().getParameters().get('ProductId');
        
        cart__c cart = new cart__c ();
        
        cart.Product__c = ProductId ;
        
        cart.Quantity__c+= 1;
        
        upsert cart ;

 

 

souvik9086souvik9086

Can you be more clear on your requirement?

 

Let me summarize what I understand.

On the insert of a cart, it will check the product, and if the product exists then the quantity is increased by one, and if not is selected then insert the cart means?

 

Thanks

arosysarosys

Its giving following error :

 

Attempt to de-reference a null object

Error is in expression '{!addtocart}' in page siteproducts
 
This is my code on page :
 

<apex:commandLink action="{!addtocart}" value="Add to Cart">
<apex:param name="ProductId" value="{!prod.Id}"/>
</apex:commandLink>

arosysarosys

means insert a new row in cart object.

 

with provided productid and quantity = 1

Neha LundNeha Lund

This code will work when, you have default value of Quantity__c as 0

 

arosysarosys

I made default value 0 .

Still its giving same error .

souvik9086souvik9086

Yes that I understand, But you said that " if product already exist" .. I mean where it will exist before cart insert? Can you share more info about? Then Better answers will be tried to provide.

 

Thanks

 

arosysarosys

Ok whenever a product is inserted for the first time.

In "Product_c" which is a master detail field productId will be inserted which is passed from page.

and in quantity 1 is inserted.

 

Else if a product is inserted second time means , cart object already had a row with same productid in that case i just need to increment the value of quantity.

 

souvik9086souvik9086

Try this

 

String ProductId = System.currentPageReference().getParameters().get('ProductId');
List<Cart__c> cartList = new List<Cart__c>();
Product prod = [select id, name ,(select quantity, product__c from Carts__r) from Product where id =: ProductId];
if(prod.Carts__r != NULL && prod.Carts__r.size()>0){
for(Cart__c cartObj : prod.Carts__r){
if(cartObj.Quantity__c != NULL){
cartObj.Quantity__c += 1;
cartList.add(cartObj);
}
}
if(cartList != NULL && cartList.size()>0){
upsert cartList;
}
}
else{
cart__c cart = new cart__c ();

cart.Product__c = ProductId ;

cart.Quantity__c= 1;

insert cart ;
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

This was selected as the best answer
arosysarosys

Thanks for the reply.

But this doesnt work either.

 

 how will it upsert on the basis of products if we'll not provide externalid.

 

I hope you are getting me correct about product_c field.

 

I created custom field in cart object , choosing field type "Master-Detail Relationship"

and in related to i selected product object.

and api name of that field is Product_c

arosysarosys

Ok its done just modified one line of your code . :

 

from

 

if(cartObj.Quantity__c != NULL){
cartObj.Quantity__c = 1;
cartList.add(cartObj);

 

to

 

if(cartObj.Quantity__c != NULL){
cartObj.Quantity__c+= 1;
cartList.add(cartObj);

souvik9086souvik9086

Finally!! :)

 

Yes first time I missed that..I edited that after some time. You may have not seen the edited one. Anyways its done.

 

Thanks