Marketcircle    Forums  Hop To Forum Categories  Extending Daylite  Hop To Forums  Scripting    Script to convert Opportunity info from Salesforce to Daylite
Go
New
Find
Notify
Tools
Reply
  
-star Rating Rate It!  Login/Join 
Journeyman
Posted
Hi,

I worked on a script that will convert data from Salesforce's Opportunities to Daylite's Opportunities. It probably won't work for you since we've imported specific Salesforce fields into specific Daylite Extra fields, but I thought I'd share it anyway. Perhaps someone can learn from my coding here.


"This script will load all opportunities
that have extra5 set to the organization name,
lookup the Organization with that name,
and add the opportunity to the organization "

opportunities := objectContext objectsForEntityNamed:'Opportunity'.
pipelines := objectContext objectsForEntityNamed:'Pipeline'.
stateReasons := objectContext objectsForEntityNamed:'OpportunityStateReason'.
stages := objectContext objectsForEntityNamed:'PipelineStage'.

index := 0.
total := opportunities count.

msg := 'Got ' ++ ((opportunities count) stringValue) ++ ' opportunities'.
sys log:msg.


[index < total] whileTrue:
[
	"For each opportunity, we get the extra6, find the associated Organization with name and print the name"

	opportunity := opportunities objectAtIndex:index.
	
	organizationName := opportunity valueForKey:'extra5'.
	opportunityName := opportunity valueForKey:'name'.
	
	"we now lookup the Organization using the opportunity's extra6 if we have one"
	
	(organizationName == nil) ifFalse:
	[
		
		orgQual := BDQualifier qualifierWhereValueOfKey:'name' isEqualTo:organizationName.
		organizations := objectContext objectsForEntityNamed:'Organization' matchingQualifier:orgQual.

		orgsCount := organizations count.
		
		"if we find an organization (should only be one with the name) link our current opportunity to it"
		
		(orgsCount > 0) ifTrue:
		[
		
			sys log:organizationName.
			sys log:opportunityName.
			
			details := opportunity valueForKey:'details'.
	
			details == nil ifFalse:
			[
				sys log:'updating details...'.
				detailsToReplace := NSMutableString stringWithCapacity:4096.
				detailsToReplace setString:details.
				theRange := NSValue rangeWithLocation:0 length: detailsToReplace length.
				numReplaced := detailsToReplace replaceOccurrencesOfString:'<br>'
withString:'\r' options:NSLiteralSearch range:theRange.
	
				sys log:numReplaced.
	
				opportunity takeValue:detailsToReplace forKey:'details'.
			].
		
			organization := organizations lastObject.
			(objectContext linkingContext) linkObject:organization toObject:opportunity.
			
			"find pipeline for extra6"
			pipelineName := opportunity valueForKey:'extra6'.
			(pipelineName == nil) ifFalse:
			[
				sys log:'setting pipeline: ' ++ pipelineName.
				pipelineQual := BDQualifier qualifierWhereValueOfKey:'name' isEqualTo:pipelineName.
				filteredPipelines := pipelines filteredArrayUsingQualifier:pipelineQual.
				(filteredPipelines == nil) ifFalse:
				[
					pipeline := filteredPipelines lastObject.
					opportunity addObject:pipeline toBothSidesOfRelationshipWithKey:'currentPipeline'.
					
					"set pipeline stage"
					stageName := opportunity valueForKey:'extra8'.
					(stageName == nil) ifFalse:
					[
						sys log:'setting stageName: ' ++ stageName.
						foundPipelineQual := BDQualifier qualifierWhereValueOfKey:'pipeline' isEqualTo:pipeline.
						stageQual := BDQualifier qualifierWhereValueOfKey:'name' isEqualTo:stageName.
						quals := {foundPipelineQual, stageQual}.
						andQualifier := (((BDAndQualifier alloc) initWithQualifierArray:quals) autorelease).
						filteredStages := stages filteredArrayUsingQualifier:andQualifier.
						(filteredStages == nil) ifFalse:
						[
							stage := filteredStages lastObject.
							opportunity takeValue:stage forKey:'currentPipelineStage'.
						].
					].
					
				].
			].
			
			"set state to value from extra3"
			stateValue := opportunity valueForKey:'extra3'.
			(stateValue == nil) ifFalse:
			[
				sys log:'setting opportunity state: ' ++ stateValue.
				opportunity takeValue:stateValue forKey:'opportunityStateType'.
			].
			
			"set state reason to value from extra4"
			stateReason := opportunity valueForKey:'extra4'.
			(stateReason == nil) ifFalse:
			[
				sys log:'setting state reason: ' ++ stateReason.
				reasonQual := BDQualifier qualifierWhereValueOfKey:'name' isEqualTo:stateReason.
				filteredReasons := stateReasons filteredArrayUsingQualifier:reasonQual.
				(filteredReasons == nil) ifFalse:
				[
					reason := filteredReasons lastObject.
					opportunity takeValue:reason forKey:'opportunityStateReason'.
				].
			].
			

			
			"add LineItem to opportunity"
			productName := (opportunity valueForKey:'extra7').
			(productName == nil) ifFalse:
			[
				sys log:'creating LineItem for: ' ++ productName.
				cc := (objectContext creationContext).
				newLineItem := cc createObjectForEntityNamed:'LineItem' addToObjectContext:true.
				newLineItem takeValue:(opportunity valueForKey:'unitPrice') forKey:'costOfGoods'.
				newLineItem takeValue:productName forKey:'name'.
				newLineItem takeValue:(opportunity valueForKey:'units') forKey:'units'.
				opportunity addObject:newLineItem toBothSidesOfRelationshipWithKey:'lineItems'.
			].
			
			sys log:'SAVING...'.
			objectContext saveChanges.
		].
	].

	
	index := index + 1.
	
]. 
 
Posts: 84 | Location: Calgary, Canada | Registered: December 15, 2006Reply With QuoteEdit or Delete MessageReport This Post
 Previous Topic | Next Topic powered by eve community  
 

Marketcircle    Forums  Hop To Forum Categories  Extending Daylite  Hop To Forums  Scripting    Script to convert Opportunity info from Salesforce to Daylite

© Copyright 2006 Marketcircle Inc. All rights reserved.