You need to sign in to do that
Don't have an account?
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.
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
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
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.
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 ;
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
Its giving following error :
Attempt to de-reference a null object
<apex:commandLink action="{!addtocart}" value="Add to Cart">
<apex:param name="ProductId" value="{!prod.Id}"/>
</apex:commandLink>
means insert a new row in cart object.
with provided productid and quantity = 1
This code will work when, you have default value of Quantity__c as 0
I made default value 0 .
Still its giving same error .
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
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.
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
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
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);
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