You need to sign in to do that
Don't have an account?
LALALA
populate lookup field(account name) on opportunity upon custom object(Orders) created
trigger CreateOppFromOrder on Orders__c (after insert) {
Set<String> orderNames = new Set<String>();
for (Orders__c order: Trigger.new) {
orderNames.add(order.Name);
}
Map<String, OrderFromWeb__c> orderFromWebMap = new Map<String, OrderFromWeb__c>();
for (OrderFromWeb__c ofw: [
select OrderNumber__c,
PO_Number__c,
PaymentStatus__c,
PaymentMethod__c
from OrderFromWeb__c
where OrderNumber__c in :orderNames]) {
orderFromWebMap.put(ofw.OrderNumber__c, ofw);
}
List<Opportunity> Opps = new List<Opportunity>();
for (Orders__c order : Trigger.new) {
OrderFromWeb__c ofw = orderFromWebMap.get(order.Name);
Opportunity op = new Opportunity(AccountId = order.Contacts__r.Account.Name);
if (
ofw != null &&
ofw.PaymentMethod__c == 'PayPal Standard') {
op.Name = date.today() + '_W:' + Order.Name;
} else {
op.Name = date.today() + '_:PO' + Order.Name;
}
//PO Number
if(ofw.PaymentMethod__c == 'PayPal Standard' && ofw.OrderNumber__c == order.Name)
{
op.PO_Number__c = Order.Name;
}
else
{
op.PO_Number__c = ofw.PO_Number__c;
}
//Account Name
op.AccountId = ??????? what i write is : op.AccountId = order.Contacts__r.Account.Name but isn't work.......
//Stage
op.StageName = 'Closed Won';
//Close Date
op.CloseDate = date.today();
opps.add(op);
}
insert opps;
}
(see bold font in code, Please)
Because Account Name field on Opportunity is lookup field, So how can I populate lookup data to opportunity?? thanks :)
Set<String> orderNames = new Set<String>();
for (Orders__c order: Trigger.new) {
orderNames.add(order.Name);
}
Map<String, OrderFromWeb__c> orderFromWebMap = new Map<String, OrderFromWeb__c>();
for (OrderFromWeb__c ofw: [
select OrderNumber__c,
PO_Number__c,
PaymentStatus__c,
PaymentMethod__c
from OrderFromWeb__c
where OrderNumber__c in :orderNames]) {
orderFromWebMap.put(ofw.OrderNumber__c, ofw);
}
List<Opportunity> Opps = new List<Opportunity>();
for (Orders__c order : Trigger.new) {
OrderFromWeb__c ofw = orderFromWebMap.get(order.Name);
Opportunity op = new Opportunity(AccountId = order.Contacts__r.Account.Name);
if (
ofw != null &&
ofw.PaymentMethod__c == 'PayPal Standard') {
op.Name = date.today() + '_W:' + Order.Name;
} else {
op.Name = date.today() + '_:PO' + Order.Name;
}
//PO Number
if(ofw.PaymentMethod__c == 'PayPal Standard' && ofw.OrderNumber__c == order.Name)
{
op.PO_Number__c = Order.Name;
}
else
{
op.PO_Number__c = ofw.PO_Number__c;
}
//Account Name
op.AccountId = ??????? what i write is : op.AccountId = order.Contacts__r.Account.Name but isn't work.......
//Stage
op.StageName = 'Closed Won';
//Close Date
op.CloseDate = date.today();
opps.add(op);
}
insert opps;
}
(see bold font in code, Please)
Because Account Name field on Opportunity is lookup field, So how can I populate lookup data to opportunity?? thanks :)
f you already have contact associated to the order and contact is associated to an account.
Now you want to create an opportunity and want to associate with the same account as contact.
Then you should write it as
//But remember Trigger.new can't give you relationship fields so you need to fetch the contact related to the order
Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
still have problems : it shows Entity is org-accessible
Account acct = [select name from account where id in: order.id];
insert acct;
ID acctID = acct.ID;
still same problems: entity is not org-accessible
by the way, order don't have accountId......only opportunity have....
oh my god,,why i can't fix it....
System.QueryException: List has more than 1 row for assignment to SObject: Trigger.CreateOppFromOrder: line 15, column 1
@Mudasir Wani
Contact con = [Select id,name,AccountId from Contact];
Account acct = [select name from account where id =: con.AccountId];
insert acct;
ID acctID = acct.Id;
is that contact that what i typed are correct? i feel still alittle bit wrong..... sorry, you are so nice:) please help me
Below query should give error as yo are fetching a list of contacts and trying to put them in a single contact
You can use the limit 1 and the query should be like
Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
sorry, still doesn't work that because in 'order' doesn't has ContactId,,so id can't =: order.ContactId
is that another way to fix this problems ?
sorry, I am too new,,,, I don't know how to do it...Thanks
Just let me know the relationship between your
Order__c , Opportunity and Account
And I will help you with the code